Day 34 Task: Working with Services in Kubernetes

Day 34 Task: Working with Services in Kubernetes

🙏 Introduction:

In this blog, we will explore one of the most critical components of Kubernetes services & its ability to manage services. In this blog, we will discuss what services are in Kubernetes and how they work.

Service Types in Kubernetes

Kubernetes supports four types of services, each with a different purpose:

  1. ClusterIP: This is the default service type in Kubernetes. It provides a stable IP address and DNS name for accessing pods within the cluster. The ClusterIP service is only accessible from within the cluster.

  2. NodePort: This service type exposes the service on a static port on each node in the cluster. It provides a way to access the service from outside the cluster.

  3. LoadBalancer: This service type provides a load balancer for the service in cloud environments that support load balancers. It automatically creates a load balancer and assigns a public IP address to the service.

  4. ExternalName: This service type provides a way to access an external service by creating a DNS CNAME record. It does not create any endpoints or perform any load balancing.

How do Services work in Kubernetes?

When you create a service in Kubernetes, it creates a virtual IP address and DNS name for accessing the pods. The service then selects a set of pods based on labels and forwards traffic to them using a load-balancing algorithm.

When a pod is added or removed from the set, the service automatically updates its list of endpoints. Services also provide health checking by periodically sending requests to the pods to ensure they are still running.

💼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 (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.

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

Steps :

  1. Create a Service definition for your todo-app Deployment in a YAML file
apiVersion: v1
kind: Service
metadata:
  name: todo-service
  labels:
    app: todo-app
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - name: http
      port: 80
      targetPort: 8000

  1. Apply the Service definition
kubectl apply -f service.yml -n todo-namespace

  1. Verify that the Service is working
kubectl get svc -n todo-namespace

💼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 (minikube) cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name> command.

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

Steps :

Here's an example of what the YAML file for a ClusterIP Service

apiVersion: v1
kind: Service
metadata:
  name: todo-service-cluster-ip
  labels:
    app: todo-app
spec:
  selector:
    app: todo-app
  ports:
    - name: http
      port: 80
      targetPort: 8000

  1. Apply the Service definition
kubectl apply -f cluster-ip-service.yml -n todo-namespace

  1. Verify that the Service is working
kubectl get svc -n todo-namespace

💼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 (minikube) cluster using the kubectl apply -f load-balancer-service.yml -n <namespace-name> command.

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

Steps :

Here's an example of what the YAML file for a LoadBalancer Service

apiVersion: v1
kind: Service
metadata:
  name: todo-service-load-balancer
  labels:
    app: todo-app
spec:
  type: LoadBalancer
  selector:
    app: todo-app
  ports:
    - name: http
      port: 80
      targetPort: 8000

  1. Apply the Service definition
kubectl apply -f load-balancer-service.yml -n todo-namespace

  1. Verify that the Service is working
kubectl get svc -n todo-namespace

👋 Conclusion :

In this blog, we cover Kubernetes services & add those services to the pods. In the next blog, we will cover some advanced topics.

Thank you for reading!

Contact me on Linkedin 🤝

Check out my GitHub for more resources 📚