Skip to content

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

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-netpol
    kubectl config set-context --current --namespace=ckad-netpol
  2. 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=8080
    kubectl expose deployment api --port=8080
    kubectl create namespace ckad-frontend; kubectl create namespace ckad-elsewhere
    kubectl rollout status deployment/api
  3. Allow ingress to api only from the ckad-frontend namespace.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
    name: api-from-frontend
    spec:
    podSelector:
    matchLabels:
    app: api
    policyTypes: ["Ingress"]
    ingress:
    - from:
    - namespaceSelector:
    matchLabels:
    kubernetes.io/metadata.name: ckad-frontend
    EOF
    for ns in ckad-frontend ckad-elsewhere; do
    echo "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; echo
    done

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:

Terminal window
kubectl delete namespace ckad-frontend ckad-elsewhere
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-netpol

Practise

  • Allow only pods labelled access=true to 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 targetPort must match the container port. Check EndpointSlices when traffic does not arrive.

Read

Demo: Named ports and a quick local test

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-svcaccess
    kubectl config set-context --current --namespace=ckad-svcaccess
  2. Use a named container port so the Service doesn’t hard-code a number.

    Terminal window
    kubectl apply -f - <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: shop
    spec:
    replicas: 2
    selector:
    matchLabels:
    app: shop
    template:
    metadata:
    labels:
    app: shop
    spec:
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/agnhost:2.66.1
    args: ["netexec", "--http-port=8080"]
    ports:
    - name: http
    containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: shop
    spec:
    selector:
    app: shop
    ports:
    - port: 80
    targetPort: http
    EOF
    kubectl rollout status deployment/shop
  3. 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; echo
    kubectl port-forward service/shop 8080:80 & sleep 2; curl -s localhost:8080/hostname; echo; kill %1
    kubectl 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-svcaccess

On this site

Know

  • Host and path rules map to Service backends. The ingressClassName picks the controller.

Read

Demo: Exact and prefix paths on one host

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace ckad-ingress
    kubectl config set-context --current --namespace=ckad-ingress
  2. Two Services behind one host, split by path type.

    Terminal window
    for v in names echoes; do
    kubectl create deployment $v --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- /agnhost netexec --http-port=8080
    kubectl expose deployment $v --port=80 --target-port=8080
    done
    kubectl apply -f - <<'EOF'
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: paths
    spec:
    ingressClassName: cloud-provider-kind
    rules:
    - host: app.example
    http:
    paths:
    - path: /hostname
    pathType: Exact
    backend:
    service:
    name: names
    port:
    number: 80
    - path: /echo
    pathType: Prefix
    backend:
    service:
    name: echoes
    port:
    number: 80
    EOF
    kubectl get ingress paths -w # wait for an ADDRESS, then Ctrl-C
  3. 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; echo
    docker exec kind-control-plane curl -s -H 'Host: app.example' "http://$IP/echo?msg=hi"; echo
    docker 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:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace ckad-ingress

Practise

  • 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.