Skip to content
Hands-on IT IT Drills

Day 7: Services and CoreDNS

Goal: You expose apps through all Service types – including a real LoadBalancer on VMs – and you know how name resolution works inside the cluster. · Duration: approx. 60 min · Prerequisite: Day 4

Briefing

CKA: Services and Networking (20 %) – “Use ClusterIP, NodePort, LoadBalancer service types and endpoints”, “Understand and use CoreDNS”.

  outside --> LoadBalancer IP (MetalLB) --+
  outside --> NodeIP:NodePort ------------+--> ClusterIP --> EndpointSlice --> Pods
  Pod     --> echo.tag07 (CoreDNS) -------+

Each type builds on the previous one: a LoadBalancer also has a NodePort, and a NodePort also has a ClusterIP.

mkdir -p ~/cka/tag07 && cd ~/cka/tag07
k create ns tag07
k config set-context --current --namespace=tag07

Drill 1: An App That Reveals Its Name

agnhost is the Kubernetes developers’ test tool; /hostname returns the Pod name.

k create deployment echo --image=registry.k8s.io/e2e-test-images/agnhost:2.53 \
  --replicas=2 -- /agnhost netexec --http-port=8080
k rollout status deployment echo
k expose deployment echo --port=80 --target-port=8080
k get svc echo

Expected:

service/echo exposed
NAME   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
echo   ClusterIP   10.97.170.32   <none>        80/TCP    1s

--port is the port of the Service, --target-port the port in the container.

k run c --image=busybox:1.37 --rm -it --restart=Never -- \
  sh -c 'wget -qO- echo/hostname; echo; wget -qO- echo/hostname; echo'

Expected (random order):

echo-57489dc7f5-fb5kn
echo-57489dc7f5-6g2rl

Save your progress? Sign up for free

Drill 2: EndpointSlices

Behind every Service with a selector, Kubernetes maintains the list of Pod IPs:

k get endpointslices -l kubernetes.io/service-name=echo

Expected:

NAME         ADDRESSTYPE   PORTS   ENDPOINTS                         AGE
echo-dgrlz   IPv4          8080    192.168.189.119,192.168.235.160   0s

Scale – the list grows along with it:

k scale deployment echo --replicas=3
J='{range .items[*].endpoints[*]}{.addresses[0]} {.nodeName}{"\n"}{end}'
k get endpointslices -l kubernetes.io/service-name=echo -o jsonpath="$J"

Expected:

192.168.189.119 worker2
192.168.235.160 worker1
192.168.189.66 worker2

A Service without endpoints is the most common network failure – usually the selector does not match the Pod labels (Day 15).

Save your progress? Sign up for free

Drill 3: NodePort

k expose deployment echo --name=echo-np --type=NodePort --port=80 --target-port=8080
k get svc echo-np

Expected:

NAME      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
echo-np   NodePort   10.105.132.187   <none>        80:31482/TCP   0s

The NodePort (here 31482, from 30000–32767) is open on every node, including cp1:

NP=$(k get svc echo-np -o jsonpath='{.spec.ports[0].nodePort}')
curl -s 172.16.0.228:$NP/hostname; echo
curl -s 172.16.0.126:$NP/hostname; echo

Expected:

echo-57489dc7f5-6g2rl
echo-57489dc7f5-fb5kn

If nothing comes back right after creating it, wait a few seconds – kube-proxy needs a moment for the rules.

Save your progress? Sign up for free

Drill 4: MetalLB – LoadBalancer Without a Cloud

In the cloud, the provider supplies the external IP. On VMs, MetalLB takes over: it assigns IPs from a pool and answers ARP requests for them.

ML=https://raw.githubusercontent.com/metallb/metallb/v0.16.2/config/manifests
k apply -f $ML/metallb-native.yaml
k wait -n metallb-system --for=condition=Ready pod --all --timeout=180s

Expected (abridged):

pod/controller-6b66bcf7d6-xmkhv condition met
pod/speaker-5d6vl condition met
pod/speaker-6ndkl condition met
pod/speaker-xqqpf condition met

Pick the pool from free addresses in your VM network – here 172.16.0.242–244:

cat <<'EOF' | k apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: lab
  namespace: metallb-system
spec:
  addresses:
  - 172.16.0.242-172.16.0.244
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: lab
  namespace: metallb-system
spec:
  ipAddressPools:
  - lab
EOF

Expected:

ipaddresspool.metallb.io/lab created
l2advertisement.metallb.io/lab created

Warning: The addresses must neither be in use nor lie within your router’s DHCP range. Check with ping first.

Save your progress? Sign up for free

Drill 5: Service of Type LoadBalancer

k expose deployment echo --name=echo-lb --type=LoadBalancer --port=80 --target-port=8080
k get svc echo-lb

Expected:

NAME      TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
echo-lb   LoadBalancer   10.101.11.148   172.16.0.242   80:31278/TCP   5s

From your laptop, outside the cluster:

curl -s 172.16.0.242/hostname; echo

