Post

Kubernetes

☸️ Kubernetes Cheat Sheet

📦 Cluster & Node Management

  1. Get cluster info.
    1
    
    kubectl cluster-info
    
  2. List all nodes.
    1
    
    kubectl get nodes
    
  3. Get node details.
    1
    
    kubectl describe node <node-name>
    
  4. Mark a node as unschedulable.
    1
    
    kubectl cordon <node-name>
    
  5. Mark a node as schedulable.
    1
    
    kubectl uncordon <node-name>
    
  6. Evict all pods from a node.
    1
    
    kubectl drain <node-name> --ignore-daemonsets
    
  7. Taint a node.
    1
    
    kubectl taint nodes <node-name> key=value:NoSchedule
    
  8. Add a label to a node.
    1
    
    kubectl label nodes <node-name> key=value
    
  9. Display node labels.
    1
    
    kubectl get nodes --show-labels
    
  10. Delete a node from the cluster.
    1
    
    kubectl delete node <node-name>
    
  11. Get nodes with a specific label.
    1
    
    kubectl get nodes --selector=env=prod
    
  12. Assign a node role.
    1
    
    kubectl label node <node-name> node-role.kubernetes.io/worker=
    
  13. Forcefully drain a node.
    1
    
    kubectl drain <node-name> --delete-local-data --force --ignore-daemonsets
    
  14. Custom format output.
    1
    
    kubectl get nodes -o custom-columns=NAME:.metadata.name,STATUS:.status.conditions.type
    
  15. Display node resource usage efficiently.
    1
    
    kubectl top nodes --use-protocol-buffers
    
  16. List only node names.
    1
    
    kubectl get nodes -o jsonpath='{.items[*].metadata.name}{"\n"}{end}'
    
  17. Check node capacity.
    1
    
    kubectl get nodes -o yaml | grep capacity
    
  18. Get node taints.
    1
    
    kubectl describe node <node-name> | grep Taint
    
  19. Label a node.
    1
    
    kubectl label node <node-name> disktype=ssd
    
  20. Get nodes with a specific label.
    1
    
    kubectl get nodes -l disktype=ssd
    
  21. Drain a node and delete emptyDir volumes.
    1
    
    kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
    
  22. Delete all nodes.
    1
    
    kubectl get nodes --no-headers | awk '{print $1}' | xargs kubectl delete node
    
  23. Get Kubelet version of nodes.
    1
    
    kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.kubeletVersion}'
    

🥅 Pod Management

  1. List all pods in the current namespace.
    1
    
    kubectl get pods
    
  2. List all pods in all namespaces.
    1
    
    kubectl get pods -A
    
  3. List pods with node details.
    1
    
    kubectl get pods -o wide
    
  4. Get pod details.
    1
    
    kubectl describe pod <pod-name>
    
  5. View logs of a pod.
    1
    
    kubectl logs <pod-name>
    
  6. View logs of a specific container.
    1
    
    kubectl logs <pod-name> -c <container-name>
    
  7. Access a running pod’s shell.
    1
    
    kubectl exec -it <pod-name> -- /bin/sh
    
  8. Delete a pod.
    1
    
    kubectl delete pod <pod-name>
    
  9. Force delete a pod.
    1
    
    kubectl delete pod --force --grace-period=0 <pod-name>
    
  10. Get pod YAML configuration.
    1
    
    kubectl get pod <pod-name> -o yaml
    
  11. Get pods by label.
    1
    
    kubectl get pod --selector=env=prod
    
  12. Add a label to a pod.
    1
    
    kubectl label pod <pod-name> env=prod
    
  13. Add an annotation.
    1
    
    kubectl annotate pod <pod-name> description="Test pod"
    
  14. List pods with a specific label.
    1
    
    kubectl get pods -l app=myapp
    
  15. Delete pods using a label selector.
    1
    
    kubectl delete pods -l app=myapp
    
  16. View logs from a previous container instance.
    1
    
    kubectl logs --previous <pod-name>
    
  17. Execute a command in a specific container.
    1
    
    kubectl exec <pod-name> -c <container-name> -- ls /app
    
  18. Copy a file from a pod.
    1
    
    kubectl cp <pod-name>:<file-path> <destination>
    
  19. Copy a file to a pod.
    1
    
    kubectl cp <source-file> <pod-name>:<destination-path>
    
  20. Sort pods by creation time.
    1
    
    kubectl get pods --sort-by=.metadata.creationTimestamp
    
  21. Force delete a pod immediately.
    1
    
    kubectl delete pod <pod-name> --grace-period=0 --force
    
  22. Get the IP address of a pod.
    1
    
    kubectl get pod <pod-name> -o jsonpath='{.status.podIP}'
    
  23. List only pod names.
    1
    
    kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
    
  24. Get only running pods.
    1
    
    kubectl get pods --field-selector=status.phase=Running
    
  25. Get pod conditions.
    1
    
    kubectl get pod <pod-name> -o jsonpath='{.status.conditions}'
    
  26. Get container images inside a pod.
    1
    
    kubectl get pod <pod-name> -o jsonpath='{.spec.containers[*].image}'
    
  27. Patch container image.
    1
    
    kubectl patch pod <pod-name> -p '{"spec":{"containers":[{"name":"<container-name>","image":"nginx:latest"}]}}'
    
  28. Delete all pods.
    1
    
    kubectl get pods --no-headers | awk '{print $1}' | xargs kubectl delete pod
    
  29. Delete all failed pods.
    1
    
    kubectl delete pod -n <namespace> --field-selector=status.phase=Failed
    
  30. Delete failed pods.
    1
    
    kubectl delete pod --field-selector=status.phase=Failed
    

