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.
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.
Solution: let's create a service.YAML in todo-app folder.
- 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 code.
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.
Solution
for this task will create cluster-ip-service.yaml file as below.
Apply the changes with below command and verify the service.
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 nginx pod will execute the below command.
kubectl exec -it <diffrent pod's name outr case its nginx> -n <namespace name> -- bash
Now inside the nginx pod we 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....
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.
create loadbalancer service using the below command
kubectl apply -f <loadalancer.yaml filename>
Verify the load balancer service details.
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 'httpd' pod in the 'test-namespace' and attempt to execute a Curl command for the 'todo-app' load balancer (located in the 'todo-app' namespace).
Now inside pod we give curl command to check communication.
We've effectively implemented service deployment on the K8s cluster using NodePort, enabling communication between pods within our namespace and across different namespaces.
I firmly believe this article will prove valuable, aiding you in uncovering fresh perspectives and acquiring enriching knowledge for Kubernetes services.
Thanks,
Kishor Chavan
.