In the vast ecosystem of Kubernetes, Services play a crucial role in enabling communication between different components of your application, both internally and externally. In this comprehensive guide, we will delve into the essence of Services in Kubernetes, explore their types, and walk through practical examples to understand their usage.
What are Services in Kubernetes?
Services function as an abstraction layer in Kubernetes, exposing a collection of Pods as a network service. They remove the complexity of networking and dynamic IP addresses to allow smooth communication between different parts of your program.
Types of Services:
ClusterIP Service(default): This type of Service exposes the set of Pods internally within the cluster, making them accessible only from within the cluster.
NodePort Service: NodePort Service exposes the Service on a static port on each node of the cluster. It enables external access to the Service by mapping a port on the node's IP address to a port on the Service.
LoadBalancer Service: LoadBalancer Service automatically provisions an external load balancer to expose the Service externally. It distributes incoming traffic across the Pods in the Service.
ExternalName: Internal clients use the DNS name of a Service as an alias for an external DNS name.
Headless: You can use a headless service when you want a Pod grouping, but don’t need a stable IP address
Task-1: Creating a Service for your nginx app Deployment
Step 1: Create a Service for your nginx app Deployment
To create a Service for the nginx app Deployment, we'll start by defining a Service definition in a YAML file.
Step 2: Create a Service definition for your nginx app Deployment in a YAML file
Step 3: Apply the Service definition to your K8s (minikube) cluster
Apply the Service definition using the following command:
kubectl apply -f service.yml -n <namespace-name>
Step 4: Verify that the Service is working
Access the nginx app using the Service's IP and Port in your Namespace.
Now, curl the above IP address of the kubernetes service to access the app.
curl -L <service-ip>:<service-port>
Task-2: Creating a ClusterIP Service
Step 1: Create a ClusterIP Service for accessing the nginx app from within the cluster
We'll create a ClusterIP Service definition in a YAML file.
Step 2: Create a ClusterIP Service definition for your nginx app Deployment in a YAML file
Step 3: Apply the ClusterIP Service definition to your K8s (minikube) cluster
Apply the ClusterIP Service definition using the command:
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
Step 4: Verify that the ClusterIP Service is working
Access the nginx app from another Pod in the cluster in your Namespace.
Task-3: Creating a LoadBalancer Service
Step 1: Create a LoadBalancer Service for accessing the nginx app from outside the cluster
We'll create a LoadBalancer Service definition in a YAML file.
Step 2: Create a LoadBalancer Service definition for your nginx app Deployment in a YAML file
Step 3: Apply the LoadBalancer Service definition to your K8s (minikube) cluster
Apply the LoadBalancer Service definition using the command:
kubectl apply -f load-balancer-service.yml -n <namespace-name>
Step 4: Verify that the LoadBalancer Service is working
Access the nginx app from outside the cluster in your Namespace.
You can also get the LoadBalancer URL using :
minikube service list
By following these steps, you'll gain a deeper understanding of Services in Kubernetes and their various types, empowering you to effectively manage communication within your Kubernetes cluster.
Follow for more:
Linkedin: https://www.linkedin.com/in/samarjeet-patil-921952251/
#cloud #AWS #k8s #deployment #pods #yaml #sevice #networking #services #loadbalancer