In this guide, we’ll explore how to implement ClusterIP and Headless Services in Google Kubernetes Engine. You’ll learn the difference between them and how Headless Services allow direct DNS resolution to Pod IPs.
📌 Step-01: Introduction
- Implement Kubernetes ClusterIP and Headless Service
- Understand how Headless Services resolve directly to Pod IPs
📌 Step-02: Kubernetes Deployment
01-kubernetes-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp1-deployment
spec:
replicas: 4
selector:
matchLabels:
app: myapp1
template:
metadata:
name: myapp1-pod
labels:
app: myapp1
spec:
containers:
- name: myapp1-container
image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0
ports:
- containerPort: 8080
📌 Step-03: ClusterIP Service
02-kubernetes-clusterip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp1-cip-service
spec:
type: ClusterIP
selector:
app: myapp1
ports:
- name: http
port: 80
targetPort: 8080
📌 Step-04: Headless Service
The key difference is:
👉 clusterIP: None
03-kubernetes-headless-service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp1-headless-service
spec:
clusterIP: None
selector:
app: myapp1
ports:
- name: http
port: 80
targetPort: 8080
🔑 Important Note
- Headless Service does not get a Cluster IP.
- DNS resolves directly to Pod IPs.
- You must use the container port (8080 here).
📌 Step-05: Deploy Manifests
# Deploy manifests
kubectl apply -f headless/
# Verify deployments
kubectl get deploy
kubectl get pods -o wide
# Check services
kubectl get svc
✅ You’ll notice:
- ClusterIP Service → has a ClusterIP.
- Headless Service → CLUSTER-IP is None.
📌 Step-06: Curl Pod Manifest
To test DNS resolution, create a Curl Pod:
apiVersion: v1
kind: Pod
metadata:
name: curl-pod
spec:
containers:
- name: curl
image: curlimages/curl
command: [ "sleep", "600" ]
📌 Step-07: Verify ClusterIP vs Headless Service
kubectl apply -f headless/curl.yaml
kubectl exec -it curl-pod -- sh
🔹 Test ClusterIP Service
nslookup myapp1-cip-service.default.svc.cluster.local
curl myapp1-cip-service.default.svc.cluster.local
👉 Returns the ClusterIP.
🔹 Test Headless Service
nslookup myapp1-headless-service.default.svc.cluster.local
curl myapp1-headless-service.default.svc.cluster.local:8080
👉 Returns multiple Pod IPs.
🎯 Now, traffic is directly resolved to Pod IPs instead of a Service IP.
📌 Step-08: Clean-Up
kubectl delete -f headless/
✅ Summary
- ClusterIP Service → provides a stable IP inside the cluster.
- Headless Service → bypasses ClusterIP, resolving directly to Pod IPs.
- Useful in stateful applications, DNS-based service discovery, and databases (e.g., Cassandra, StatefulSets).
🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.
— Latchu | Senior DevOps & Cloud Engineer
☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions
Top comments (0)