CKAD — Essential kubectl Skills

6 minute read

Why kubectl Speed Matters

Speed is everything in the CKAD exam. You have roughly 6–8 minutes per task. The difference between passing and failing often comes down to how fast you can execute kubectl commands.

This page covers the essential kubectl skills and shortcuts you should master before sitting for the exam.

Set Up Aliases First

Run these commands at the very start of your exam session. They save seconds on every single command:

# Essential kubectl alias
alias k=kubectl
complete -o default -F __start_kubectl k

# Shortcuts for common flags
export do="--dry-run=client -o yaml"
export now="--force --grace-period 0"

How These Shortcuts Work

Shortcut Expands To Why It Helps
k kubectl Saves 6 characters per command
$do --dry-run=client -o yaml Generate YAML templates instantly
$now --force --grace-period 0 Delete resources immediately (no wait)

Usage Examples

# Instead of:
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml

# You type:
k run nginx --image=nginx $do > pod.yaml

# Instead of:
kubectl delete pod nginx --force --grace-period 0

# You type:
k delete pod nginx $now

Tip: Practice using these aliases in your daily kubectl work. By exam day, they should be muscle memory.

Imperative Commands (Faster Than YAML)

Imperative commands create resources directly from the command line — no YAML needed. They are significantly faster than writing YAML from scratch.

Create Resources

# Create a Pod
k run nginx --image=nginx:1.27

# Create a Pod with labels
k run nginx --image=nginx --labels="app=web,env=prod"

# Create a Pod with a specific port
k run nginx --image=nginx --port=80

# Create a Deployment
k create deployment web --image=nginx --replicas=3

# Create a Job
k create job backup --image=busybox -- /bin/sh -c "echo backup done"

# Create a CronJob
k create cronjob hourly-report --image=busybox --schedule="0 * * * *" -- /bin/sh -c "echo report"

# Create a ConfigMap from literals
k create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info

# Create a ConfigMap from a file
k create configmap app-config --from-file=config.properties

# Create a Secret
k create secret generic db-creds --from-literal=password=secret123

# Create a docker-registry Secret
k create secret docker-registry regcred \
  --docker-server=registry.example.com \
  --docker-username=user \
  --docker-password=pass

Expose Resources

# Expose a Deployment as ClusterIP
k expose deployment web --port=80 --target-port=80 --type=ClusterIP

# Expose a Deployment as NodePort
k expose deployment web --port=80 --target-port=80 --type=NodePort

# Expose a Pod
k expose pod nginx --port=80 --name=nginx-svc

Update Resources

# Scale a Deployment
k scale deployment web --replicas=5

# Update image on a Deployment
k set image deployment/web nginx=nginx:1.28

# Rollback a Deployment
k rollout undo deployment/web

# Check rollout status
k rollout status deployment/web

# View rollout history
k rollout history deployment/web

When to Use Imperative vs Declarative

Approach When to Use Command
Imperative Quick resource creation during exam kubectl run, kubectl create
Declarative Complex resources needing YAML customization kubectl apply -f manifest.yaml
Hybrid Generate YAML with imperative, then edit kubectl run ... $do > file.yaml

Pro Tip: The hybrid approach is the most powerful. Use --dry-run=client -o yaml to generate a YAML template, then add fields that imperative commands don’t support (like probes, volumes, security contexts).

Generate YAML Templates

The --dry-run=client -o yaml flag is your best friend. It generates YAML without creating the resource:

# Generate Pod YAML
k run nginx --image=nginx $do > pod.yaml

# Generate Deployment YAML
k create deployment web --image=nginx --replicas=3 $do > deploy.yaml

# Generate Job YAML
k create job backup --image=busybox $do -- /bin/sh -c "echo done" > job.yaml

# Generate Service YAML
k expose deployment web --port=80 --type=ClusterIP $do > svc.yaml

# Generate CronJob YAML
k create cronjob report --image=busybox --schedule="*/5 * * * *" $do -- /bin/sh -c "echo report" > cron.yaml

Then edit the generated YAML to add fields like probes, volumes, or security contexts.

Context Switching

The CKAD exam uses multiple clusters. You must switch contexts as directed by each question.

# List all contexts
k config get-contexts

# Check current context
k config current-context

# Switch to a specific cluster
k config use-context cluster2

# Set default namespace for current context
k config set-context --current --namespace=dev

# Verify namespace
k config view --minify | grep namespace

Warning: Always check which context you are on before answering a question. The exam frequently switches clusters. Working on the wrong cluster loses points.

Essential Debugging Commands

When things go wrong (and they will), these commands help you diagnose quickly:

# View Pod details and events
k describe pod <name>

# View container logs
k logs <pod-name>
k logs <pod-name> -c <container-name>    # Multi-container Pod
k logs <pod-name> --previous              # Previous container instance

# Execute commands in a running container
k exec -it <pod-name> -- /bin/sh
k exec <pod-name> -- cat /etc/config/app.properties

# Get events sorted by time
k get events --sort-by='.lastTimestamp'

# Check resource usage
k top pods
k top nodes

kubectl explain — Your In-Exam Reference

kubectl explain is like having the API documentation in your terminal. Use it when you can’t remember a field name:

# Top-level resource fields
k explain pod.spec

# Drill into nested fields
k explain pod.spec.containers
k explain pod.spec.containers.livenessProbe
k explain pod.spec.containers.resources

# Show full recursive documentation
k explain pod.spec --recursive | grep -i volume

# Deployment spec
k explain deployment.spec.strategy

Tip: kubectl explain often faster than switching to the docs browser tab. Learn to use it for quick field lookups.

Output Formatting

Format output to quickly find what you need:

# Wide output with more columns
k get pods -o wide

# Output as YAML
k get pod nginx -o yaml

# Output as JSON
k get pod nginx -o json

# Custom columns
k get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,IP:.status.podIP

# JSONPath — extract specific fields
k get pods -o jsonpath='{.items[*].metadata.name}'

# Sort by a field
k get pods --sort-by='.metadata.creationTimestamp'

Quick Reference Card

Task Command
Create Pod k run <name> --image=<img>
Create Deployment k create deploy <name> --image=<img> --replicas=<n>
Create Service k expose deploy <name> --port=<p> --type=<t>
Create ConfigMap k create cm <name> --from-literal=key=val
Create Secret k create secret generic <name> --from-literal=key=val
Create Job k create job <name> --image=<img> -- <cmd>
Create CronJob k create cj <name> --image=<img> --schedule=<s> -- <cmd>
Generate YAML Add --dry-run=client -o yaml
Scale k scale deploy <name> --replicas=<n>
Update image k set image deploy/<name> <container>=<img>
Rollback k rollout undo deploy/<name>
Delete fast k delete <resource> <name> --force --grace-period 0
Switch context k config use-context <name>
Set namespace k config set-context --current --namespace=<ns>

Key Takeaways

  • Set up alias k=kubectl and export do / export now at the start of every exam
  • Use imperative commands for creating resources quickly
  • Use the hybrid approach (imperative + edit YAML) for complex resources
  • Always check your context before answering a question
  • kubectl explain is faster than switching to the docs tab for field lookups
  • Master describe, logs, and exec for debugging

FAQ

Q: Are aliases pre-configured in the exam?

The k=kubectl alias is often pre-configured in recent exams, but don’t rely on it. Set it up yourself at the start to be safe.

Q: Can I use bash scripts during the exam?

Yes, you can write and execute bash scripts in the terminal. However, for most tasks, direct kubectl commands are faster.

Q: Is tab completion available?

Yes, kubectl bash completion is typically available. If not, you can enable it with source <(kubectl completion bash).

Updated: