ClusterIP
- Docker & Kubernetes course
- Intermediate
- Lesson 38/82
- 4 min
Learn the ClusterIP service type.
ClusterIP
ClusterIP is the default Service type: it gives a virtual IP reachable only from inside the cluster. It's used for internal communication between applications (API → database). In the YAML, port is what the service listens on, and targetPort is the container port on the pod:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080
Service DNS names
The cluster DNS (CoreDNS) creates a record for each service in the form name.namespace.svc.cluster.local. From the same namespace the short name is enough, from another namespace name.namespace:
$ kubectl run -it --rm dns-test --image=busybox:1.36 --restart=Never -- nslookup web
Name: web.default.svc.cluster.local
Address: 10.96.45.12
# from another namespace: http://web.defaultA headless service, written with clusterIP: None, gets no virtual IP; DNS returns the pod IPs directly (used with StatefulSets).
Practice A ClusterIP service
Write a Service YAML: name web; selector app: web; a single port: port: 80, targetPort: 8080. (If no type is written, it is ClusterIP.)
Starter code
# buraya yazSample solution
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080Expected output
service/web created
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is a request sent to this service from another pod?
- http://web:80 (or http://web) Correct answer
- http://web:8080
- http://localhost:80
- With the host's IP
Explanation: The client uses the service port (80); the service forwards it to the pod's 8080.
2 Specify the container port on the pod.
ports:
- port: 80
____: 8080
ports:
- port: 80
targetPort: 8080
Explanation: targetPort is the pod port traffic is forwarded to.
3 Match.
| Term | Meaning |
|---|---|
ClusterIP |
Cluster-internal only |
port |
The service port |
targetPort |
The pod port |
clusterIP: None |
Headless service |