Day 34 : Working with Services in Kubernetes

Day 34 : Working with Services in Kubernetes

ยท

3 min read

Welcome back to our Kubernetes journey! Today, we delve into the realm of Services in Kubernetes. Services are a crucial aspect of managing communication and networking within a Kubernetes cluster, allowing different parts of your application to communicate with each other or with external entities efficiently. In this blog post, we'll walk through the tasks of creating and managing Services in Kubernetes, using our familiar todo-app as an example.

What are Services in Kubernetes?

In Kubernetes, Services are an abstraction layer that provides a consistent way to access a set of Pods. They enable decoupling between the logical service and the Pods that implement it, offering benefits like load balancing, service discovery, and more. There are several types of Services in Kubernetes, each serving specific use cases:

  1. ClusterIP Service: Exposes the Service on an internal IP within the cluster. This type of Service is accessible only from within the cluster.

  2. NodePort Service: Exposes the Service on a static port on each Node's IP. It's accessible externally (outside the cluster) using the Node's IP and the static port.

  3. LoadBalancer Service: Exposes the Service externally using a cloud provider's load balancer. It assigns an external IP to the Service, making it accessible from outside the cluster.

Now, let's dive into our tasks for the day.

Task 1: Creating a Service for todo-app Deployment

First up, we'll create a Service for our todo-app Deployment from Day 32. This Service will allow us to access our todo-app within the Kubernetes cluster.

Steps:

  1. Create Service Definition: We'll start by defining our Service in a YAML file (service.yaml).

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-service
     spec:
       type: NodePort
       selector:
         app: todo-app
       ports:
           - port: 80
           targetPort: 8000
           nodePort: 30007
    

  2. Apply Service Definition: Using the kubectl apply command, we'll apply the Service definition to our Kubernetes cluster.

     kubectl apply -f service.yaml -n project
    

  3. Verify Service: We'll verify that the Service is working by accessing the todo-app using the Service's IP and Port within our Namespace.

Task 2: Creating a ClusterIP Service for todo-app

Next, we'll create a ClusterIP Service for accessing the todo-app from within the cluster itself.

Steps:

  1. Create ClusterIP Service Definition: We'll define our ClusterIP Service in a YAML file (cluster-ip-service.yml).

     yamlCopy codeapiVersion: v1
     kind: Service
     metadata:
       name: todo-app-clusterip
       labels:
         app: todo-app
     spec:
       type: ClusterIP
       selector:
         app: todo-app
       ports:
         - protocol: TCP
           port: 80
           targetPort: 8080
    
  2. Apply ClusterIP Service Definition: Apply the ClusterIP Service definition to our Kubernetes cluster.

     kubectl apply -f cluster-ip-service.yaml -n project
    

  3. Verify ClusterIP Service: Access the todo-app from another Pod in the cluster within our Namespace to ensure the ClusterIP Service is working.

Task 3: Creating a LoadBalancer Service for todo-app

Lastly, we'll create a LoadBalancer Service to access the todo-app from outside the cluster, making it accessible to external users.

Steps:

  1. Create LoadBalancer Service Definition: Define our LoadBalancer Service in a YAML file (load-balancer-service.yaml).

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-app-loadbalancer
     spec:
       type: LoadBalancer
       selector:
         app: todo-app
       ports:
         - port: 80
           targetPort: 8000
    

  2. Apply LoadBalancer Service Definition: Apply the LoadBalancer Service definition to our Kubernetes cluster.

     kubectl apply -f load-balancer-service.yaml -n project
    

  3. Verify LoadBalancer Service: Access the todo-app from outside the cluster within our Namespace to confirm that the LoadBalancer Service is working.

By completing these tasks, we've gained a deeper understanding of Kubernetes Services and how they facilitate communication and access within our cluster. Remember, Services play a vital role in maintaining a scalable and reliable Kubernetes environment for your applications.

I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .

thank you : )

ย