Day 34: Working with Services in Kubernetes

Day 34: Working with Services in Kubernetes

Welcome back to the 90DaysOfDevOps challenge! Today, we'll dive into the world of Kubernetes Services, an essential component for enabling communication between various parts of your application. If you missed any previous blogs, especially Day-32 where we deployed our todo-app, feel free to check it out for context.

What are Services in K8s?

In Kubernetes, Services provide a way to expose and access your application within the cluster or externally. Before we apply a Service, accessing pods directly via their IP addresses might work, but it's not a sustainable solution. When pods are deleted or scaled, their IP addresses change, making it challenging to maintain consistent communication.

In Kubernetes, services are a crucial component for enabling communication between different parts of your application. There are several types of services, each serving a specific purpose. Let's explore the main types with examples:

  1. ClusterIP Service:

    • Description: This is the default service type. It exposes the service on an internal IP within the cluster, making it accessible only from within the cluster.

    • Example:

        apiVersion: v1
        kind: Service
        metadata:
          name: my-service
        spec:
          selector:
            app: my-app
          ports:
            - protocol: TCP
              port: 80
              targetPort: 8080
      
  2. NodePort Service:

    • Description: Exposes the service on a static port on each node's IP, allowing external access to the service.

    • Example:

        apiVersion: v1
        kind: Service
        metadata:
          name: my-nodeport-service
        spec:
          selector:
            app: my-app
          type: NodePort
          ports:
            - protocol: TCP
              port: 80
              targetPort: 8080
      
  3. LoadBalancer Service:

    • Description: Provides external access to the service via a cloud provider's load balancer. Automatically assigns an external IP to route traffic to the service.

    • Example:

        apiVersion: v1
        kind: Service
        metadata:
          name: my-lb-service
        spec:
          selector:
            app: my-app
          type: LoadBalancer
          ports:
            - protocol: TCP
              port: 80
              targetPort: 8080
      
  4. ExternalName Service:

    • Description: Maps the service to an external DNS name without exposing an IP. Used for accessing services outside the cluster.

    • Example:

        apiVersion: v1
        kind: Service
        metadata:
          name: external-service
        spec:
          type: ExternalName
          externalName: example.com
          ports:
            - protocol: TCP
              port: 80
      
  5. Headless Service:

    • Description: Acts as a service without a cluster IP. Useful for scenarios where you need DNS records for individual pods.

    • Example:

        apiVersion: v1
        kind: Service
        metadata:
          name: headless-service
        spec:
          clusterIP: None
          selector:
            app: my-app
          ports:
            - protocol: TCP
              port: 80
              targetPort: 8080
      

These service types play a crucial role in defining how your applications are exposed and accessed, providing flexibility and control over communication within and outside the Kubernetes cluster.

Before applying service :

Accessing Application within The Cluster Using Pod IP:

-->ssh to minikube Node:

Running

Trying to Access Pod After Deleting It:

Again New Pod is Created With Different IP:

We can not access our pod with previous IP , so to over this we need services.

Task-1: Creating a ClusterIP Service

Let's start by creating a ClusterIP Service for our todo-app Deployment.

  1. Creating Service Definition:

     apiVersion: v1
     kind: Service
     metadata:
       name: node-service
       namespace: nodespace
       labels:
          app: vmbox
     spec:
       selector:
         app: vmbox
       ports:
         - protocol: TCP
           port: 8000
           targetPort: 8000
    

  2. Applying the Service Definition:

     kubectl apply -f service.yml -n <namespace-name>
    

  3. Verifying the Service:

    Access the todo-app using the Service's IP and Port within the cluster.

Task-2: Creating a NodePort Service

    • Trying to access application outside the cluster:

      • But failed to connect:

Now, let's make our todo-app accessible from outside the cluster using a NodePort Service.

  1. Creating NodePort Service Definition:

     apiVersion: v1
     kind: Service
     metadata:
       name: node-service
       namespace: nodespace
       labels:
          app: vmbox
     spec:
       selector:
         app: vmbox
       type: NodePort
       ports:
         - protocol: TCP
           port: 8000
           targetPort: 8000
           nodePort: 30012
    

  2. Applying the NodePort Service Definition:

    1.  kubectl apply -f nodeport-service.yml -n <namespace-name>
      

  3. Verifying NodePort Service:

    Access the todo-app from another Pod in the cluster using the assigned NodePort.

Task-3: Creating a LoadBalancer Service

Different from NodePort, LoadBalancer Services allow external access in a more secure way.

  1. Creating LoadBalancer Service Definition:

     apiVersion: v1
     kind: Service
     metadata:
       name: node-service
       namespace: nodespace
       labels:
          app: vmbox
     spec:
       selector:
         app: vmbox
       type: LoadBalancer
       ports:
         - protocol: TCP
           port: 8000
           targetPort: 8000
    

  2. Applying LoadBalancer Service Definition:

     kubectl apply -f loadbalancer-service.yml -n <namespace-name>
    

  3. Verifying LoadBalancer Service:

    Access the todo-app from another Pod in the cluster using the assigned LoadBalancer's external IP.

NodePort vs. LoadBalancer:

  • NodePort exposes the service on a static port on each node, whereas LoadBalancer provisions an external IP for access and balances the traffic across nodes.

Congratulations! You've successfully worked with Kubernetes Services, enhancing the accessibility and scalability of your todo-app. In the next blog, we'll explore more advanced topics in the 90DaysOfDevOps challenge. Stay tuned!