Kubernetes
☸️ Kubernetes Cheat Sheet
📦 Cluster & Node Management
- Get cluster info.
1
kubectl cluster-info
- List all nodes.
1
kubectl get nodes
- Get node details.
1
kubectl describe node <node-name>
- Mark a node as unschedulable.
1
kubectl cordon <node-name>
- Mark a node as schedulable.
1
kubectl uncordon <node-name>
- Evict all pods from a node.
1
kubectl drain <node-name> --ignore-daemonsets
- Taint a node.
1
kubectl taint nodes <node-name> key=value:NoSchedule
- Add a label to a node.
1
kubectl label nodes <node-name> key=value
- Display node labels.
1
kubectl get nodes --show-labels
- Delete a node from the cluster.
1
kubectl delete node <node-name>
- Get nodes with a specific label.
1
kubectl get nodes --selector=env=prod
- Assign a node role.
1
kubectl label node <node-name> node-role.kubernetes.io/worker=
- Forcefully drain a node.
1
kubectl drain <node-name> --delete-local-data --force --ignore-daemonsets
- Custom format output.
1
kubectl get nodes -o custom-columns=NAME:.metadata.name,STATUS:.status.conditions.type
- Display node resource usage efficiently.
1
kubectl top nodes --use-protocol-buffers
- List only node names.
1
kubectl get nodes -o jsonpath='{.items[*].metadata.name}{"\n"}{end}'
- Check node capacity.
1
kubectl get nodes -o yaml | grep capacity
- Get node taints.
1
kubectl describe node <node-name> | grep Taint
- Label a node.
1
kubectl label node <node-name> disktype=ssd
- Get nodes with a specific label.
1
kubectl get nodes -l disktype=ssd
- Drain a node and delete emptyDir volumes.
1
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
- Delete all nodes.
1
kubectl get nodes --no-headers | awk '{print $1}' | xargs kubectl delete node
- Get Kubelet version of nodes.
1
kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.kubeletVersion}'
🥅 Pod Management
- List all pods in the current namespace.
1
kubectl get pods
- List all pods in all namespaces.
1
kubectl get pods -A
- List pods with node details.
1
kubectl get pods -o wide
- Get pod details.
1
kubectl describe pod <pod-name>
- View logs of a pod.
1
kubectl logs <pod-name>
- View logs of a specific container.
1
kubectl logs <pod-name> -c <container-name>
- Access a running pod’s shell.
1
kubectl exec -it <pod-name> -- /bin/sh
- Delete a pod.
1
kubectl delete pod <pod-name>
- Force delete a pod.
1
kubectl delete pod --force --grace-period=0 <pod-name>
- Get pod YAML configuration.
1
kubectl get pod <pod-name> -o yaml
- Get pods by label.
1
kubectl get pod --selector=env=prod
- Add a label to a pod.
1
kubectl label pod <pod-name> env=prod
- Add an annotation.
1
kubectl annotate pod <pod-name> description="Test pod"
- List pods with a specific label.
1
kubectl get pods -l app=myapp
- Delete pods using a label selector.
1
kubectl delete pods -l app=myapp
- View logs from a previous container instance.
1
kubectl logs --previous <pod-name>
- Execute a command in a specific container.
1
kubectl exec <pod-name> -c <container-name> -- ls /app
- Copy a file from a pod.
1
kubectl cp <pod-name>:<file-path> <destination>
- Copy a file to a pod.
1
kubectl cp <source-file> <pod-name>:<destination-path>
- Sort pods by creation time.
1
kubectl get pods --sort-by=.metadata.creationTimestamp
- Force delete a pod immediately.
1
kubectl delete pod <pod-name> --grace-period=0 --force
- Get the IP address of a pod.
1
kubectl get pod <pod-name> -o jsonpath='{.status.podIP}'
- List only pod names.
1
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
- Get only running pods.
1
kubectl get pods --field-selector=status.phase=Running
- Get pod conditions.
1
kubectl get pod <pod-name> -o jsonpath='{.status.conditions}'
- Get container images inside a pod.
1
kubectl get pod <pod-name> -o jsonpath='{.spec.containers[*].image}'
- Patch container image.
1
kubectl patch pod <pod-name> -p '{"spec":{"containers":[{"name":"<container-name>","image":"nginx:latest"}]}}'
- Delete all pods.
1
kubectl get pods --no-headers | awk '{print $1}' | xargs kubectl delete pod
- Delete all failed pods.
1
kubectl delete pod -n <namespace> --field-selector=status.phase=Failed
- Delete failed pods.
1
kubectl delete pod --field-selector=status.phase=Failed
🚀 Deployment Management
- List all deployments.
1
kubectl get deployments
- Get deployment details.
1
kubectl describe deployment <deployment-name>
- Deploy resources from a YAML file.
1
kubectl apply -f <deployment.yaml>
- Create a simple deployment.
1
kubectl create deployment <name> --image=<image>
- Delete a deployment.
1
kubectl delete deployment <deployment-name>
- Check deployment rollout status.
1
kubectl rollout status deployment <deployment-name>
- View deployment history.
1
kubectl rollout history deployment <deployment-name>
- Rollback to a previous version.
1
kubectl rollout undo deployment <deployment-name>
- Get all ReplicaSets.
1
kubectl get rs
- Scale a deployment.
1
kubectl scale deployment <deployment-name> --replicas=3
- Restart all pods in a deployment.
1
kubectl rollout restart deployment <deployment-name>
- Show labels of deployments.
1
kubectl get deployments --show-labels
- Delete all deployments.
1
kubectl delete deployment --all
- Update container image in a deployment.
1
kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"<container-name>","image":"nginx:latest"}]}}}}'
- Scale a deployment to zero.
1
kubectl scale --replicas=0 deployment/<deployment-name>
- Get deployment names only.
1
kubectl get deployment -o=jsonpath='{.items[*].metadata.name}'
- Update deployment image.
1
kubectl set image deployment/<deployment-name> <container-name>=nginx:latest
- Patch a resource.
1
kubectl patch deployment <deployment-name> -p '{"spec":{"replicas":5}}'
- Set resource requests/limits for a deployment.
1
kubectl set resources deployment <deployment-name> --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
- Patch deployment label.
1
kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"metadata":{"labels":{"version":"v2"}}}}}'
- Pause a deployment rollout.
1
kubectl rollout pause deployment <deployment-name>
- Resume a paused deployment rollout.
1
kubectl rollout resume deployment <deployment-name>
- Annotate a deployment.
1
kubectl annotate deployment <deployment-name> kubernetes.io/change-cause="Updated image to v2"
🌐 Services & Networking
- List all services.
1
kubectl get svc
- List all services in all namespaces.
1
kubectl get svc -A
- Get service details.
1
kubectl describe svc <service-name>
- Delete a service.
1
kubectl delete svc <service-name>
- Expose a deployment as a service.
1
kubectl expose deployment <deployment-name> --type=NodePort --port=80
- List all service endpoints.
1
kubectl get endpoints
- Forward local port to a pod.
1
kubectl port-forward pod/<pod-name> 8080:80
- Start a proxy to access the API server.
1
kubectl proxy
- List service names.
1
kubectl get services -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
- Delete all services.
1
kubectl delete service --all
- Change a service type.
1
kubectl patch svc <service-name> -p '{"spec": {"type": "LoadBalancer"}}'
- Get LoadBalancer IP.
1
kubectl get svc <service-name> -o jsonpath='{.status.loadBalancer.ingress.ip}'
- Get the NodePort of a service.
1
kubectl get svc <service-name> -o yaml | grep nodePort
- Change service port mapping.
1
kubectl patch service <service-name> -p '{"spec":{"ports":[{"port":8080,"targetPort":80}]}}'
- Forward a port to a deployment.
1
kubectl port-forward deployment/<deployment-name> 8080:80
- Get services sorted by creation date.
1
kubectl get svc --sort-by=.metadata.creationTimestamp
🚦 Ingress & Load Balancer
- List all ingress resources.
1
kubectl get ingress
- Get ingress details.
1
kubectl describe ingress <ingress-name>
- Delete an ingress resource.
1
kubectl delete ingress <ingress-name>
- Deploy an ingress rule.
1
kubectl apply -f ingress.yaml
- Get Ingress external IP.
1
kubectl get ingress -o jsonpath='{.items[*].status.loadBalancer.ingress[*].ip}'
- Get ingress names only.
1
kubectl get ingress -o=jsonpath='{.items[*].metadata.name}'
- List all ingress hosts.
1
kubectl get ingress -o jsonpath='{range .items[*]}{.spec.rules[*].host}'
- Edit an existing ingress rule.
1
kubectl edit ingress <ingress-name>
- Delete all ingress rules.
1
kubectl delete ingress --all
⚙️ ConfigMaps & Secrets
- List all ConfigMaps.
1
kubectl get configmaps
- Get ConfigMap details.
1
kubectl describe configmap <configmap-name>
- Create a ConfigMap.
1
kubectl create configmap <config-name> --from-literal=key=value
- Delete a ConfigMap.
1
kubectl delete configmap <configmap-name>
- List all secrets.
1
kubectl get secrets
- Get secret details.
1
kubectl describe secret <secret-name>
- Create a secret.
1
kubectl create secret generic <secret-name> --from-literal=key=value
- View ConfigMap in YAML format.
1
kubectl get configmap <configmap-name> -o yaml
- Decode secret value.
1
kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 --decode
- Edit an existing ConfigMap.
1
kubectl edit configmap <configmap-name>
🏳️🌈 Namespace Management
- List all namespaces.
1
kubectl get namespaces
- Create a new namespace.
1
kubectl create namespace <namespace>
- Delete a namespace.
1
kubectl delete namespace <namespace>
- List pods in a specific namespace.
1
kubectl get pods -n <namespace>
- Set default namespace for the current context.
1
kubectl config set-context --current --namespace=<namespace>
- Force delete a namespace.
1
kubectl delete namespace <namespace> --grace-period=0 --force
- Get namespace names only.
1
kubectl get namespace -o=jsonpath='{.items[*].metadata.name}'
💾 Persistent Storage
- List all Persistent Volumes.
1
kubectl get pv
- List all Persistent Volume Claims.
1
kubectl get pvc
- Get PV details.
1
kubectl describe pv <pv-name>
- Get PVC details.
1
kubectl describe pvc <pvc-name>
- View PV details.
1
kubectl get pv -o wide
- List PVC names only.
1
kubectl get pvc -o jsonpath='{.items[*].metadata.name}'
🧰 StatefulSets & DaemonSets
- List all StatefulSets.
1
kubectl get statefulsets
- Get StatefulSet details.
1
kubectl describe statefulset <statefulset-name>
- List all DaemonSets.
1
kubectl get daemonsets
- Get DaemonSet details.
1
kubectl describe daemonset <daemonset-name>
- Restart a DaemonSet.
1
kubectl rollout restart daemonset <daemonset-name>
🐞 Events & Debugging
- List all events.
1
kubectl get events
- Sort events by time.
1
kubectl get events --sort-by=.metadata.creationTimestamp
- Debug a running pod.
1
kubectl debug pod/<pod-name> --image=busybox
🛡️ RBAC & Security
- List all roles.
1
kubectl get roles
- List all role bindings.
1
kubectl get rolebindings
- Get role details.
1
kubectl describe role <role-name>
- List all cluster roles.
1
kubectl get clusterroles
- List all cluster role bindings.
1
kubectl get clusterrolebindings
📊 Resource Management
- View CPU & memory usage of nodes.
1
kubectl top nodes
- View CPU & memory usage of pods.
1
kubectl top pods
- Edit a live deployment.
1
kubectl edit deployment <deployment-name>
- View Kubeconfig settings.
1
kubectl config view
- List available Kubernetes contexts.
1
kubectl config get-contexts
- Switch to a different context.
1
kubectl config use-context <context-name>
⎈ Helm
- List all Helm releases.
1
helm list
- Install a Helm chart.
1
helm install <name> <chart>
- Upgrade a Helm release.
1
helm upgrade <name> <chart>
- Rollback a Helm release.
1
helm rollback <name> <revision>
⏱️ Jobs & CronJobs
- List all jobs.
1
kubectl get jobs
- Get job details.
1
kubectl describe job <job-name>
- Delete a job.
1
kubectl delete job <job-name>
- List all cron jobs.
1
kubectl get cronjobs
- Delete a cron job.
1
kubectl delete cronjob <cronjob-name>
🛠️ Miscellaneous
- Get documentation for pod resources.
1
kubectl explain pod
- Run a temporary pod.
1
kubectl run --rm -it busybox -- /bin/sh
- Test YAML before applying.
1
kubectl apply --dry-run=client -f <file>.yaml
- Delete all resources in the namespace.
1
kubectl delete all --all
- Delete all pods and services.
1
kubectl delete pod,svc --all
- Delete all jobs.
1
kubectl delete jobs --all
- Check component health.
1
kubectl get componentstatus
- View certificate signing requests.
1
kubectl get csr
- Drain a node with emptyDir cleanup.
1
kubectl drain <node> --delete-emptydir-data
- Run a debug container.
1
kubectl run debug --image=busybox --restart=Never --rm -it -- /bin/sh
- List all API resources.
1
kubectl api-resources
- Get details of API services.
1
kubectl describe apiservices
- Delete a CertificateSigningRequest.
1
kubectl delete csr <csr-name>
- Explain pod spec structure.
1
kubectl explain pod.spec
- Generate pod YAML.
1
kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml
- 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.