Skip to content
Laviyx Learn

Try

Kubernetes Practice

Practise Kubernetes with kubectl: pods, deployments, scaling, rolling updates and rollbacks, services, ConfigMaps, Secrets and debugging failed pods. Every solution has been run on a real Kubernetes cluster and shows its real output.

Intermediate10 problemsUpdated

Review first: What Is Kubernetes?, Pods and Deployments, Services and Networking, ConfigMaps, Secrets and Probes

  1. 1.Run your first pod

    Beginner

    Start a pod called web from nginx:1.30-alpine, wait until it is ready, print its phase, then delete it.

    Show hint

    kubectl run, then kubectl wait --for=condition=Ready.

    Show solution
    Terminal
    kubectl run web --image=nginx:1.30-alpine
    kubectl wait --for=condition=Ready pod/web --timeout=90s > /dev/null
    kubectl get pod web -o jsonpath='{.status.phase}{"\n"}'
    kubectl delete pod web --wait=false > /dev/null
    Output
    pod/web created
    Running
  2. 2.Find pods by label

    Beginner

    Apply pod.yaml, then list the pods that have the label tier=frontend, and print the value of the app label. Delete the pod afterwards.

    Starter code (Terminal)
    cat > pod.yaml <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
      name: shop
      labels:
        app: shop
        tier: frontend
    spec:
      containers:
        - name: web
          image: nginx:1.30-alpine
          ports:
            - containerPort: 80
    EOF
    Show hint

    -l tier=frontend selects by label, and -o name prints just the names.

    Show solution
    Terminal
    kubectl apply -f pod.yaml
    kubectl wait --for=condition=Ready pod/shop --timeout=90s > /dev/null
    kubectl get pods -l tier=frontend -o name
    kubectl get pod shop -o jsonpath='{.metadata.labels.app}{"\n"}'
    kubectl delete -f pod.yaml --wait=false > /dev/null
    Output
    pod/shop created
    pod/shop
    shop
  3. 3.Deploy three copies

    Beginner

    Apply deployment.yaml, wait for the rollout, and print how many replicas are ready.

    Starter code (Terminal)
    cat > deployment.yaml <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: api
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: api
      template:
        metadata:
          labels:
            app: api
        spec:
          containers:
            - name: web
              image: nginx:1.29-alpine
              ports:
                - containerPort: 80
    EOF
    Show hint

    kubectl rollout status, then .status.readyReplicas.

    Show solution
    Terminal
    kubectl apply -f deployment.yaml
    kubectl rollout status deployment/api --timeout=120s | tail -n 1
    kubectl get deployment api -o jsonpath='{.status.readyReplicas}{"\n"}'
    kubectl delete -f deployment.yaml > /dev/null
    Output
    deployment.apps/api created
    deployment "api" successfully rolled out
    3
  4. 4.Scale up and down

    Intermediate

    With the api deployment running, scale it to 5 replicas and print the ready count, then scale it to 2 and print the number of pods once the extra ones are gone.

    Starter code (Terminal)
    cat > deployment.yaml <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: api
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: api
      template:
        metadata:
          labels:
            app: api
        spec:
          containers:
            - name: web
              image: nginx:1.29-alpine
              ports:
                - containerPort: 80
    EOF
    Show hint

    kubectl scale, then kubectl wait --for=delete for the pods that are removed.

    Show solution
    Terminal
    kubectl scale deployment api --replicas=5
    kubectl rollout status deployment/api --timeout=120s > /dev/null
    kubectl get deployment api -o jsonpath='{.status.readyReplicas}{"\n"}'
    kubectl scale deployment api --replicas=2
    kubectl wait --for=jsonpath='{.status.replicas}'=2 deployment/api --timeout=90s > /dev/null
    until [ "$(kubectl get pods -l app=api --no-headers | wc -l)" -eq 2 ]; do sleep 1; done
    kubectl get pods -l app=api --no-headers | wc -l
    kubectl delete -f deployment.yaml > /dev/null
    Output
    deployment.apps/api scaled
    5
    deployment.apps/api scaled
    2
  5. 5.Update the image

    Intermediate

    The api deployment runs nginx:1.29-alpine. Update it to nginx:1.30-alpine, wait for the rolling update to finish, and print the new image.

    Starter code (Terminal)
    cat > deployment.yaml <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: api
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: api
      template:
        metadata:
          labels:
            app: api
        spec:
          containers:
            - name: web
              image: nginx:1.29-alpine
              ports:
                - containerPort: 80
    EOF
    Show hint

    kubectl set image deployment/api web=..., where web is the container name.

    Show solution
    Terminal
    kubectl set image deployment/api web=nginx:1.30-alpine
    kubectl rollout status deployment/api --timeout=120s | tail -n 1
    kubectl get deployment api -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
    kubectl delete -f deployment.yaml > /dev/null
    Output
    deployment.apps/api image updated
    deployment "api" successfully rolled out
    nginx:1.30-alpine
  6. 6.Roll back a bad release

    Intermediate

    Someone deployed the image nginx:0.0.0-missing, which does not exist. Show that the new pod cannot start, then roll back and print the image that is running again.

    Starter code (Terminal)
    cat > deployment.yaml <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: api
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: api
      template:
        metadata:
          labels:
            app: api
        spec:
          containers:
            - name: web
              image: nginx:1.29-alpine
              ports:
                - containerPort: 80
    EOF
    Show hint

    kubectl rollout undo returns to the previous version.

    Show solution
    Terminal
    until kubectl get pods -l app=api -o jsonpath='{.items[*].status.containerStatuses[0].state.waiting.reason}' | grep -q Image; do sleep 2; done
    kubectl get pods -l app=api -o jsonpath='{range .items[*]}{.status.containerStatuses[0].state.waiting.reason}{"\n"}{end}' | grep -c Image
    kubectl rollout undo deployment/api 2> /dev/null
    kubectl rollout status deployment/api --timeout=120s | tail -n 1
    kubectl get deployment api -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
    kubectl delete -f deployment.yaml > /dev/null
    Output
    1
    deployment.apps/api rolled back
    deployment "api" successfully rolled out
    nginx:1.29-alpine
  7. 7.Reach a deployment through a Service

    Intermediate

    Expose the api deployment as a Service on port 80, then fetch the page title from a temporary Alpine pod using the Service's DNS name.

    Starter code (Terminal)
    cat > deployment.yaml <<'EOF'
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: api
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: api
      template:
        metadata:
          labels:
            app: api
        spec:
          containers:
            - name: web
              image: nginx:1.29-alpine
              ports:
                - containerPort: 80
    EOF
    Show hint

    kubectl expose, then kubectl run --rm -i with wget -qO- http://api.

    Show solution
    Terminal
    kubectl expose deployment api --port=80
    kubectl get service api -o jsonpath='{.spec.type}{"\n"}'
    kubectl run client --image=alpine:3.24 --restart=Never --rm -i --quiet -- wget -qO- http://api.default.svc.cluster.local | grep -o '<title>.*</title>'
    kubectl delete service api > /dev/null
    kubectl delete -f deployment.yaml > /dev/null
    Output
    service/api exposed
    ClusterIP
    <title>Welcome to nginx!</title>
  8. 8.Configure a pod with a ConfigMap

    Intermediate

    Create a ConfigMap settings with APP_ENV=production and LOG_LEVEL=info. Run a pod that loads it as environment variables and prints them, then read its logs.

    Show hint

    envFrom with configMapRef loads every key as a variable.

    Show solution
    Terminal
    kubectl create configmap settings --from-literal=APP_ENV=production --from-literal=LOG_LEVEL=info
    cat > app.yaml <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
      name: app
    spec:
      restartPolicy: Never
      containers:
        - name: app
          image: alpine:3.24
          command: ["sh", "-c", "echo env=$APP_ENV level=$LOG_LEVEL"]
          envFrom:
            - configMapRef:
                name: settings
    EOF
    kubectl apply -f app.yaml
    kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/app --timeout=90s > /dev/null
    kubectl logs app
    kubectl delete pod app --wait=false > /dev/null
    kubectl delete configmap settings > /dev/null
    Output
    configmap/settings created
    pod/app created
    env=production level=info
  9. 9.Store a password in a Secret

    Intermediate

    Create a Secret db with password=Ch4nge-me. Show that Kubernetes stores it base64-encoded, then decode it.

    Show hint

    -o jsonpath='{.data.password}', then base64 -d.

    Show solution
    Terminal
    kubectl create secret generic db --from-literal=password=Ch4nge-me
    kubectl get secret db -o jsonpath='{.data.password}{"\n"}'
    kubectl get secret db -o jsonpath='{.data.password}' | base64 -d
    echo
    kubectl delete secret db > /dev/null
    Output
    secret/db created
    Q2g0bmdlLW1l
    Ch4nge-me
  10. 10.Debug a crashing pod

    Advanced

    The pod in crash.yaml exits with code 1, so Kubernetes keeps restarting it. Apply it, wait for the first restart, then print the reason, the exit code and the message of the last crash.

    Starter code (Terminal)
    cat > crash.yaml <<'EOF'
    apiVersion: v1
    kind: Pod
    metadata:
      name: crash
    spec:
      containers:
        - name: app
          image: alpine:3.24
          command: ["sh", "-c", "echo Starting; echo Config file missing; exit 1"]
          terminationMessagePolicy: FallbackToLogsOnError
    EOF
    Show hint

    .status.containerStatuses[0].lastState.terminated holds reason, exitCode and message. terminationMessagePolicy: FallbackToLogsOnError puts the end of the log in message.

    Show solution
    Terminal
    kubectl apply -f crash.yaml
    until [ "$(kubectl get pod crash -o jsonpath='{.status.containerStatuses[0].restartCount}')" -ge 1 ] 2> /dev/null; do sleep 2; done
    kubectl get pod crash -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}{"\n"}'
    kubectl get pod crash -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}{"\n"}'
    kubectl get pod crash -o jsonpath='{.status.containerStatuses[0].lastState.terminated.message}'
    kubectl delete -f crash.yaml --wait=false > /dev/null
    Output
    pod/crash created
    Error
    1
    Starting
    Config file missing