Introduction
In this comprehensive guide, we delve into the world of Kubernetes Services, crucial components for managing network applications within a cluster. Services act as intelligent connectors, ensuring seamless communication between pods, regardless of how many come and go. Learn about the various types of services and their specific use cases, providing you with a clear understanding of how they optimize application functionality.
Understanding Services in Kubernetes
In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.
Kubernetes Service is like a traffic manager for a group of related programs, called Pods*. It makes sure that no matter how many Pods come and go, you can always find and talk to them in the same way. This helps different parts of an application work together smoothly. Think of it as an intelligent connector that links everything behind the scenes. Plus, extra tools like Ingress and the Gateway API help manage how traffic flows into your applications.*
For additional references, explore this link: < LINK >
Type of Services in Kubernetes
Service Types in Kubernetes refer to the different ways in which services can be made accessible to applications and users.
There are four main types:
ClusterIP: This service type is accessible only within the cluster, providing a reliable way for pods to communicate with each other.
NodePort: It opens a specific port on all nodes in the cluster, making the service accessible externally, but it's less efficient for high traffic.
LoadBalancer: This type provisions a load balancer externally (like in a cloud environment) and automatically configures it to route traffic to your service, ensuring even distribution.
ExternalName: It provides an alias for an external service, allowing pods to refer to it by a stable name, regardless of its actual address.
Task 1: Creating a Service for todo-app Deployment in Kubernetes
Create a Service for your todo-app Deployment from previous blog
Go through to my previous blog
Create a Service definition for your todo-app Deployment in a YAML file.
apiVersion: v1 kind: Service metadata: name: node-todo-service namespace: node-todo spec: selector: app: todo type: NodePort ports: - protocol: TCP port: 8000 targetPort: 8000 nodePort: 30009
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.
In Minikube: How to verify this
kubectl port-forward service/<service_name> <local_port>:<target_port> -n <namespace>
kubectl port-forward service/node-todo-service 30009:8000 -n node-todo
Simply past to the IP in the web-browser that is shown in the first line
Task 2: Creating ClusterIP Service for todo-app Access in Kubernetes
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.
When you create a service in Kubernetes without specifying a type, it automatically becomes a
ClusterIP
service.apiVersion: v1 kind: Service metadata: name: todo-service-clusterip namespace: node-todo spec: selector: app: todo ports: - protocol: TCP port: 80 targetPort: 8000
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.
Step 1:Check the Service
kubectl get svc -n <namespace> kubectl get pods -n <namespace>
Step 2: Create the Test Pod
apiVersion: v1 kind: Pod metadata: name: podtest namespace: node-todo spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'while true; do wget -q -O- todo-service-clusterip:8000; done'] command: ['sh', '-c', 'while true; do echo "hello k8s"; wget -q -O- todo-service-clusterip:8000; done']
This YAML file defines a pod named
test-pod
in thenode-todo
namespace. It runs abusybox
container that continuously sends HTTP requests to todo-service-clusterip:8000
.Step 3: Apply the Test Pod
kubectl apply -f <file_name.yml> -n <namespace>
This command applies the
test-pod.yml
file to thenode-todo
namespace, creating thepodtest
pod.Step 4: Connect to the Test Pod
kubectl exec -it <pod_name> -n <namespace> -- /bin/sh # -- : This is used to separate the kubectl exec options from the command that will be executed in the container.
This command connects to the
test-pod
in thenode-todo
namespace.Step 5: Check Application Connectivity
How to get the cluster-IP
-------> TWO COMMANDS <-------
#First commnad kubectl get all -n <namespace>
#Second command kubectl get service -n <namespace>
wget -qO- http:<clusterIP>:<Port>
This command tests the application connectivity from within the pod, using the ClusterIP and port.
If you want to retrieve logs from a pod, use below command
kubectl logs <pod_name> -n <namespace>
Output: That we added in the file Test Pod
Task 3: Creating LoadBalancer Service for todo-app Access in Kubernetes
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.
apiVersion: v1 kind: Service metadata: name: todo-app-loadbalancer spec: selector: app: todo ports: - protocol: TCP port: 80 targetPort: 8000 type: LoadBalancer
Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the
kubectl apply -f load-balancer-service.yml -n <namespace-name>
command.kubectl apply -f <service_name> -n <namespace-name>
Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
kubectl get svc -n <namespace-name>
Conclusion
Kubernetes Services are crucial for cluster communication. From ClusterIP for internal traffic to NodePort for external access, and LoadBalancer for wider network accessibility, mastering these services is vital. This knowledge ensures your applications thrive in dynamic environments.
Let's Connect ❤️:
| ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|
------- Let's connect! -------
|_____________|
............... \ (•◡•) / ...................
................. \ ...... / .....................
.................. ——- .......................
.................. | ... | ......................
.................. |_ . |_ ....................
Thank you for exploring my blog!