Kubernetes

Cluster Info

kubectl cluster-infoCluster endpoint info
kubectl get nodesList cluster nodes
kubectl get nsList namespaces
kubectl config viewView kubeconfig
kubectl config use-contextSwitch context
kubectl api-resourcesList resource types

Pods

kubectl get podsList pods
kubectl get pods -o widePods with node info
kubectl describe pod <name>Pod details
kubectl logs <pod>Pod logs
kubectl logs -f <pod>Follow logs
kubectl exec -it <pod> -- shShell into pod
kubectl delete pod <name>Delete pod
kubectl port-forward <pod> 8080:80Port forward

Deployments

kubectl get deployList deployments
kubectl create deploy <n> --image=imgCreate deployment
kubectl scale deploy <n> --replicas=3Scale replicas
kubectl rollout status deploy/<n>Rollout status
kubectl rollout undo deploy/<n>Rollback
kubectl set image deploy/<n> c=img:v2Update image
kubectl rollout historyRollout history
kubectl edit deploy <n>Edit in place

Services & Networking

kubectl get svcList services
kubectl expose deploy <n> --port=80Create service
kubectl get ingressList ingress rules
ClusterIPInternal-only service
NodePortExpose on node port
LoadBalancerExternal load balancer
kubectl get endpointsService endpoints

Config & Secrets

kubectl create configmapCreate configmap
kubectl create secret genericCreate secret
kubectl get configmapsList configmaps
kubectl get secretsList secrets
kubectl describe secret <n>Secret details
--from-literal=k=vFrom key-value pair
--from-file=pathFrom file
envFrom: configMapRefUse in pod spec

Storage

kubectl get pvPersistent volumes
kubectl get pvcPersistent volume claims
kubectl get scStorage classes
emptyDir: {}Temp pod volume
hostPathNode filesystem path
persistentVolumeClaimPVC-backed volume

Apply & Manage

kubectl apply -f file.yamlApply config
kubectl apply -f dir/Apply all in dir
kubectl delete -f file.yamlDelete from file
kubectl diff -f file.yamlPreview changes
kubectl get allAll resources
kubectl get all -n namespaceIn namespace
kubectl label pod <n> k=vAdd label
kubectl annotate pod <n> k=vAdd annotation

Debugging

kubectl describe <resource>Detailed info
kubectl get eventsCluster events
kubectl top podsPod resource usage
kubectl top nodesNode resource usage
kubectl logs --previousPrevious container logs
kubectl run debug --image=busybox -itDebug pod

Output & Filtering

-o yamlOutput as YAML
-o jsonOutput as JSON
-o wideExtra columns
-l app=webFilter by label
--all-namespaces / -AAll namespaces
-w / --watchWatch for changes
allprintabledoc.com