CKA — Essential kubectl Skills
Why Speed Matters
The CKA exam gives you 2 hours for 15–20 tasks. That is roughly 6–8 minutes per task. If you spend time typing long commands or writing YAML from scratch, you will run out of time. kubectl mastery and smart shortcuts are essential for passing.
Set up these shortcuts at the very start of your exam — they will save minutes on every question.
Aliases and Auto-Completion
# Set these up FIRST THING in the exam
alias k=kubectl
complete -o default -F __start_kubectl k
# Shortcuts for common operations
export do="--dry-run=client -o yaml"
export now="--force --grace-period 0"
These three lines give you:
kinstead ofkubectl(saves typing on every command)- Tab-completion for
k(works likekubectltab-complete) $doto generate YAML without creating resources$nowto force-delete resources instantly
Imperative Commands
Imperative commands are faster than writing YAML from scratch. Use them to create resources quickly, then edit the YAML if needed.
Create Resources
# Create a Pod
k run nginx --image=nginx
# 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 report --image=busybox --schedule="*/5 * * * *" -- /bin/sh -c "date"
# Create a Namespace
k create namespace dev
# Create a ConfigMap
k create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info
# Create a Secret
k create secret generic db-creds --from-literal=password=secret123
# Create a ServiceAccount
k create serviceaccount app-sa
# Create a ClusterRole
k create clusterrole pod-reader --verb=get,list,watch --resource=pods
# Create a ClusterRoleBinding
k create clusterrolebinding pod-reader-binding --clusterrole=pod-reader --serviceaccount=default:app-sa
Expose Resources
# Expose a Deployment as ClusterIP Service
k expose deployment web --port=80 --target-port=80
# Expose as NodePort
k expose deployment web --port=80 --target-port=80 --type=NodePort
# Create a Service for a Pod
k expose pod nginx --port=80 --name=nginx-svc
Generate YAML (Dry-Run)
# 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 Service YAML
k expose deployment web --port=80 $do > svc.yaml
# Edit the YAML file, then apply
vim pod.yaml
k apply -f pod.yaml
Imperative vs Declarative
| Aspect | Imperative | Declarative |
|---|---|---|
| Speed | Faster for simple resources | Slower (write YAML first) |
| Complexity | Limited options | Full customization |
| Exam strategy | Use for simple creates | Use for complex specs |
| Example | k run nginx --image=nginx |
k apply -f pod.yaml |
| Best for | Pods, Deployments, Services, ConfigMaps | Multi-container Pods, complex scheduling, volumes |
Exam strategy: Use imperative commands to create simple resources. Use $do to generate YAML, then edit it for complex requirements.
Context Switching
Every CKA question specifies a cluster context. Always switch context before starting a question.
# List all contexts
k config get-contexts
# Switch to a different cluster
k config use-context cluster1
# Verify current context
k config current-context
# Set default namespace for current context
k config set-context --current --namespace=kube-system
# View current context details
k config view --minify
Warning: Forgetting to switch context is the #1 reason candidates lose marks on tasks they know how to solve. Make it a habit: read the context line, switch, then start.
Output Formatting
# Wide output (shows node, IP, etc.)
k get pods -o wide
# YAML output
k get pod nginx -o yaml
# JSON output
k get pod nginx -o json
# JSONPath — extract specific fields
k get pods -o jsonpath='{.items[*].metadata.name}'
# Custom columns
k get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# Sort by creation time
k get pods --sort-by=.metadata.creationTimestamp
Debugging Commands
# Describe a resource (events, conditions)
k describe pod nginx
# View logs
k logs nginx
k logs nginx -c sidecar # specific container
k logs nginx --previous # previous crashed container
k logs -l app=web # logs by label
# Execute command in a Pod
k exec -it nginx -- /bin/sh
# Debug a node
k debug node/worker1 -it --image=busybox
# Check resource usage
k top pods
k top nodes
# Check RBAC permissions
k auth can-i create pods
k auth can-i create pods --as=dev-user
k auth can-i create pods --as=system:serviceaccount:default:app-sa
Quick Reference Table
| Task | Command |
|---|---|
| Create Pod | k run nginx --image=nginx |
| Create Deployment | k create deploy web --image=nginx --replicas=3 |
| Expose Service | k expose deploy web --port=80 --type=NodePort |
| Generate YAML | k run nginx --image=nginx $do > pod.yaml |
| Force delete | k delete pod nginx $now |
| Switch context | k config use-context cluster1 |
| Set namespace | k config set-context --current --namespace=dev |
| Check permissions | k auth can-i create pods --as=dev-user |
| View all resources | k get all -A |
| Describe resource | k describe pod nginx |
| View logs | k logs nginx -f |
| Exec into Pod | k exec -it nginx -- /bin/sh |
Next: Plan your study path with Prerequisites & Study Plan →