Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 8Name in the course: Services and cluster DNS

Reach pods with Services and DNS

Pods come and go, and their IPs change. A Service gives a stable name and IP in front of a group of pods (chosen by labels) and spreads the traffic over them. CoreDNS turns service names into IPs inside the cluster.

  • 5 Lessons
  • 4 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Services, endpoints and CoreDNS

8.1

Know the service types

The type decides who can reach the service.

TypeReachable fromNotes
ClusterIP (default)Inside the clusterA virtual IP
NodePortEvery node's IP on a port 30000–32767For tests or external load balancers
LoadBalancerThe internet / networkAsks the cloud for a load balancer
ExternalNameInside the clusterA DNS alias (CNAME) to an outside name
Headless (clusterIP: None)Inside the clusterDNS returns the pod IPs (StatefulSets)

8.2

Expose a Deployment

kubectl expose creates a Service with the Deployment's labels as selector.

kubectl expose deployment web --port=80 --target-port=8080 -n shop
kubectl expose deployment web --name=web-np --type=NodePort --port=80 --target-port=8080 -n shop
kubectl get svc -n shop
NAME     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
web      ClusterIP   10.96.41.12    <none>        80/TCP         10s
web-np   NodePort    10.96.200.7    <none>        80:31452/TCP   5s
curl http://NODE_IP:31452/
service.yaml
apiVersion: v1
kind: Service
metadata: { name: web, namespace: shop }
spec:
  selector: { app: web }
  ports:
    - name: http
      port: 80
      targetPort: 8080
FieldMeaning
portThe port of the Service
targetPortThe port of the container
nodePortThe port on every node (NodePort type)
selectorWhich pods get the traffic

8.3

Check the endpoints

The Service sends traffic only to ready pods that match the selector. The endpoints list shows them.

kubectl get endpointslices -n shop -l kubernetes.io/service-name=web
kubectl describe svc web -n shop | grep -i endpoints
Endpoints:         10.244.1.15:8080,10.244.2.9:8080

Remember

No endpoints = the selector labels do not match the pod labels, or the pods are not ready. This is the most common Service problem.

8.4

Use cluster DNS

Every Service gets a DNS name: SERVICE.NAMESPACE.svc.cluster.local. Inside the same namespace, the short name SERVICE works.

kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- nslookup web.shop.svc.cluster.local
Name:      web.shop.svc.cluster.local
Address 1: 10.96.41.12
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- http://web.shop
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl get configmap coredns -n kube-system -o yaml
NameResolves to
webThe Service in the same namespace
web.shopThe Service web in namespace shop
web.shop.svc.cluster.localThe full name
10-244-1-15.shop.pod.cluster.localA pod by its IP

8.5

Know how kube-proxy routes

kube-proxy (or an eBPF CNI like Cilium) programs every node so traffic to a Service IP goes to one of its pods.

kubectl get daemonset kube-proxy -n kube-system
kubectl get configmap kube-proxy -n kube-system -o yaml | grep mode
iptables-save | grep web | head

All commands in this chapter