Understanding Kubernetes Services: ClusterIP, NodePort, LoadBalancer, and ExternalName
Safique
In Kubernetes, Pods are ephemeral, meaning they can be created and destroyed dynamically. To allow stable communication between different components of an application, Kubernetes provides Services.
A Service exposes a set of Pods using a stable IP and DNS name, ensuring reliable networking within and outside the cluster.
Why Do We Need Kubernetes Services?
- Pods have dynamic IP addresses that change when restarted.
- Services provide a fixed virtual IP (VIP) that remains stable.
- Services enable load balancing across multiple pod instances.
Types of Kubernetes Services
Kubernetes provides four main types of services:
Service Type Scope Use Case ClusterIP Internal Default service type, used for inter-pod communication. NodePort External Exposes a service on each node’s IP at a static port. LoadBalancer External Uses cloud provider’s load balancer to expose the service. ExternalName External Maps a Kubernetes service to an external domain name.
ClusterIP Service (Default Type)
ClusterIP is the default Kubernetes service type. It assigns a virtual IP that is accessible only within the cluster.
Use Case:
- Allows communication between pods in the same cluster.
Example: ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: my-clusterip-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80 # Service port
targetPort: 8080 # Pod port
type: ClusterIP
Apply the service:
kubectl apply -f clusterip-service.yaml
Check the service:
kubectl get services
Access it inside the cluster:
curl http://my-clusterip-service:80
NodePort Service
A NodePort service exposes the application on a static port on each worker node’s IP.
Use Case:
- Allows external access to the service using NodeIP:NodePort.
- Useful for development but not ideal for production.
Example: NodePort Service
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007
type: NodePort
Apply the service:
kubectl apply -f nodeport-service.yaml
Access it externally:
curl http://<NODE-IP>:30007
LoadBalancer Service
A LoadBalancer service provisions a cloud provider’s load balancer (AWS, Azure, GCP) to expose the service externally.
Use Case:
- Ideal for production when running on cloud platforms.
- Provides external access with a public IP.
Example: LoadBalancer Service
apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Apply the service:
kubectl apply -f loadbalancer-service.yaml
Check the external IP:
kubectl get services
Access it using the assigned external IP:
curl http://<EXTERNAL-IP>
ExternalName Service
An ExternalName service maps a Kubernetes service to an external domain name instead of routing traffic to pods.
Use Case:
- Useful for proxying traffic to an external service, such as a database or API.
Example: ExternalName Service
apiVersion: v1
kind: Service
metadata:
name: my-externalname-service
spec:
type: ExternalName
externalName: api.example.com
Access it inside the cluster:
curl http://my-externalname-service
This redirects traffic to api.example.com.
Choosing the Right Service Type
Scenario Recommended Service Internal pod-to-pod communication ClusterIP Local development access NodePort Cloud-based external access LoadBalancer Redirect traffic to an external domain ExternalName
Kubernetes Services provide stable networking for applications running inside and outside the cluster. Choosing the right service type depends on your use case, whether it’s internal communication, external access, or load balancing.
Author: Safique