Day 34: Creating Kubernetes Services - A Step-by-Step Guide

Day 34: Creating Kubernetes Services - A Step-by-Step Guide

ยท

4 min read

Congratulations ๐ŸŽŠ on your learning on Deployments in K8s on Day 33

What are Services in K8s?

In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.

If you want to read more about Service and networking in Kubernetes, read Day 33

Task-1

Create a Service for your todo-app Deployment from Day 32

Create a Service definition for your todo-app Deployment in a YAML file.

Apply the Service definition to your K8s cluster.

Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.

Steps:

  • We are using the same TODO app we used on day 32 for deployment in Kubernetes.

  • Create a YAML file named "service.yml" and write the following content in it:

      apiVersion: v1
      kind: Service
      metadata:
        name: todo-service
        namespace: my-app
      spec: 
        selector:
           app: todo-app
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8080
        type: NodePort
    
  • Apply the service to the Kubernetes cluster by using the following command:

      kubectl apply -f service.yml
    
  • To verify that the service is working, use the following command:

      kubectl get svc todo-service -n my-app
    

    This will provide information about the service, including the "EXTERNAL-IP" and "PORT". You can use the external IP and port to access your todo-app on your web browser or another client.


Task-2

Create a ClusterIP Service for accessing the todo-app from within the cluster

Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.

Apply the ClusterIP Service definition to your K8s cluster.

Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

Steps:

  • Create a YAML file of ClusterIP service named "cluster-ip-service.yml". And write the following content in it.

      apiVersion: v1
      kind: Service
      metadata:
        name: todo-app-clusterip
        namespace: my-app
      spec: 
        selector:
          app: todo-app
        ports: 
         - protocol: TCP
           port: 80
           targetPort: 8080
    

    we define a ClusterIP Service named todo-app-clusterip-service that selects pods labelled with app: todo-app. It exposes port 80 on the Service, which forwards traffic to port 8080 on the selected pods.

  • To apply the ClusterIP service to the Kubernetes, follow the command:

      kubectl apply -f cluster-ip-service.yml -n my-app
    
  • To verify that the ClusterIP service is working and accessible, you need to create a temporary test pod and access the todo-app with the test pod.

    Create a temporary pod for testing:

      kubectl run -i --tty --rm debug-pod --image=busybox -n my-app -- /bin/sh
    

    Inside the debug pod, you can access the todo-app by using the curl command

      curl http://todo-app-clusterip-service:80
    

    You should receive a response from the todo-app that it's accessible via the ClusterIP service.


Task-3

Create a LoadBalancer Service for accessing the todo-app from outside the cluster

Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

Apply the LoadBalancer Service definition to your K8s cluster.

Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.

Steps:

  • Create a YAML file of LoadBalancer named "load-balancer.yml and write the following content in it.

      apiVersion: v1
      kind: Service
      metadata:
        name: todo-app-loadbalancer
        namespace: my-app
      spec: 
        selector: 
          app: todo-app
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8080
        type: LoadBalancer
    
  • To apply the load balancer service to Kubernetes, write the following command:

      kubectl apply -f load-balancer.yml -n my-app
    
  • You can access the Load Balancer service using the "minikube service" command. This command opens the service in your default web browser. Write the following command:

      minikube service todo-app-loadbalancer -n my-app
    

    This command will open a web browser with the external IP and port assigned to the LoadBalancer service, allowing you to access the todo-app outside the Minikube cluster.

  • Congratulations, you've dived deep into the Kubernetes and performed the tasks very well. Keep up the good work.


<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>

ย