Day -37: Mastering Kubernetes Services: A Deep Dive into ClusterIP and NodePort
Kubernetes is like a busy traffic control system for your applications, ensuring that all your apps are running smoothly, no matter where they're deployed. A critical part of this system is Kubernetes Services. Let's break down what these services are, the different types, and how to use them, in a way that's easy to understand.
Kubernetes Services क्या होते हैं?
Imagine you're running a restaurant, and you've got chefs in the kitchen (your pods) cooking up a storm. The waiters (Kubernetes Services) need to know where to take the food so that it reaches the right customers. Kubernetes Services ensure that the traffic (requests) reaches the right pod, no matter where it's running in the cluster.
Types of Kubernetes Services
ClusterIP:
This is like a private line, जहां सिर्फ़ internal communication होती है। It exposes the service on a cluster-internal IP, which means it's not accessible from outside the cluster.
Example: Your chefs (pods) are only accessible by the restaurant staff (other pods) and not directly by customers (external users).
NodePort:
NodePort allows external traffic to access your application by opening a specific port on all nodes. यह ऐसा है जैसे आपने अपनी restaurant की kitchen को एक बाहर वाली खिड़की दे दी, जिससे कोई भी बाहर से खाना order कर सकता है।
Example: External customers (users) can directly reach out to the kitchen (pods) via a specific port.
LoadBalancer:
LoadBalancer is like having a fancy receptionist who routes customer orders to the right chef. It exposes your service to the internet via a cloud provider's load balancer.
Example: This is like the front door of your restaurant, which directs customers to the right place.
What is ClusterIP?
ClusterIP is the default service type in Kubernetes. ये एक अंदरूनी सेवा है, जो सिर्फ़ आपके क्लस्टर के अंदर काम करती है। किसी भी external source को इसके बारे में पता नहीं चलता है, और ना ही वो इसको access कर सकते हैं।
Create ClusterIP Through Command:
kubectl expose deployment <deployment-name> --type=ClusterIP --name=<service-name>
ClusterIP Manifest File Example:
apiVersion: v1
kind: Service
metadata:
name: my-clusterip-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Check if ClusterIP is Working:
kubectl get svc
kubectl describe svc <service-name>
You can also curl the ClusterIP service from inside the cluster to check connectivity.
What is NodePort?
NodePort is a way to expose your service on a static port on each Node's IP. ये बिल्कुल वैसा है जैसे आपकी restaurant की kitchen में एक खास खिड़की हो, जहां से कोई भी बाहर से आकर order कर सकता है।
Static Ports in Kubernetes for NodePort:
- Kubernetes typically allocates ports between 30000 and 32767 for NodePort services.
Create NodePort Service Through Command:
kubectl expose deployment <deployment-name> --type=NodePort --name=<service-name>
NodePort Manifest File Example:
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
nodePort: 30007
Check if NodePort is Working:
kubectl get svc
kubectl describe svc <service-name>
curl <NodeIP>:<NodePort>
You can access the service from a browser using http://<NodeIP>:<NodePort>
.
Endpoints and Accessing from the Internet
Endpoints in Kubernetes are like the delivery addresses of your application. They point to the pods that the service routes traffic to.
To access a NodePort service from the internet, you can use your browser by entering the NodeIP
and NodePort
. For example, http://192.168.99.100:30007
.
Conclusion
Kubernetes Services are essential for managing traffic within your application, whether it's internal (ClusterIP) or external (NodePort). ClusterIP is perfect for internal communication, while NodePort allows external access. समझने में आसान है, और अगर आप Kubernetes में नए हैं, तो इन services को अच्छे से समझकर ही आगे बढ़ें। ये बिलकुल उस restaurant के waiters की तरह हैं, जो ये तय करते हैं कि खाना किसके पास जाएगा।
Connect and Follow Me on Socials
Subscribe to my newsletter
Read articles from priyadarshi ranjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
priyadarshi ranjan
priyadarshi ranjan
Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!