Expected: a Pod name such as echo-57489dc7f5-fb5kn. Without MetalLB, EXTERNAL-IP would stay <pending> forever – a typical exam scenario.

Save your progress? Sign up for free

Drill 6: How a Pod Resolves Names

k get svc kube-dns -n kube-system
k run d --image=busybox:1.37 --rm -it --restart=Never -- cat /etc/resolv.conf

Expected:

NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   47m
search tag07.svc.cluster.local svc.cluster.local cluster.local localdomain
nameserver 10.96.0.10
options ndots:5

For historical reasons, the Service is called kube-dns; CoreDNS runs behind it. The search list explains why echo is enough within the same namespace and echo.tag07 works from anywhere. The configuration lives in a ConfigMap:

k get cm coredns -n kube-system -o jsonpath='{.data.Corefile}'

Expected (abridged):

.:53 {
    errors
    health {
       lameduck 5s
    }
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
       pods insecure
       fallthrough in-addr.arpa ip6.arpa
       ttl 30
    }
    forward . /etc/resolv.conf {
       max_concurrent 1000
    }
    ...
}

kubernetes answers cluster names, forward sends everything else to the node’s DNS.

Save your progress? Sign up for free

Drill 7: DNS Names for Services and Pods

From another namespace:

k run dn -n default --image=busybox:1.37 --rm -it --restart=Never -- sh -c \
  'nslookup echo.tag07.svc.cluster.local
   nslookup 192-168-235-160.tag07.pod.cluster.local'

Expected (abridged):

Name:   echo.tag07.svc.cluster.local
Address: 10.97.170.32
Name:   192-168-235-160.tag07.pod.cluster.local
Address: 192.168.235.160

Services: <service>.<namespace>.svc.cluster.local. Pods: the IP with dashes instead of dots, then <namespace>.pod.cluster.local.

Tip: busybox’s nslookup often ignores the search list. When in doubt, use the full name (FQDN) or test with wget -qO- echo.tag07/….

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Create a NodePort Service fixed on the fixed port 30080 that points to port 8080 of the echo Pods. Target time: 60 s
  2. Change the Service echo to type NodePort. Target time: 20 s
  3. Output only the external IP of echo-lb. Target time: 20 s

Solution

k create service nodeport fixed --tcp=80:8080 --node-port=30080
k set selector svc fixed app=echo
curl -s 172.16.0.83:30080/hostname; echo
k patch svc echo -p '{"spec":{"type":"NodePort"}}'
k get svc echo-lb -o jsonpath='{.status.loadBalancer.ingress[0].ip}{"\n"}'

Expected (excerpt):

service/fixed created
service/fixed selector updated
echo-57489dc7f5-fb5kn
service/echo patched
172.16.0.242

k create service sets the selector app=fixed – hence the set selector.

Save your progress? Sign up for free

Checkpoint

k get svc -o custom-columns=NAME:.metadata.name,TYPE:.spec.type

Expected:

NAME      TYPE
echo      NodePort
echo-lb   LoadBalancer
echo-np   NodePort
fixed     NodePort
k get endpointslices -l kubernetes.io/service-name=fixed

Expected: three addresses under ENDPOINTS.

k get pods -n metallb-system --no-headers | wc -l

Expected: 4 (one controller, three speakers).

Challenge

Pods should be able to resolve the name cp1.lab – it should point to 172.16.0.126.

Hint: CoreDNS has a hosts plugin; after a change to the ConfigMap, CoreDNS reloads it by itself thanks to reload (up to one minute).

Solution

Back up first, then edit:

k get cm coredns -n kube-system -o yaml | grep -v resourceVersion: \
  > ~/cka/coredns-backup.yaml
k edit cm coredns -n kube-system

Insert below the line ready:

        hosts {
           172.16.0.126 cp1.lab
           fallthrough
        }

After about a minute:

k run h -n default --image=busybox:1.37 --rm -it --restart=Never -- nslookup cp1.lab

Expected (abridged):

Name:   cp1.lab
Address: 172.16.0.126

fallthrough passes all other names on to the next plugins – without it, every other name would suddenly be unknown. The grep -v resourceVersion: during the backup ensures that the backup can be applied again later; otherwise the API rejects it as outdated (the object has been modified).

Clean up (MetalLB stays for Day 8):

k apply -f ~/cka/coredns-backup.yaml
k config set-context --current --namespace=default
k delete ns tag07

Save your progress? Sign up for free

Exam Tip

  • “Make the Service reachable” almost always means: check the selector and targetPort. k get endpointslices shows at once whether there are Pods behind it.
  • Back up the CoreDNS ConfigMap before every change. A typo takes down name resolution for the whole cluster.
  • EXTERNAL-IP <pending> means: there is no LoadBalancer controller.

Enterprise Note

  • MetalLB in L2 mode pins each IP to one node; in larger networks, use BGP mode with the router (or Cilium/Calico BGP).
  • Usually block NodePorts from the outside – access goes through Ingress or Gateway (Day 8).
  • With many DNS queries, deploy NodeLocal DNSCache and lower ndots for apps.