Mastering Services in Kubernetes : A Step-by-Step Guide

Urvish SuhagiyaUrvish Suhagiya
6 min read

What is a Kubernetes Service?

In Kubernetes, a Service is an abstraction that defines a logical set of Pods and provides a stable network endpoint (IP address) for accessing them. Pods in Kubernetes are ephemeral, meaning their IP addresses can change over time, especially when they are recreated. This can cause problems if other Pods or external clients need to communicate with them. Services solve this problem by providing a consistent, stable IP address (or DNS name) and can route traffic to the Pods, even if their individual IP addresses change.

Types of Kubernetes Services

Kubernetes offers several types of Services, but today we will focus on three common ones:

  1. ClusterIP Service: This is the default Service type. It exposes the Service only within the cluster. It’s used when you want Pods to communicate with each other inside the cluster but don’t need external access.

  2. LoadBalancer Service: This type exposes the Service externally using a cloud provider's load balancer. It’s useful when you want to expose your application to the outside world, such as to users over the internet.

  3. NodePort Service: Exposes the Service on a specific port on all the nodes in the cluster. This is useful for development or debugging when you want to access the application externally but don’t want to use a full-fledged LoadBalancer.

Let’s now go through each task in today’s learning journey and understand how to work with Services in Kubernetes.


Task 1: Creating a Basic Service for Your Application

What Are We Doing?

In this task, we’ll create a simple ClusterIP Service for our todo-app Deployment. This will make the app accessible from within the Kubernetes cluster using a stable IP address.

Steps:

  1. Create the Service Definition File (service.yml)

    First, we need to create a YAML file (service.yml) that will define how the Service should behave. This file will describe the necessary configuration to expose the todo-app Deployment via a Service.

    Here’s an example of what the service.yml might look like:

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-app-service
       namespace: <namespace-name>  # Specify the namespace where your app is running
     spec:
       selector:
         app: todo-app  # This selects the Pods that the Service will target
       ports:
         - protocol: TCP
           port: 80      # The port that the Service will expose
           targetPort: 8080  # The port inside the Pods that the app is running on
       type: ClusterIP  # This makes the Service accessible only within the cluster
    
    • apiVersion: This specifies the version of the Kubernetes API we are using.

    • kind: This specifies that we are defining a Service.

    • metadata: This section defines metadata for the Service, such as its name and namespace.

    • spec: This defines the actual specification of the Service:

      • selector: The selector ensures that this Service is tied to the Pods with the label app: todo-app. This ensures the Service forwards traffic to the correct Pods.

      • ports: This defines the port configuration. The Service will listen on port 80, and traffic will be forwarded to port 8080 in the Pods.

      • type: In this case, we're using ClusterIP, which makes the Service accessible only within the cluster.

  2. Apply the Service to Kubernetes Cluster

    Once the service.yml file is ready, you need to apply it to your Kubernetes cluster using the kubectl apply command. This command will tell Kubernetes to create the Service defined in the YAML file.

    Run this command:

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

    This command tells Kubernetes to create the Service for the todo-app in the specified namespace.

  3. Verify the Service

    After the Service is created, you can verify its status by using the following command:

     kubectl get svc -n <namespace-name>
    

    This command lists all the services in the specified namespace. You should see an entry for your todo-app-service, and it will have a ClusterIP address. You can use this IP to access your application from other Pods within the same cluster.


Task 2: Creating a ClusterIP Service for Internal Access

In this task, we will create a ClusterIP service that makes the todo-app accessible from other Pods within the cluster. This type of Service is useful when you want to expose your app to other applications running inside your Kubernetes cluster, but not to external users.

Steps:

  1. Create the ClusterIP Service Definition

    The cluster-ip-service.yml file will look similar to the previous service definition. The only difference is that we will specifically use ClusterIP as the Service type.

    Here’s how the YAML file might look:

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-app-clusterip
       namespace: <namespace-name>
     spec:
       selector:
         app: todo-app
       ports:
         - protocol: TCP
           port: 80
           targetPort: 8080
       type: ClusterIP  # Ensures the Service is only accessible within the cluster
    
  2. Apply the ClusterIP Service

    Use the same command to apply the ClusterIP service:

     kubectl apply -f cluster-ip-service.yml -n <namespace-name>
    
  3. Verify the ClusterIP Service

    Use this command to check the Service:

     kubectl get svc -n <namespace-name>
    

    You will see the ClusterIP address listed in the output. You can now test accessing the application from another Pod inside the cluster using the ClusterIP and port 80.


Task 3: Creating a LoadBalancer Service for External Access

In this task, we will expose the todo-app to the outside world by creating a LoadBalancer Service. This type of Service is useful when you want your app to be accessible from external clients, such as users browsing the web.

Steps:

  1. Create the LoadBalancer Service Definition

    In the load-balancer-service.yml file, we will define a LoadBalancer type Service. This will expose the todo-app externally through a cloud provider’s load balancer (if your cluster is hosted on a cloud provider that supports this).

    Here’s the YAML file:

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-app-loadbalancer
       namespace: <namespace-name>
     spec:
       selector:
         app: todo-app
       ports:
         - protocol: TCP
           port: 80
           targetPort: 8080
       type: LoadBalancer  # This exposes the app to the outside world
    
  2. Apply the LoadBalancer Service

    To apply this Service definition, run:

     kubectl apply -f load-balancer-service.yml -n <namespace-name>
    
  3. Verify the LoadBalancer Service

    Once applied, use the following command to verify the Service:

     kubectl get svc -n <namespace-name>
    

    You will see an external IP assigned to the LoadBalancer. This IP can be used to access your todo-app from outside the Kubernetes cluster.


Conclusion

By completing these tasks, you’ve learned how to create and manage Kubernetes Services to expose your application within and outside the cluster. Kubernetes Services provide a stable way to manage network access to Pods, ensuring that even if Pods are recreated or their IPs change, the service remains accessible and traffic is routed correctly.

Mastering Kubernetes Services is an essential skill for anyone working with Kubernetes, as they enable seamless communication between Pods and clients, both internally and externally.

Keep exploring and happy learning! 🚀


Resources:

2
Subscribe to my newsletter

Read articles from Urvish Suhagiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Urvish Suhagiya
Urvish Suhagiya

Exploring the world of DevOps 🌐.