Kubernetes Services - NodePort, ClusterIP & LoadBalancer

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
In Kubernetes, services are fundamental in exposing your applications to the network, both internally within the cluster and externally to the outside world. Understanding the different types of Kubernetes services NodePort, ClusterIP, and LoadBalancer is crucial for efficiently managing and scaling your containerized applications. This blog post will explore these service types, their use cases, and how to implement them.
When to Use Which Service?
ClusterIP: Use for internal microservice communication.
NodePort: Use when you need external access without a cloud load balancer.
LoadBalancer: Use in production for managed load balancing and high availability.
1. ClusterIP Service
ClusterIP is the default service type in Kubernetes. It exposes the service on an internal IP address within the cluster, making it accessible only from within the cluster.
Ideal for internal communication between different components of your application, such as between microservices.
Used when external access is not required.
Here’s an example of a YAML configuration to create a ClusterIP service:
apiVersion: v1
kind: Service
metadata:
name: my-clusterip
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
To apply this configuration, use the following command:
kubectl apply -f clusterip.yaml
This command creates a service that targets any Pod labeled with app: myapp and forwards traffic to port 8080 on those Pods.
2. NodePort
NodePort service exposes the service on each node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service routes, is automatically created. You can contact the NodePort service from outside the cluster by requesting <NodeIP>:<NodePort>.
Suitable when you want to expose your application externally without needing a load balancer.
Useful for development and testing purposes.
Below is a sample YAML configuration to create a NodePort service:
apiVersion: v1
kind: Service
metadata:
name: my-nodeport
spec:
type: NodePort
selector:
app: myapp
ports:
- port: 80
targetPort: 8080
nodePort: 30007
Apply this configuration using:
kubectl apply -f nodeport.yaml
This service will be accessible at <NodeIP>:30007.
3. LoadBalancer
LoadBalancer service is an extension of NodePort that automatically provisions a load balancer from the cloud provider managing your Kubernetes cluster. It directs traffic to a NodePort service, which in turn routes traffic to your Pods.
Best for production environments where external traffic needs to be load-balanced across multiple nodes.
Useful for applications requiring high availability.
Here’s an example of a YAML configuration for a LoadBalancer service:
apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Apply this configuration using:
kubectl apply -f loadbalancer.yaml
Once created, the cloud provider will provision a load balancer and assign it an external IP, making the service accessible from the internet.
Kubernetes services are essential for managing network traffic within and outside your cluster. By choosing the right service type ClusterIP, NodePort, or LoadBalancer you can efficiently control how your applications are exposed and accessed, ensuring they are scalable, reliable, and secure.