CKAD: Services and Networking
20% of the CKAD exam, 3 competencies. Back to the CKAD overview.
Each competency lists what to know and the official documentation to read, then a demo you can run. Demos run on the demo lab, a throwaway kind cluster on your own machine; set it up once before you start.
Demonstrate basic understanding of NetworkPolicies
Section titled “Demonstrate basic understanding of NetworkPolicies”Know
- Policies select pods and allow ingress or egress by pod selector, namespace selector, or IP block. No policy means allow all.
Read
Demo: Allow traffic from one namespace only
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-netpolkubectl config set-context --current --namespace=ckad-netpol -
A server in this namespace, and clients in two others.
Terminal window kubectl create deployment api --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080kubectl expose deployment api --port=8080kubectl create namespace ckad-frontend; kubectl create namespace ckad-elsewherekubectl rollout status deployment/api -
Allow ingress to
apionly from theckad-frontendnamespace.Terminal window kubectl apply -f - <<'EOF'apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: api-from-frontendspec:podSelector:matchLabels:app: apipolicyTypes: ["Ingress"]ingress:- from:- namespaceSelector:matchLabels:kubernetes.io/metadata.name: ckad-frontendEOFfor ns in ckad-frontend ckad-elsewhere; doecho "from $ns:"; kubectl run c -n $ns --rm -i --restart=Never --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- wget -qO- -T 3 http://api.ckad-netpol:8080/hostname; echodone
What you should see: The call from ckad-frontend returns a pod name; the one from ckad-elsewhere times out. Every namespace carries the kubernetes.io/metadata.name label automatically, which makes it a safe selector.
Clean up:
kubectl delete namespace ckad-frontend ckad-elsewherekubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-netpolPractise
- Allow only pods labelled
access=trueto reach a database pod on port 5432.
On this site
Provide and troubleshoot access to applications via services
Section titled “Provide and troubleshoot access to applications via services”Know
- Selectors must match pod labels and
targetPortmust match the container port. Check EndpointSlices when traffic does not arrive.
Read
Demo: Named ports and a quick local test
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-svcaccesskubectl config set-context --current --namespace=ckad-svcaccess -
Use a named container port so the Service doesn’t hard-code a number.
Terminal window kubectl apply -f - <<'EOF'apiVersion: apps/v1kind: Deploymentmetadata:name: shopspec:replicas: 2selector:matchLabels:app: shoptemplate:metadata:labels:app: shopspec:containers:- name: appimage: registry.k8s.io/e2e-test-images/agnhost:2.66.1args: ["netexec", "--http-port=8080"]ports:- name: httpcontainerPort: 8080---apiVersion: v1kind: Servicemetadata:name: shopspec:selector:app: shopports:- port: 80targetPort: httpEOFkubectl rollout status deployment/shop -
Test from inside, from your laptop, and by DNS name from another namespace.
Terminal window kubectl run c --rm -i --restart=Never --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- wget -qO- -T 3 http://shop/hostname; echokubectl port-forward service/shop 8080:80 & sleep 2; curl -s localhost:8080/hostname; echo; kill %1kubectl run c -n default --rm -i --restart=Never --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- wget -qO- -T 3 http://shop.ckad-svcaccess.svc.cluster.local/hostname; echo
What you should see: All three return a pod name. targetPort: http resolves per pod, so you can change the container port without touching the Service. From another namespace the short name shop won’t resolve; use shop.<namespace> or the full name.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-svcaccessOn this site
Use Ingress rules to expose applications
Section titled “Use Ingress rules to expose applications”Know
- Host and path rules map to Service backends. The
ingressClassNamepicks the controller.
Read
Demo: Exact and prefix paths on one host
-
Create a namespace for this demo and make it the default for your context.
Terminal window kubectl create namespace ckad-ingresskubectl config set-context --current --namespace=ckad-ingress -
Two Services behind one host, split by path type.
Terminal window for v in names echoes; dokubectl create deployment $v --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080kubectl expose deployment $v --port=80 --target-port=8080donekubectl apply -f - <<'EOF'apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: pathsspec:ingressClassName: cloud-provider-kindrules:- host: app.examplehttp:paths:- path: /hostnamepathType: Exactbackend:service:name: namesport:number: 80- path: /echopathType: Prefixbackend:service:name: echoesport:number: 80EOFkubectl get ingress paths -w # wait for an ADDRESS, then Ctrl-C -
Try each rule and a path that matches neither.
Terminal window IP=$(kubectl get ingress paths -o jsonpath='{.status.loadBalancer.ingress[0].ip}')docker exec kind-control-plane curl -s -H 'Host: app.example' http://$IP/hostname; echodocker exec kind-control-plane curl -s -H 'Host: app.example' "http://$IP/echo?msg=hi"; echodocker exec kind-control-plane curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example' http://$IP/hostname/extra
What you should see: /hostname returns a names- pod, /echo?msg=hi returns hi from echoes, and /hostname/extra is a 404 because that rule is Exact. Prefix matches by path element, so /echo also matches /echo/anything but not /echoes.
Clean up:
kubectl config set-context --current --namespace=defaultkubectl delete namespace ckad-ingressPractise
- Create an Ingress with two path rules and a TLS section that references a Secret.
On this site
Domain and competency names are from the CNCF CKAD curriculum (v1.37), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.