Skip to content

kubectl Command Flashcards

Quick reference flashcards for kubectl commands. Test yourself by hiding the answers!


Pod Operations

Q: Create a pod named 'nginx' with nginx image

Answer
kubectl run nginx --image=nginx

Q: Create a pod with specific port exposed

Answer
kubectl run nginx --image=nginx --port=80

Q: Get pods in all namespaces

Answer
kubectl get pods -A
# or
kubectl get pods --all-namespaces

Q: Get pod YAML output

Answer
kubectl get pod nginx -o yaml

Q: Delete pod immediately (force)

Answer
kubectl delete pod nginx --force --grace-period=0

Q: Execute command in pod

Answer
kubectl exec nginx -- ls /
kubectl exec -it nginx -- /bin/bash

Q: Get pod logs

Answer
kubectl logs nginx
kubectl logs nginx -f  # follow
kubectl logs nginx --previous  # previous container

Deployment Operations

Q: Create deployment with 3 replicas

Answer
kubectl create deployment nginx --image=nginx --replicas=3

Q: Scale deployment

Answer
kubectl scale deployment nginx --replicas=5

Q: Update deployment image

Answer
kubectl set image deployment/nginx nginx=nginx:1.20

Q: Rollback deployment

Answer
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment/nginx --to-revision=2

Q: Check rollout status

Answer
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx

Service Operations

Q: Expose deployment as ClusterIP service

Answer
kubectl expose deployment nginx --port=80 --type=ClusterIP

Q: Expose as NodePort

Answer
kubectl expose deployment nginx --port=80 --type=NodePort

Q: Get service endpoints

Answer
kubectl get endpoints nginx

ConfigMap & Secret

Q: Create ConfigMap from literal

Answer
kubectl create configmap myconfig --from-literal=key1=value1 --from-literal=key2=value2

Q: Create ConfigMap from file

Answer
kubectl create configmap myconfig --from-file=config.txt
kubectl create configmap myconfig --from-file=mykey=config.txt

Q: Create Secret from literal

Answer
kubectl create secret generic mysecret --from-literal=password=secret123

Q: Decode secret value

Answer
kubectl get secret mysecret -o jsonpath='{.data.password}' | base64 -d

Namespace & Context

Q: Create namespace

Answer
kubectl create namespace dev

Q: Set default namespace for context

Answer
kubectl config set-context --current --namespace=dev

Q: Get current context

Answer
kubectl config current-context

Q: Switch context

Answer
kubectl config use-context my-cluster

YAML Generation

Q: Generate pod YAML without creating

Answer
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml

Q: Generate deployment YAML

Answer
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deploy.yaml

Q: Generate service YAML

Answer
kubectl expose deployment nginx --port=80 --dry-run=client -o yaml > svc.yaml

Troubleshooting

Q: Describe resource for events

Answer
kubectl describe pod nginx
kubectl describe node worker-1

Q: Get events sorted by time

Answer
kubectl get events --sort-by='.lastTimestamp'

Q: Check resource usage

Answer
kubectl top pods
kubectl top nodes

Q: Debug with temporary pod

Answer
kubectl run debug --image=busybox --rm -it --restart=Never -- sh

← Back to Home