Skip to main content

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 kubectl commands.
  • Module 3 is Docker-focused, so the kubectl commands come primarily from Module 2 and Module 4.
  • k is commonly used as a shorthand alias for kubectl.
  • Common short resource names used in the modules:
    • po = pods
    • svc = services
    • rs = ReplicaSets

Cluster Basics


CommandExampleWhat it does
kubectl get nodeskubectl get nodesLists the worker nodes in the cluster. Good first connectivity check after getting AKS credentials.
kubectl get nodes -o widekubectl get nodes -o wideShows more node detail, including internal IPs, Kubernetes version, and scheduling information.
kubectl get pods --all-namespaceskubectl get pods --all-namespacesLists pods across every namespace, including system pods in kube-system.
kubectl get all --all-namespaceskubectl get all --all-namespacesLists the main resources across all namespaces: pods, services, deployments, ReplicaSets, and more.
kubectl get nskubectl get nsLists namespaces in the cluster. Useful when checking isolation boundaries.
kubectl get allkubectl get allLists the main resources in the current namespace, usually default unless you changed context.

Pods


CommandExampleWhat it does
kubectl runkubectl run my-pod --image=nginx --port=80Creates a pod imperatively from the command line. Useful for quick demos and tests.
kubectl get podskubectl get podsLists pods in the current namespace.
kubectl get pokubectl get poShort form of kubectl get pods.
kubectl get pods -o widekubectl get pods -o wideLists pods plus extra detail like node placement and pod IPs.
kubectl describe podkubectl describe pod my-podShows detailed pod information, including events, image pulls, status, and conditions.
kubectl logskubectl logs my-podDumps the current logs from a pod's container.
kubectl logs -fkubectl logs -f my-podStreams pod logs live. Useful while testing in a browser or troubleshooting startup issues.
kubectl exec -it ... -- /bin/shkubectl exec -it my-pod -- /bin/shOpens an interactive shell inside a running container.
kubectl exec -it ... -- envkubectl exec -it my-pod -- envRuns a one-off command inside the container without opening a shell.
kubectl exec -it ... -- lskubectl exec -it my-pod -- lsLists files in the container filesystem.
kubectl exec -it ... -- cat ...kubectl exec -it my-pod -- cat /usr/share/nginx/html/index.htmlReads a file from inside the container.
kubectl delete podkubectl delete pod my-podDeletes a pod. If a controller owns it, Kubernetes usually recreates it.

Services And Ingress


CommandExampleWhat it does
kubectl get serviceskubectl get servicesLists services in the current namespace.
kubectl get services --all-namespaceskubectl get services --all-namespacesLists services across the entire cluster.
kubectl get servicekubectl get serviceSame general idea as get services; often used when checking for external IP assignment.
kubectl get svckubectl get svcShort form of kubectl get service.
kubectl describe servicekubectl describe service my-serviceShows selectors, endpoints, ports, cluster IP, external IP, and recent events for a service.
kubectl get ingresskubectl get ingressLists Ingress resources in the current namespace.
kubectl get ingress --all-namespaceskubectl get ingress --all-namespacesLists all Ingress resources across the cluster.
kubectl describe ingresskubectl describe ingress my-ingressShows hosts, paths, backend services, and events for an Ingress resource.
kubectl expose podkubectl expose pod my-pod --type=LoadBalancer --port=80 --target-port=80 --name=my-serviceCreates a service in front of a pod. In AKS, LoadBalancer triggers Azure load balancer integration.
kubectl expose rskubectl expose rs my-replicasetapp-rs --type=LoadBalancer --port=80 --target-port=80 --name=my-replicaset-serviceCreates a service in front of a ReplicaSet so traffic can be distributed across its pods.
kubectl expose deploymentkubectl expose deployment my-deployment --type=LoadBalancer --port=80 --target-port=80 --name=my-deployment-serviceCreates a service in front of a Deployment. Common way to publish an app externally.
kubectl expose deploymentkubectl expose deployment my-backend-app --port=8080 --target-port=8080 --name=my-backend-serviceCreates a default ClusterIP service for internal app-to-app communication.

YAML And Declarative Workflows


