Working with Services in Kubernetes

3 min read

Cover Image for Working with Services in Kubernetes

What are services?

In Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy by which to access them. It provides a stable network endpoint (IP address and port) for accessing the Pods that belong to the Service, even if the underlying Pods are dynamically created or terminated.

Services can be accessed within the cluster or exposed to the external network depending on their type. Here are some commonly used service types in Kubernetes:

  • ClusterIP: This is the default service type. It exposes the Service on a cluster-internal IP address, which can be used by other Pods within the cluster to access the Service.

  • NodePort: This type exposes the Service on a static port on each selected node in the cluster. It makes the Service accessible from outside the cluster by targeting any node's IP address and the assigned NodePort.

  • LoadBalancer: This type creates an external load balancer that distributes traffic to the Service. It works with cloud providers that support external load balancers, assigning a unique external IP address to the Service.

To view the services running in your Kubernetes cluster, you can use the following command:

kubectl get services

This command will display a list of services in the current namespace, including their names, type, cluster IP, external IP (if any), and ports.

To get more detailed information about a specific service, you can use the following command:

kubectl describe service <service-name>

Replace <service-name> with the name of the service, you want to get information about.

Services play a crucial role in Kubernetes for achieving reliable and scalable communication between different components of an application. They provide a way to abstract and manage access to your application's workloads and ensure their availability.

Task-1:

  1. create a service.yaml file

     apiVersion: v1
     kind: Service
     metadata:
       name: todo-service
     spec:
       type: NodePort
       selector:
         app: todo-app
       ports:
           # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
         - port: 80
           targetPort: 8000
           # Optional field
           # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
           nodePort: 30007
    
  2. run the command kubectl apply -f service.yaml

  3. run the command to see the URL at which the service node port interacts with :

    minikube service todo-service --url

node port allows us to interact with the pods outside the cluster. As you can see, we accessing the Django-todo app from the browser.

Task-2:

How to create a ClusterIP?

  1. create a yaml file with type clusterIP

     apiVersion: v1
     kind: Service
     metadata:
         name: django-app-service
         namespace: my-namespace
     spec:
         type: NodePort
         selector:
           app: django-app
         ports:
           - protocol: TCP
             port: 8000
             targetPort: 8000
         type: ClusterIP
    
  2. run this command to confirm that the service is running in your namespace kubectl get services -n my-namespace

Task-3:

how to create a load balancer?

  1. create a yaml file deployment

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: myapp-deployment
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: myapp
       template:
         metadata:
           labels:
             app: myapp
         spec:
           containers:
             - name: myapp-container
               image: your-app-image
               ports:
                 - containerPort: 80
    
  2. create a service yaml file

     apiVersion: v1
     kind: Service
     metadata:
       name: myapp-service
     spec:
       selector:
         app: myapp
       ports:
         - protocol: TCP
           port: 80
           targetPort: 80
       type: LoadBalancer
    
  3. run command kubectl apply -f deployment.yaml and

    run kubectl apply -f service.yaml

  4. verify the service is created kubectl get services

  5. access the load balancer run command: minikube ip

Happy learning!!