Summary - Service
- Docker & Kubernetes course
- Intermediate
- Lesson 42/82
- 5 min
Combine ClusterIP, NodePort and selector to write a Service.
Summary: Services
In this group:
- A Service gives label-selected pods a stable IP and DNS name; it spreads traffic over the ready pods.
portis the service's port,targetPortthe pod's; DNS:name.namespace.svc.cluster.local.- Types:
ClusterIP(internal),NodePort(node port),LoadBalancer(cloud external IP),ExternalName(DNS alias). - Selector labels combine with AND; blue-green switching can be done by changing labels.
kubectl exposecreates one quickly; if the EndpointSlice is empty, the selector doesn't match.
kubectl expose deploy web --port=80 --target-port=8080 --type=NodePort
kubectl get svc,endpointslicesPractice Inspect the service
Write three commands: expose the web deployment with --port=80 --target-port=8080 --type=NodePort; show the web service in detail; list the pods with their labels.
Starter code
# buraya yazSample solution
kubectl expose deployment web --port=80 --target-port=8080 --type=NodePort
kubectl describe svc web
kubectl get pods --show-labelsExpected output
service/web exposed Name: web Type: NodePort Port: <unset> 80/TCP TargetPort: 8080/TCP NodePort: <unset> 31512/TCP Endpoints: 10.244.1.5:8080,10.244.2.7:8080 NAME READY STATUS LABELS web-7c5b9f6d8-abcde 1/1 Running app=web,pod-template-hash=7c5b9f6d8
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 You created a service but requests get no answer and the endpoint list is empty. What is the most likely cause?
- The selector doesn't match the pod labels Correct answer
- Port 80 is forbidden
- DNS is off
- The service type is ClusterIP
Explanation: Compare the labels with kubectl get pods --show-labels.
2 Create the service with the NodePort type.
kubectl expose deploy web --port=80 --target-port=8080 ____=NodePort
kubectl expose deploy web --port=80 --target-port=8080 --type=NodePort
Explanation: --type selects the service type.
3 Match the types.
| Term | Meaning |
|---|---|
ClusterIP |
Internal communication |
NodePort |
Node IP:port |
LoadBalancer |
External IP |
ExternalName |
CNAME |