Kubectl is your best friend when working with Kubernetes. Here are 20 commands for daily work.
Context and Namespace¶
kubectl config get-contexts kubectl config use-context production kubectl config set-context –current –namespace=app
Resource Overview¶
kubectl get pods -A kubectl get pods -o wide kubectl get pods –sort-by=.status.startTime
Logs¶
kubectl logs -f my-pod kubectl logs –previous my-pod kubectl logs -l app=frontend –all-containers
Exec and Port Forward¶
kubectl exec -it my-pod – /bin/sh kubectl port-forward svc/my-app 8080:80
Resource Usage¶
kubectl top pods –sort-by=memory kubectl top nodes
Scale and Rollout¶
kubectl scale deployment my-app –replicas=5 kubectl rollout undo deployment/my-app kubectl rollout status deployment/my-app
Apply and Delete¶
kubectl apply -f manifest.yaml kubectl delete pod my-pod –grace-period=0 –force
JSONPath and Dry Run¶
kubectl get pods -o jsonpath=’{.items[*].metadata.name}’ kubectl create deployment nginx –image=nginx –dry-run=client -o yaml
Labels and Selectors¶
kubectl label pods my-pod env=production kubectl get pods -l env=production
Debug and Events¶
kubectl debug my-pod -it –image=busybox kubectl get events –sort-by=.lastTimestamp kubectl describe pod my-pod
ConfigMap and Secret¶
kubectl create configmap my-config –from-file=config.yaml kubectl create secret generic db-pass –from-literal=password=s3cret kubectl get secret db-pass -o jsonpath=’{.data.password}’ | base64 -d
Aliases¶
alias k=kubectl alias kgp=”kubectl get pods” alias kl=”kubectl logs -f”
Tip¶
Install kubectx, kubens and set up zsh completion — game changer.