Working with Services in Kubernetes

Working with Services in Kubernetes

Different types of Services in K8s

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 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.

👉Explanation for service.yaml file.

1. API version -In our case v1, so specified in this section.

2. The kind- of resource you are creating. in this case a service is specified in this section.

3. Metadata - We would give a name for service and mention namespace details as well. In our case, it is todo-service.

4. Spec-Selector- This section defines the selector for the pods that the Service should target. In this case, it will target pods with the app label set to todo-app-label.

5. Type - This section specifies the type of service you want to create, in our case its, NodePort.

6. The port - mapping for the Service is described in this section.

To redirect traffic to the target port 8000 on the pods, the Service will listen on port 8000. A NodePort is also defined as 30007, making it possible to contact the Service from outside the cluster by using the IP addresses of any cluster nodes and the nodePort specified in the Service.

Let's deploy the service using service.yaml file using the below apply command.

We can see our service get created in todo-app namespace. Now if check worker node Public-IP:30007(nodePort) we can able to access our application thorugh browser as well (Our node IP addres:30007).

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.

🔦 For cluster -ip we have to create cluster-ip-service.yaml file as below.

To access the to-do app from another Pod in the cluster in your Namespace (todo-app) to ensure that the ClusterIP Service is operational.

Get the ClusterIP Service's IP address using kubectl get svc -n <namespace name>

Now check all available pods in the namespace and try to connect the cluster from inside the pod.

In our case we tried to connect clusterIP from nginx-pod to go inside this pod will execute the below command.

kubectl exec -it nginx-pod -n todo-app bash with this command we can enter into in the Pod.

Now inside the nginx pod will try to execute curl command for clusterIP.

The result will only manifest if two different pods within the same namespace can communicate. Let's explore and see what unfolds....

Once we enter into in the pod we have updated the packages and install curl utility to check the connectivity.

to check ip address of the pods within its pods:

We could see that from nginx pod (different deployment pod) we were successfully able to communicate with our todo-app.

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.

Solution

Now we create another load-balancer.yaml file as below.

Apply the changes with below command and verify the service.

Let's conduct a load balancer test to facilitate communication between two distinct pods residing in separate namespaces.

We've got two namespaces, 'test-namespace' and 'todo-app,' each hosting its own set of pods.

Let's establish a connection to the 'my-web-app' pod in the 'test-namespace' and attempt to execute a Curl command for the 'todo-app' load balancer (located in the 'todo-app' namespace).

We've effectively implemented service deployment on the K8s cluster using NodePort, enabling communication between pods within our namespace and across different namespaces.

Thank You !!

Happy Learning !!