CommandExampleWhat it does
kubectl apply -fkubectl apply -f deployment.yamlCreates or updates a resource from a YAML file. Preferred for declarative workflows.
kubectl apply -fkubectl apply -f service.yamlApplies a service manifest from YAML.
kubectl create -fkubectl create -f replicaset-demo.ymlCreates a resource from YAML without trying to merge updates like apply does.
kubectl replace -fkubectl replace -f replicaset-demo.ymlReplaces an existing resource definition with the contents of the YAML file. Useful after changing replica counts or specs.
kubectl get pod -o yamlkubectl get pod my-pod -o yamlShows the live pod manifest that Kubernetes is storing.
kubectl get service -o yamlkubectl get service my-service -o yamlShows the live service manifest stored in the cluster.
kubectl get pod -o yamlkubectl get pods my-replicasetapp-rs-wgdt6 -o yamlUseful for checking ownerReferences and seeing which controller owns a pod.

Deployments


CommandExampleWhat it does
kubectl create deploymentkubectl create deployment my-deployment --image=stacksimplify/kubenginx:1.0.0Creates a Deployment imperatively from an image.
kubectl create deploymentkubectl create deployment my-backend-app --image=stacksimplify/kube-helloworld:1.0.0Creates a backend application Deployment.
kubectl create deploymentkubectl create deployment my-frontend-app --image=solostroup/my-frontend-app:1.0.0Creates a frontend application Deployment.
kubectl get deploymentskubectl get deploymentsLists Deployments in the current namespace.
kubectl get deploymentkubectl get deploymentAlso lists Deployment objects.
kubectl describe deploymentkubectl describe deployment my-deploymentShows status, selectors, events, replica counts, and rollout information for a Deployment.
kubectl get replicasetkubectl get replicasetLists ReplicaSets. Useful when a Deployment creates new ones during updates.
kubectl get rskubectl get rsShort form of kubectl get replicaset.

Scaling


CommandExampleWhat it does
kubectl scale --replicaskubectl scale --replicas=10 deployment/my-deploymentScales a Deployment up to 10 pods.
kubectl scale --replicaskubectl scale --replicas=2 deployment/my-deploymentScales a Deployment down to 2 pods.
kubectl scale --replicaskubectl scale --replicas=10 deployment/my-backend-appScales the backend Deployment in the services demo.
kubectl scale --replicaskubectl scale --replicas=5 deployment/my-frontend-appScales the frontend Deployment in the services demo.

Rollouts, Updates, And Rollbacks


CommandExampleWhat it does
kubectl set imagekubectl set image deployment/my-deployment kubenginx=stacksimplify/kubenginx:2.0.0 --record=trueUpdates the image for a container in a Deployment and records rollout history.
kubectl rollout statuskubectl rollout status deployment/my-deploymentWatches and reports rollout progress for a Deployment.
kubectl rollout historykubectl rollout history deployment/my-deploymentShows the revision history for a Deployment.
kubectl rollout history --revisionkubectl rollout history deployment/my-deployment --revision=2Shows details for a specific Deployment revision.
kubectl edit deploymentkubectl edit deployment/mydeployment --record=trueOpens the Deployment manifest in an editor for live cluster changes.
kubectl rollout undokubectl rollout undo deployment/my-deploymentRolls back a Deployment to the previous revision.
kubectl rollout undo --to-revisionkubectl rollout undo deployment/my-deployment --to-revision=2Rolls back directly to a specific revision.
kubectl rollout restartkubectl rollout restart deployment/my-deploymentRestarts all pods in a Deployment one by one without changing the image version.
kubectl rollout pausekubectl rollout pause deployment/my-deploymentPauses a Deployment so multiple changes can be staged before rollout.
kubectl rollout resumekubectl rollout resume deployment/my-deploymentResumes a paused Deployment and applies pending changes.
kubectl set resourceskubectl set resources deployment/my-deployment -c=kubenginx --limits=cpu=20m,memory=30MiUpdates resource limits on the Deployment's container.

Cleanup


CommandExampleWhat it does
kubectl delete -fkubectl delete -f deployment.yamlDeletes a resource using the YAML file that created it.
kubectl delete -fkubectl delete -f service.yamlDeletes a service from its manifest file.
kubectl delete deploymentkubectl delete deployment myapp1-deploymentDeletes a Deployment by name.
kubectl delete servicekubectl delete service myapp1-loadbalancerDeletes a Service by name.
kubectl delete svckubectl delete svc my-serviceShort form for deleting a Service.
kubectl delete replicasetkubectl delete replicaset my-replicasetapp-rsDeletes a ReplicaSet and the pods it owns.
kubectl delete service/...kubectl delete service/my-frontend-serviceDeletes the frontend service by resource type and name.
kubectl delete service/...kubectl delete service/my-backend-serviceDeletes the backend service by resource type and name.
kubectl delete deployment/...kubectl delete deployment/my-frontend-appDeletes the frontend Deployment.
kubectl delete deployment/...kubectl delete deployment/my-backend-appDeletes 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.