$ command-helper

Kubernetes (kubectl)

Contexts, pods, logs, deployments, debugging and port-forward.

Requires Google sign-in

Contexts and namespaces

Current context
kubectl config current-context
List contexts
kubectl config get-contexts
Switch context
kubectl config use-context <context>
Default namespace for the current context
kubectl config set-context --current --namespace=<namespace>

Viewing resources

Pods in a namespace
kubectl get pods -n <namespace>
Pods with nodes and IPs
kubectl get pods -n <namespace> -o wide
All main resources in a namespace
kubectl get all -n <namespace>
Pod details and events
kubectl describe pod <pod> -n <namespace>
Events sorted by time
kubectl get events -n <namespace> --sort-by=.lastTimestamp
Pods not in Running stateadvanced
kubectl get pods -A --field-selector=status.phase!=Running
Pod resource usage
kubectl top pods -n <namespace>

Logs and debugging

Follow pod logs
kubectl logs -f <pod> -n <namespace>
Logs of the previous (crashed) container
kubectl logs <pod> -n <namespace> --previous
Logs of all pods in a deployment
kubectl logs -f deployment/<deployment> -n <namespace> --all-containers
Open a shell in a pod
kubectl exec -it <pod> -n <namespace> -- sh
Forward a service port to localhost
kubectl port-forward svc/<k8s_service> 8080:80 -n <namespace>
Temporary pod for network debuggingadvanced
kubectl run debug --rm -it --image=nicolaka/netshoot -n <namespace> -- bash

Deployments

Apply a manifest
kubectl apply -f manifest.yaml
Restart a deployment
kubectl rollout restart deployment/<deployment> -n <namespace>
Rollout status
kubectl rollout status deployment/<deployment> -n <namespace>
Roll back to the previous version
kubectl rollout undo deployment/<deployment> -n <namespace>
Scale
kubectl scale deployment/<deployment> --replicas=3 -n <namespace>
Decode a secretadvanced
kubectl get secret <k8s_secret> -n <namespace> -o jsonpath='{.data.password}' | base64 -d

Nodes

Nodes with versions and IPs
kubectl get nodes -o wide
Node resource usage
kubectl top nodes
Pods running on a node
kubectl get pods -A --field-selector spec.nodeName=<node>
Stop scheduling new pods on a nodeadvanced
kubectl cordon <node>
Evict pods before maintenanceadvanced
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data

Bring the node back with kubectl uncordon.

Config, jobs and access

ConfigMap from a file
kubectl create configmap <configmap> --from-file=app.yaml -n <namespace>
Secret from literals
kubectl create secret generic <k8s_secret> --from-literal=password='<secret_value>' -n <namespace>
Generate YAML instead of creating (for GitOps)advanced
kubectl create deployment <deployment> --image=<image> --dry-run=client -o yaml > deployment.yaml
Run a CronJob right nowadvanced
kubectl create job --from=cronjob/<cronjob> <cronjob>-manual -n <namespace>
Can I (or a service account) do this?advanced
kubectl auth can-i create deployments -n <namespace> --as system:serviceaccount:<namespace>:<service_account>
What changes before applying
kubectl diff -f manifest.yaml
Explain any resource field
kubectl explain deployment.spec.strategy
Wait until a deployment is readyadvanced
kubectl wait --for=condition=available deployment/<deployment> -n <namespace> --timeout=120s