Introduction

This comprehensive cheatsheet covers essential kubectl commands for daily Kubernetes operations. Bookmark this for quick reference!

Cluster Info

kubectl version
kubectl cluster-info
kubectl get nodes
kubectl get componentstatuses
kubectl config view
kubectl config current-context
kubectl config use-context <context-name>

Pods

# List
kubectl get pods
kubectl get pods -o wide
kubectl get pods --all-namespaces
kubectl get pods -n <namespace>
kubectl get pods --show-labels
kubectl get pods -l app=nginx

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

# Describe
kubectl describe pod <pod-name>
kubectl get pod <pod-name> -o yaml

# Logs
kubectl logs <pod-name>
kubectl logs -f <pod-name>
kubectl logs <pod-name> -c <container-name>
kubectl logs --previous <pod-name>

# Execute
kubectl exec -it <pod-name> -- bash
kubectl exec <pod-name> -- ls /
kubectl exec <pod-name> -c <container> -- bash

# Port Forward
kubectl port-forward <pod-name> 8080:80

# Delete
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --force --grace-period=0

Deployments

# Create
kubectl create deployment nginx --image=nginx --replicas=3
kubectl apply -f deployment.yaml

# List
kubectl get deployments
kubectl get deploy -o wide

# Scale
kubectl scale deployment nginx --replicas=5
kubectl autoscale deployment nginx --min=3 --max=10 --cpu-percent=80

# Update
kubectl set image deployment/nginx nginx=nginx:1.21
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment/nginx --to-revision=2

# Edit
kubectl edit deployment nginx

# Delete
kubectl delete deployment nginx

Services

# Create
kubectl expose deployment nginx --port=80 --type=ClusterIP
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl expose deployment nginx --port=80 --type=LoadBalancer

# List
kubectl get services
kubectl get svc -o wide

# Describe
kubectl describe service nginx

# Delete
kubectl delete service nginx

ConfigMaps & Secrets

# ConfigMap
kubectl create configmap myconfig --from-literal=key=value
kubectl create configmap myconfig --from-file=config.txt
kubectl get configmaps
kubectl describe configmap myconfig
kubectl delete configmap myconfig

# Secrets
kubectl create secret generic mysecret --from-literal=password=secret
kubectl create secret generic mysecret --from-file=password.txt
kubectl create secret tls tls-secret --cert=cert.crt --key=key.key
kubectl get secrets
kubectl describe secret mysecret
kubectl get secret mysecret -o jsonpath='{.data.password}' | base64 --decode
kubectl delete secret mysecret

Namespaces

kubectl get namespaces
kubectl create namespace dev
kubectl delete namespace dev
kubectl config set-context --current --namespace=dev
kubectl get pods --all-namespaces
kubectl get pods -n kube-system

Labels & Selectors

kubectl label pods nginx app=web
kubectl label pods nginx app-
kubectl get pods -l app=web
kubectl get pods -l 'app in (web,api)'
kubectl get pods --show-labels

Resource Management

# Get all resources
kubectl get all
kubectl get all -n <namespace>

# Describe
kubectl describe <resource-type> <resource-name>

# Edit
kubectl edit <resource-type> <resource-name>

# Delete
kubectl delete <resource-type> <resource-name>
kubectl delete -f file.yaml
kubectl delete all --all

Debugging

# Events
kubectl get events
kubectl get events --sort-by=.metadata.creationTimestamp

# Logs
kubectl logs <pod-name>
kubectl logs <pod-name> --previous
kubectl logs -f <pod-name>
kubectl logs <pod-name> -c <container>

# Describe
kubectl describe pod <pod-name>
kubectl describe node <node-name>

# Top
kubectl top nodes
kubectl top pods
kubectl top pod <pod-name>

# Debug
kubectl run debug --image=busybox -it --rm -- sh
kubectl debug <pod-name> -it --image=busybox

Contexts & Config

kubectl config get-contexts
kubectl config current-context
kubectl config use-context <context>
kubectl config set-context --current --namespace=<namespace>
kubectl config view

Apply & Create

kubectl apply -f file.yaml
kubectl apply -f directory/
kubectl apply -f https://url/file.yaml
kubectl create -f file.yaml
kubectl replace -f file.yaml
kubectl delete -f file.yaml

Output Formats

kubectl get pods -o wide
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o name
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

Useful Aliases

alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deployments'
alias kdp='kubectl describe pod'
alias kl='kubectl logs'
alias kex='kubectl exec -it'
alias kaf='kubectl apply -f'
alias kdel='kubectl delete'

Advanced Commands

# Patch
kubectl patch deployment nginx -p '{"spec":{"replicas":5}}'

# Annotate
kubectl annotate pod nginx description="My nginx pod"

# Taint
kubectl taint nodes node1 key=value:NoSchedule
kubectl taint nodes node1 key:NoSchedule-

# Cordon/Uncordon
kubectl cordon node1
kubectl uncordon node1
kubectl drain node1 --ignore-daemonsets

# Copy files
kubectl cp <pod>:/path/to/file ./local-file
kubectl cp ./local-file <pod>:/path/to/file

Helm Commands

# Repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm search repo nginx

# Install
helm install myrelease bitnami/nginx
helm install myrelease bitnami/nginx -f values.yaml
helm upgrade myrelease bitnami/nginx

# List
helm list
helm status myrelease

# Uninstall
helm uninstall myrelease

# Rollback
helm rollback myrelease 1

Quick Tips

Watch resources:

kubectl get pods -w
watch kubectl get pods

Multiple resources:

kubectl get pods,svc,deploy

All namespaces:

kubectl get pods --all-namespaces
kubectl get pods -A

Dry run:

kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml

Force delete:

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

Conclusion

Master these commands for efficient Kubernetes operations. Practice regularly to build muscle memory!

Resources