Skip to content

KCNA: Kubernetes Fundamentals

44% of the KCNA exam, 4 competencies. Back to the KCNA 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.

Know

  • A cluster is a control plane (API server, etcd, scheduler, controller manager) plus worker nodes running the kubelet, a container runtime, and usually kube-proxy.
  • Everything is an API object with spec (what you want) and status (what is). Controllers loop to make status match spec.
  • Pods are the smallest unit you deploy. You rarely create them directly; Deployments, StatefulSets, DaemonSets and Jobs create them for you.
  • Namespaces scope names and policies. Labels and selectors are how objects find each other.

Read

Demo: Watch a controller put things back

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

    Terminal window
    kubectl create namespace kcna-core
    kubectl config set-context --current --namespace=kcna-core
  2. Look at the control plane. On a kubeadm-style cluster like kind, its components run as pods.

    Terminal window
    kubectl get pods -n kube-system -o wide
    kubectl get nodes -o wide
  3. Create a Deployment with two replicas, then delete one of its pods.

    Terminal window
    kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --replicas=2 -- /agnhost netexec --http-port=8080
    kubectl rollout status deployment/web
    kubectl delete $(kubectl get pods -l app=web -o name | head -1) --wait=false
    kubectl get pods -l app=web -w
  4. Compare what you asked for with what exists.

    Terminal window
    kubectl get deployment web -o jsonpath='{.spec.replicas} wanted, {.status.readyReplicas} ready{"\n"}'
    kubectl get replicaset -l app=web

What you should see: A new pod appears within seconds with a different name. Nobody ran a command to create it: the ReplicaSet controller saw fewer pods than spec.replicas and made one. That loop of comparing spec with status is how all of Kubernetes works.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcna-core

Practise

  • Create a kind cluster and run kubectl get pods -n kube-system to see the control plane components as pods.
  • Run kubectl explain deployment.spec and read the fields.

On this site

Know

  • kubectl talks to the API server using a kubeconfig: clusters, users, and contexts.
  • Imperative commands (kubectl create, run, expose) are for quick work; declarative YAML with kubectl apply is how teams manage real systems.
  • Access is authenticated, then authorized (usually RBAC), then passed through admission control.

Read

Demo: Find out who you are and what you can do

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

    Terminal window
    kubectl create namespace kcna-admin
    kubectl config set-context --current --namespace=kcna-admin
  2. Read your kubeconfig: which cluster, which user, which context.

    Terminal window
    kubectl config get-contexts
    kubectl config view --minify
  3. Ask the API server who it thinks you are, then what you may do.

    Terminal window
    kubectl auth whoami
    kubectl auth can-i create deployments
    kubectl auth can-i '*' '*' --all-namespaces
  4. Do the same thing imperatively and declaratively, and compare.

    Terminal window
    kubectl create configmap imperative --from-literal=mode=quick
    kubectl create configmap declarative --from-literal=mode=reviewed --dry-run=client -o yaml > cm.yaml
    kubectl apply -f cm.yaml
    kubectl get configmaps

What you should see: auth whoami shows the kubernetes-admin user in the kubeadm:cluster-admins group, and every can-i answers yes. That is normal for a lab and exactly what you never hand out in production. The second ConfigMap came from a file you could review and commit; that is the habit to build.

Clean up:

Terminal window
rm -f cm.yaml
kubectl config set-context --current --namespace=default
kubectl delete namespace kcna-admin

Practise

  • Switch between two contexts with kubectl config use-context, then check which one is active.

On this site

Know

  • The scheduler picks a node for each new pod: it filters out nodes that cannot run it, then scores the rest.
  • Requests (not limits) decide whether a pod fits. Node selectors, affinity, taints and tolerations steer placement.
  • A pod that fits nowhere stays Pending with a FailedScheduling event.

Read

Demo: Make a pod unschedulable on purpose

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

    Terminal window
    kubectl create namespace kcna-sched
    kubectl config set-context --current --namespace=kcna-sched
  2. Ask for more CPU than any node has.

    Terminal window
    kubectl run greedy --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --overrides='{"spec":{"containers":[{"name":"greedy","image":"registry.k8s.io/e2e-test-images/agnhost:2.66.1","args":["pause"],"resources":{"requests":{"cpu":"64"}}}]}}'
    kubectl get pod greedy
  3. Read why the scheduler refused.

    Terminal window
    kubectl describe pod greedy | sed -n '/Events:/,$p'
    kubectl events --for pod/greedy
  4. Now steer a pod to one node with a label and a nodeSelector.

    Terminal window
    kubectl label node kind-worker2 demo=picked
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
    name: placed
    spec:
    nodeSelector:
    demo: picked
    containers:
    - name: app
    image: registry.k8s.io/e2e-test-images/agnhost:2.66.1
    args: ["pause"]
    EOF
    kubectl get pod placed -o wide

What you should see: greedy stays Pending with a FailedScheduling event that says how many nodes had insufficient CPU. placed lands on kind-worker2 and nowhere else.

Clean up:

Terminal window
kubectl label node kind-worker2 demo-
kubectl config set-context --current --namespace=default
kubectl delete namespace kcna-sched

Practise

  • Ask for more CPU than your node has and read the FailedScheduling event.

On this site

Know

  • A container image is layered, immutable filesystem content plus metadata; a container is a running instance isolated with Linux namespaces and limited with cgroups.
  • Kubernetes talks to runtimes such as containerd and CRI-O through the Container Runtime Interface (CRI). Docker Engine is no longer used directly by the kubelet.
  • Images are referenced by registry, repository, and tag or digest. A digest pins exact content.

Read

Demo: Tags move, digests don’t

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

    Terminal window
    kubectl create namespace kcna-images
    kubectl config set-context --current --namespace=kcna-images
  2. Run a pod by tag and read the digest the node actually pulled.

    Terminal window
    kubectl run bytag --image=registry.k8s.io/e2e-test-images/busybox:1.37.0-2 -- sleep 3600
    kubectl wait --for=condition=Ready pod/bytag --timeout=120s
    kubectl get pod bytag -o jsonpath='{.status.containerStatuses[0].imageID}{"\n"}'
  3. Look at the same image from the node’s side, through the container runtime.

    Terminal window
    docker exec kind-worker crictl images | grep busybox
    docker exec kind-worker crictl ps --name bytag
  4. Run a second pod pinned by that digest.

    Terminal window
    DIGEST=$(kubectl get pod bytag -o jsonpath='{.status.containerStatuses[0].imageID}')
    kubectl run bydigest --image="$DIGEST" -- sleep 3600
    kubectl get pod bydigest -o jsonpath='{.spec.containers[0].image}{"\n"}'

What you should see: The imageID is a sha256: digest. The tag is a pointer someone can move; the digest names exact content. crictl shows the kubelet talks to containerd, not Docker. (If bytag landed on kind-worker2, run the crictl lines there.)

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcna-images

Practise

  • Pull the same image by tag and by digest and compare kubectl get pod -o jsonpath image IDs.

On this site


Domain and competency names are from the CNCF KCNA curriculum (current curriculum (unversioned)), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.