🚀 Deployment Management

  1. List all deployments.
    1
    
    kubectl get deployments
    
  2. Get deployment details.
    1
    
    kubectl describe deployment <deployment-name>
    
  3. Deploy resources from a YAML file.
    1
    
    kubectl apply -f <deployment.yaml>
    
  4. Create a simple deployment.
    1
    
    kubectl create deployment <name> --image=<image>
    
  5. Delete a deployment.
    1
    
    kubectl delete deployment <deployment-name>
    
  6. Check deployment rollout status.
    1
    
    kubectl rollout status deployment <deployment-name>
    
  7. View deployment history.
    1
    
    kubectl rollout history deployment <deployment-name>
    
  8. Rollback to a previous version.
    1
    
    kubectl rollout undo deployment <deployment-name>
    
  9. Get all ReplicaSets.
    1
    
    kubectl get rs
    
  10. Scale a deployment.
    1
    
    kubectl scale deployment <deployment-name> --replicas=3
    
  11. Restart all pods in a deployment.
    1
    
    kubectl rollout restart deployment <deployment-name>
    
  12. Show labels of deployments.
    1
    
    kubectl get deployments --show-labels
    
  13. Delete all deployments.
    1
    
    kubectl delete deployment --all
    
  14. Update container image in a deployment.
    1
    
    kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"<container-name>","image":"nginx:latest"}]}}}}'
    
  15. Scale a deployment to zero.
    1
    
    kubectl scale --replicas=0 deployment/<deployment-name>
    
  16. Get deployment names only.
    1
    
    kubectl get deployment -o=jsonpath='{.items[*].metadata.name}'
    
  17. Update deployment image.
    1
    
    kubectl set image deployment/<deployment-name> <container-name>=nginx:latest
    
  18. Patch a resource.
    1
    
    kubectl patch deployment <deployment-name> -p '{"spec":{"replicas":5}}'
    
  19. Set resource requests/limits for a deployment.
    1
    
    kubectl set resources deployment <deployment-name> --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
    
  20. Patch deployment label.
    1
    
    kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"metadata":{"labels":{"version":"v2"}}}}}'
    
  21. Pause a deployment rollout.
    1
    
    kubectl rollout pause deployment <deployment-name>
    
  22. Resume a paused deployment rollout.
    1
    
    kubectl rollout resume deployment <deployment-name>
    
  23. Annotate a deployment.
    1
    
    kubectl annotate deployment <deployment-name> kubernetes.io/change-cause="Updated image to v2"
    

