Appendix 1: Kubernetes Cheatsheet
This cheatsheet summarizes the kubectl commands introduced across Module 1 through Module 4. I will revisit it later and add more commands from the remaining modules as I work through them.
Notes:
- Module 1 is introductory and does not add
kubectlcommands. - Module 3 is Docker-focused, so the
kubectlcommands come primarily from Module 2 and Module 4. kis commonly used as a shorthand alias forkubectl.- Common short resource names used in the modules:
po= podssvc= servicesrs= ReplicaSets
Cluster Basics
| Command | Example | What it does |
|---|---|---|
kubectl get nodes | kubectl get nodes | Lists the worker nodes in the cluster. Good first connectivity check after getting AKS credentials. |
kubectl get nodes -o wide | kubectl get nodes -o wide | Shows more node detail, including internal IPs, Kubernetes version, and scheduling information. |
kubectl get pods --all-namespaces | kubectl get pods --all-namespaces | Lists pods across every namespace, including system pods in kube-system. |
kubectl get all --all-namespaces | kubectl get all --all-namespaces | Lists the main resources across all namespaces: pods, services, deployments, ReplicaSets, and more. |
kubectl get ns | kubectl get ns | Lists namespaces in the cluster. Useful when checking isolation boundaries. |
kubectl get all | kubectl get all | Lists the main resources in the current namespace, usually default unless you changed context. |
Pods
| Command | Example | What it does |
|---|---|---|
kubectl run | kubectl run my-pod --image=nginx --port=80 | Creates a pod imperatively from the command line. Useful for quick demos and tests. |
kubectl get pods | kubectl get pods | Lists pods in the current namespace. |
kubectl get po | kubectl get po | Short form of kubectl get pods. |
kubectl get pods -o wide | kubectl get pods -o wide | Lists pods plus extra detail like node placement and pod IPs. |
kubectl describe pod | kubectl describe pod my-pod | Shows detailed pod information, including events, image pulls, status, and conditions. |
kubectl logs | kubectl logs my-pod | Dumps the current logs from a pod's container. |
kubectl logs -f | kubectl logs -f my-pod | Streams pod logs live. Useful while testing in a browser or troubleshooting startup issues. |
kubectl exec -it ... -- /bin/sh | kubectl exec -it my-pod -- /bin/sh | Opens an interactive shell inside a running container. |
kubectl exec -it ... -- env | kubectl exec -it my-pod -- env | Runs a one-off command inside the container without opening a shell. |
kubectl exec -it ... -- ls | kubectl exec -it my-pod -- ls | Lists files in the container filesystem. |
kubectl exec -it ... -- cat ... | kubectl exec -it my-pod -- cat /usr/share/nginx/html/index.html | Reads a file from inside the container. |
kubectl delete pod | kubectl delete pod my-pod | Deletes a pod. If a controller owns it, Kubernetes usually recreates it. |
Services And Ingress
| Command | Example | What it does |
|---|---|---|
kubectl get services | kubectl get services | Lists services in the current namespace. |
kubectl get services --all-namespaces | kubectl get services --all-namespaces | Lists services across the entire cluster. |
kubectl get service | kubectl get service | Same general idea as get services; often used when checking for external IP assignment. |
kubectl get svc | kubectl get svc | Short form of kubectl get service. |
kubectl describe service | kubectl describe service my-service | Shows selectors, endpoints, ports, cluster IP, external IP, and recent events for a service. |
kubectl get ingress | kubectl get ingress | Lists Ingress resources in the current namespace. |
kubectl get ingress --all-namespaces | kubectl get ingress --all-namespaces | Lists all Ingress resources across the cluster. |
kubectl describe ingress | kubectl describe ingress my-ingress | Shows hosts, paths, backend services, and events for an Ingress resource. |
kubectl expose pod | kubectl expose pod my-pod --type=LoadBalancer --port=80 --target-port=80 --name=my-service | Creates a service in front of a pod. In AKS, LoadBalancer triggers Azure load balancer integration. |
kubectl expose rs | kubectl expose rs my-replicasetapp-rs --type=LoadBalancer --port=80 --target-port=80 --name=my-replicaset-service | Creates a service in front of a ReplicaSet so traffic can be distributed across its pods. |
kubectl expose deployment | kubectl expose deployment my-deployment --type=LoadBalancer --port=80 --target-port=80 --name=my-deployment-service | Creates a service in front of a Deployment. Common way to publish an app externally. |
kubectl expose deployment | kubectl expose deployment my-backend-app --port=8080 --target-port=8080 --name=my-backend-service | Creates a default ClusterIP service for internal app-to-app communication. |
YAML And Declarative Workflows
| Command | Example | What it does |
|---|---|---|
kubectl apply -f | kubectl apply -f deployment.yaml | Creates or updates a resource from a YAML file. Preferred for declarative workflows. |
kubectl apply -f | kubectl apply -f service.yaml | Applies a service manifest from YAML. |
kubectl create -f | kubectl create -f replicaset-demo.yml | Creates a resource from YAML without trying to merge updates like apply does. |
kubectl replace -f | kubectl replace -f replicaset-demo.yml | Replaces an existing resource definition with the contents of the YAML file. Useful after changing replica counts or specs. |
kubectl get pod -o yaml | kubectl get pod my-pod -o yaml | Shows the live pod manifest that Kubernetes is storing. |
kubectl get service -o yaml | kubectl get service my-service -o yaml | Shows the live service manifest stored in the cluster. |
kubectl get pod -o yaml | kubectl get pods my-replicasetapp-rs-wgdt6 -o yaml | Useful for checking ownerReferences and seeing which controller owns a pod. |
Deployments
| Command | Example | What it does |
|---|---|---|
kubectl create deployment | kubectl create deployment my-deployment --image=stacksimplify/kubenginx:1.0.0 | Creates a Deployment imperatively from an image. |
kubectl create deployment | kubectl create deployment my-backend-app --image=stacksimplify/kube-helloworld:1.0.0 | Creates a backend application Deployment. |
kubectl create deployment | kubectl create deployment my-frontend-app --image=solostroup/my-frontend-app:1.0.0 | Creates a frontend application Deployment. |
kubectl get deployments | kubectl get deployments | Lists Deployments in the current namespace. |
kubectl get deployment | kubectl get deployment | Also lists Deployment objects. |
kubectl describe deployment | kubectl describe deployment my-deployment | Shows status, selectors, events, replica counts, and rollout information for a Deployment. |
kubectl get replicaset | kubectl get replicaset | Lists ReplicaSets. Useful when a Deployment creates new ones during updates. |
kubectl get rs | kubectl get rs | Short form of kubectl get replicaset. |
Scaling
| Command | Example | What it does |
|---|---|---|
kubectl scale --replicas | kubectl scale --replicas=10 deployment/my-deployment | Scales a Deployment up to 10 pods. |
kubectl scale --replicas | kubectl scale --replicas=2 deployment/my-deployment | Scales a Deployment down to 2 pods. |
kubectl scale --replicas | kubectl scale --replicas=10 deployment/my-backend-app | Scales the backend Deployment in the services demo. |
kubectl scale --replicas | kubectl scale --replicas=5 deployment/my-frontend-app | Scales the frontend Deployment in the services demo. |
Rollouts, Updates, And Rollbacks
| Command | Example | What it does |
|---|---|---|
kubectl set image | kubectl set image deployment/my-deployment kubenginx=stacksimplify/kubenginx:2.0.0 --record=true | Updates the image for a container in a Deployment and records rollout history. |
kubectl rollout status | kubectl rollout status deployment/my-deployment | Watches and reports rollout progress for a Deployment. |
kubectl rollout history | kubectl rollout history deployment/my-deployment | Shows the revision history for a Deployment. |
kubectl rollout history --revision | kubectl rollout history deployment/my-deployment --revision=2 | Shows details for a specific Deployment revision. |
kubectl edit deployment | kubectl edit deployment/mydeployment --record=true | Opens the Deployment manifest in an editor for live cluster changes. |
kubectl rollout undo | kubectl rollout undo deployment/my-deployment | Rolls back a Deployment to the previous revision. |
kubectl rollout undo --to-revision | kubectl rollout undo deployment/my-deployment --to-revision=2 | Rolls back directly to a specific revision. |
kubectl rollout restart | kubectl rollout restart deployment/my-deployment | Restarts all pods in a Deployment one by one without changing the image version. |
kubectl rollout pause | kubectl rollout pause deployment/my-deployment | Pauses a Deployment so multiple changes can be staged before rollout. |
kubectl rollout resume | kubectl rollout resume deployment/my-deployment | Resumes a paused Deployment and applies pending changes. |
kubectl set resources | kubectl set resources deployment/my-deployment -c=kubenginx --limits=cpu=20m,memory=30Mi | Updates resource limits on the Deployment's container. |
Cleanup
| Command | Example | What it does |
|---|---|---|
kubectl delete -f | kubectl delete -f deployment.yaml | Deletes a resource using the YAML file that created it. |
kubectl delete -f | kubectl delete -f service.yaml | Deletes a service from its manifest file. |
kubectl delete deployment | kubectl delete deployment myapp1-deployment | Deletes a Deployment by name. |
kubectl delete service | kubectl delete service myapp1-loadbalancer | Deletes a Service by name. |
kubectl delete svc | kubectl delete svc my-service | Short form for deleting a Service. |
kubectl delete replicaset | kubectl delete replicaset my-replicasetapp-rs | Deletes a ReplicaSet and the pods it owns. |
kubectl delete service/... | kubectl delete service/my-frontend-service | Deletes the frontend service by resource type and name. |
kubectl delete service/... | kubectl delete service/my-backend-service | Deletes the backend service by resource type and name. |
kubectl delete deployment/... | kubectl delete deployment/my-frontend-app | Deletes the frontend Deployment. |
kubectl delete deployment/... | kubectl delete deployment/my-backend-app | Deletes the backend Deployment. |
Fast Starter Set
If you only want the commands you will reach for most often, start with these:
kubectl get nodes
kubectl get pods
kubectl get svc
kubectl describe pod my-pod
kubectl logs my-pod
kubectl exec -it my-pod -- /bin/sh
kubectl apply -f deployment.yaml
kubectl expose deployment my-app --type=LoadBalancer --port=80 --target-port=80 --name=my-app-service
kubectl scale --replicas=3 deployment/my-app
kubectl rollout status deployment/my-app
kubectl get all
kubectl delete deployment/my-app
That set covers cluster checks, inspection, troubleshooting, deployment, service exposure, scaling, rollout verification, and cleanup.