Working with Services in Kubernetes | Day 34 of | 90 Days of DevOps

Working with Services in Kubernetes | Day 34 of | 90 Days of DevOps

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.

Services are essential for managing network traffic in Kubernetes clusters. They provide several benefits, including:

  • Stable network identities: Services have a unique name and a stable IP address, which makes it easy to connect to Pods even if their IP addresses change.

  • Load balancing: Services can distribute traffic across multiple Pods, which helps to improve application performance and availability.

  • Service discovery: Services make it easy for other Pods to find each other, even if they are not running on the same node.

  • Health checks: Services can perform health checks on Pods, and automatically remove Pods that are unhealthy from the load balancing pool.

Task-1: Create a Service for your todo-app Deployment

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

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

This YAML file defines a Service named todo-app that targets Pods with the label app: todo-app. The Service exposes the Pods on port 80, which is the same port that the Pods are listening on.

Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command:

kubectl apply -f service.yml -n default

This command will create the Service in your K8s cluster.

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

kubectl get service todo-app -n default

This command will print the Service's IP address and port.

curl <service-ip>:<port>

This command will curl the todo-app application.

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:

apiVersion: v1
kind: Service
metadata:
  name: cluster-ip-todo-app
  namespace: default
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

This YAML file defines a ClusterIP Service named cluster-ip-todo-app that targets Pods with the label app: todo-app. The Service exposes the Pods on port 80, which is the same port that the Pods are listening on. The type: ClusterIP property specifies that this Service is a ClusterIP Service.

Apply the ClusterIP Service definition to your K8s (minikube) cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name> command:

kubectl apply -f cluster-ip-service.yml -n default

This command will create the ClusterIP Service in your K8s cluster.

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

kubectl get pods -n default

This command will print the names of the Pods in your Namespace.

kubectl exec -it <pod-name> -- curl <cluster-ip-service-ip>:<port>

This command will curl the todo-app application from another Pod in the cluster.

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:

YAML

apiVersion: v1
kind: Service
metadata:
  name: load-balancer-todo-app
  namespace: default
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
  1. Apply the YAML file to create the Service by running:
kubectl apply -f load-balancer-service.yml -n default

This command applies the LoadBalancer Service definition to your Kubernetes cluster.

  1. Verify that the Service is created by running:
kubectl get services todo-app-loadbalancer-service -n default

This command displays the details of the todo-app-loadbalancer-service, including its external IP address. Use this external IP address and port

This YAML file defines a LoadBalancer, that exposes port 80 of our Pods as port 80 on an external IP address. The Service belongs to the todo Namespace and select the Pods that have the label app: todo. The type: LoadBalancer specifies that this is a LoadBalancer Service.

Conclusion:

This article shows you how to create the services of the todo app in the Kubernetes cluster that allows us to expose and access our applications in a cluster.

I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please feel free to leave a comment below.

Thank you for reading and stay tuned for more!