What is a Service?
- Docker & Kubernetes course
- Intermediate
- Lesson 37/82
- 4 min
Learn that a Service provides network access to Pods.
What is a Service?
Pods come and go and each gets a new IP. A Service offers a stable virtual IP and DNS name in front of a group of pods selected by label; it spreads incoming traffic over the ready pods:
web (Service, 10.96.45.12:80)
├─► 10.244.1.5:8080 (web-…-abcde)
├─► 10.244.2.7:8080 (web-…-fghij)
└─► 10.244.1.9:8080 (web-…-klmno)Another pod in the same namespace reaches this service at http://web.
expose and Endpoints
The fastest way to create a Service for a deployment is kubectl expose. The real pod addresses behind the Service are kept in EndpointSlice objects; if it comes out empty, the selector matches no pods:
$ kubectl expose deployment web --port=80 --target-port=8080
service/web exposed
$ kubectl get endpointslices -l kubernetes.io/service-name=web
NAME ADDRESSTYPE PORTS ENDPOINTS
web-x7k2p IPv4 8080 10.244.1.5,10.244.2.7,10.244.1.9Practice Expose and verify the service
Write three commands: expose the web deployment with --port=80 --target-port=8080; list the services; list the EndpointSlices of the web service with -l kubernetes.io/service-name=web.
Starter code
# buraya yazSample solution
kubectl expose deployment web --port=80 --target-port=8080
kubectl get svc
kubectl get endpointslices -l kubernetes.io/service-name=webExpected output
service/web exposed NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d web ClusterIP 10.96.45.12 <none> 80/TCP 2s NAME ADDRESSTYPE PORTS ENDPOINTS web-x7k2p IPv4 8080 10.244.1.5,10.244.2.7
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which pods does a Service send traffic to?
- The Deployment's first pod
- Pods matching the selector and Ready Correct answer
- All pods on the same node
- Pods with the same name
Explanation: A pod failing its readinessProbe is removed from the endpoint list.
2 Create a Service for the deployment.
kubectl ____ deployment web --port=80 --target-port=8080
kubectl expose deployment web --port=80 --target-port=8080
Explanation: expose uses the deployment's selector.
3 Match.
| Term | Meaning |
|---|---|
Service |
Stable address |
selector |
Target pods |
EndpointSlice |
Real pod addresses |
http://web |
DNS name |