🌐 Services & Networking

  1. List all services.
    1
    
    kubectl get svc
    
  2. List all services in all namespaces.
    1
    
    kubectl get svc -A
    
  3. Get service details.
    1
    
    kubectl describe svc <service-name>
    
  4. Delete a service.
    1
    
    kubectl delete svc <service-name>
    
  5. Expose a deployment as a service.
    1
    
    kubectl expose deployment <deployment-name> --type=NodePort --port=80
    
  6. List all service endpoints.
    1
    
    kubectl get endpoints
    
  7. Forward local port to a pod.
    1
    
    kubectl port-forward pod/<pod-name> 8080:80
    
  8. Start a proxy to access the API server.
    1
    
    kubectl proxy
    
  9. List service names.
    1
    
    kubectl get services -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
    
  10. Delete all services.
    1
    
    kubectl delete service --all
    
  11. Change a service type.
    1
    
    kubectl patch svc <service-name> -p '{"spec": {"type": "LoadBalancer"}}'
    
  12. Get LoadBalancer IP.
    1
    
    kubectl get svc <service-name> -o jsonpath='{.status.loadBalancer.ingress.ip}'
    
  13. Get the NodePort of a service.
    1
    
    kubectl get svc <service-name> -o yaml | grep nodePort
    
  14. Change service port mapping.
    1
    
    kubectl patch service <service-name> -p '{"spec":{"ports":[{"port":8080,"targetPort":80}]}}'
    
  15. Forward a port to a deployment.
    1
    
    kubectl port-forward deployment/<deployment-name> 8080:80
    
  16. Get services sorted by creation date.
    1
    
    kubectl get svc --sort-by=.metadata.creationTimestamp
    

🚦 Ingress & Load Balancer

  1. List all ingress resources.
    1
    
    kubectl get ingress
    
  2. Get ingress details.
    1
    
    kubectl describe ingress <ingress-name>
    
  3. Delete an ingress resource.
    1
    
    kubectl delete ingress <ingress-name>
    
  4. Deploy an ingress rule.
    1
    
    kubectl apply -f ingress.yaml
    
  5. Get Ingress external IP.
    1
    
    kubectl get ingress -o jsonpath='{.items[*].status.loadBalancer.ingress[*].ip}'
    
  6. Get ingress names only.
    1
    
    kubectl get ingress -o=jsonpath='{.items[*].metadata.name}'
    
  7. List all ingress hosts.
    1
    
    kubectl get ingress -o jsonpath='{range .items[*]}{.spec.rules[*].host}'
    
  8. Edit an existing ingress rule.
    1
    
    kubectl edit ingress <ingress-name>
    
  9. Delete all ingress rules.
    1
    
    kubectl delete ingress --all
    

⚙️ ConfigMaps & Secrets

  1. List all ConfigMaps.
    1
    
    kubectl get configmaps
    
  2. Get ConfigMap details.
    1
    
    kubectl describe configmap <configmap-name>
    
  3. Create a ConfigMap.
    1
    
    kubectl create configmap <config-name> --from-literal=key=value
    
  4. Delete a ConfigMap.
    1
    
    kubectl delete configmap <configmap-name>
    
  5. List all secrets.
    1
    
    kubectl get secrets
    
  6. Get secret details.
    1
    
    kubectl describe secret <secret-name>
    
  7. Create a secret.
    1
    
    kubectl create secret generic <secret-name> --from-literal=key=value
    
  8. View ConfigMap in YAML format.
    1
    
    kubectl get configmap <configmap-name> -o yaml
    
  9. Decode secret value.
    1
    
    kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 --decode
    
  10. Edit an existing ConfigMap.
    1
    
    kubectl edit configmap <configmap-name>
    

🏳️‍🌈 Namespace Management

  1. List all namespaces.
    1
    
    kubectl get namespaces
    
  2. Create a new namespace.
    1
    
    kubectl create namespace <namespace>
    
  3. Delete a namespace.
    1
    
    kubectl delete namespace <namespace>
    
  4. List pods in a specific namespace.
    1
    
    kubectl get pods -n <namespace>
    
  5. Set default namespace for the current context.
    1
    
    kubectl config set-context --current --namespace=<namespace>
    
  6. Force delete a namespace.
    1
    
    kubectl delete namespace <namespace> --grace-period=0 --force
    
  7. Get namespace names only.
    1
    
    kubectl get namespace -o=jsonpath='{.items[*].metadata.name}'
    

💾 Persistent Storage

  1. List all Persistent Volumes.
    1
    
    kubectl get pv
    
  2. List all Persistent Volume Claims.
    1
    
    kubectl get pvc
    
  3. Get PV details.
    1
    
    kubectl describe pv <pv-name>
    
  4. Get PVC details.
    1
    
    kubectl describe pvc <pvc-name>
    
  5. View PV details.
    1
    
    kubectl get pv -o wide
    
  6. List PVC names only.
    1
    
    kubectl get pvc -o jsonpath='{.items[*].metadata.name}'
    

🧰 StatefulSets & DaemonSets

  1. List all StatefulSets.
    1
    
    kubectl get statefulsets
    
  2. Get StatefulSet details.
    1
    
    kubectl describe statefulset <statefulset-name>
    
  3. List all DaemonSets.
    1
    
    kubectl get daemonsets
    
  4. Get DaemonSet details.
    1
    
    kubectl describe daemonset <daemonset-name>
    
  5. Restart a DaemonSet.
    1
    
    kubectl rollout restart daemonset <daemonset-name>
    

🐞 Events & Debugging

  1. List all events.
    1
    
    kubectl get events
    
  2. Sort events by time.
    1
    
    kubectl get events --sort-by=.metadata.creationTimestamp
    
  3. Debug a running pod.
    1
    
    kubectl debug pod/<pod-name> --image=busybox
    

🛡️ RBAC & Security

  1. List all roles.
    1
    
    kubectl get roles
    
  2. List all role bindings.
    1
    
    kubectl get rolebindings
    
  3. Get role details.
    1
    
    kubectl describe role <role-name>
    
  4. List all cluster roles.
    1
    
    kubectl get clusterroles
    
  5. List all cluster role bindings.
    1
    
    kubectl get clusterrolebindings
    

📊 Resource Management

  1. View CPU & memory usage of nodes.
    1
    
    kubectl top nodes
    
  2. View CPU & memory usage of pods.
    1
    
    kubectl top pods
    
  3. Edit a live deployment.
    1
    
    kubectl edit deployment <deployment-name>
    
  4. View Kubeconfig settings.
    1
    
    kubectl config view
    
  5. List available Kubernetes contexts.
    1
    
    kubectl config get-contexts
    
  6. Switch to a different context.
    1
    
    kubectl config use-context <context-name>
    

⎈ Helm

  1. List all Helm releases.
    1
    
    helm list
    
  2. Install a Helm chart.
    1
    
    helm install <name> <chart>
    
  3. Upgrade a Helm release.
    1
    
    helm upgrade <name> <chart>
    
  4. Rollback a Helm release.
    1
    
    helm rollback <name> <revision>
    

⏱️ Jobs & CronJobs

  1. List all jobs.
    1
    
    kubectl get jobs
    
  2. Get job details.
    1
    
    kubectl describe job <job-name>
    
  3. Delete a job.
    1
    
    kubectl delete job <job-name>
    
  4. List all cron jobs.
    1
    
    kubectl get cronjobs
    
  5. Delete a cron job.
    1
    
    kubectl delete cronjob <cronjob-name>
    

🛠️ Miscellaneous

  1. Get documentation for pod resources.
    1
    
    kubectl explain pod
    
  2. Run a temporary pod.
    1
    
    kubectl run --rm -it busybox -- /bin/sh
    
  3. Test YAML before applying.
    1
    
    kubectl apply --dry-run=client -f <file>.yaml
    
  4. Delete all resources in the namespace.
    1
    
    kubectl delete all --all
    
  5. Delete all pods and services.
    1
    
    kubectl delete pod,svc --all
    
  6. Delete all jobs.
    1
    
    kubectl delete jobs --all
    
  7. Check component health.
    1
    
    kubectl get componentstatus
    
  8. View certificate signing requests.
    1
    
    kubectl get csr
    
  9. Drain a node with emptyDir cleanup.
    1
    
    kubectl drain <node> --delete-emptydir-data
    
  10. Run a debug container.
    1
    
    kubectl run debug --image=busybox --restart=Never --rm -it -- /bin/sh
    
  11. List all API resources.
    1
    
    kubectl api-resources
    
  12. Get details of API services.
    1
    
    kubectl describe apiservices
    
  13. Delete a CertificateSigningRequest.
    1
    
    kubectl delete csr <csr-name>
    
  14. Explain pod spec structure.
    1
    
    kubectl explain pod.spec
    
  15. Generate pod YAML.
    1
    
    kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml
    
  16. Update an environment variable in a deployment.
    1
    
    kubectl set env deployment/<deployment-name> CONFIG_VAR=value
    
This post is licensed under CC BY 4.0 by the author.