K8s kubectl CheatSheet (Troubleshooting)
?
S
BashMaintenance Ops for K8s. Official Docs: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/
1# Start a dummy pod
2 kubectl run -it --rm debug --image=busybox --restart=Never -- sh
3
4# View all Pods and their Nodes allocation
5kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces
6
7# Set all replicas to 0
8kubectl get deployments | grep 'nexus-\|redis-' | awk '{print $1}' | xargs -I {} kubectl scale deployment {} --replicas=0
9
10# Kubebctl binary not running as expected (after installing kubectl with brew)
11# Errors: error: SchemaError(io.k8s.apimachinery.pkg.apis.meta.v1.Patch): invalid object doesnt have additional properties
12rm /usr/local/bin/kubectl
13brew link --overwrite --dry-run kubernetes-cli
14brew link --overwrite kubernetes-cli
15
16
17# View logs of a crashed container
18kubectl logs plucking-parrot-metrics-server-68db7b7fd5-n4n72
19
20# View logs with tail
21kubectl logs plucking-bird --follow --tail 1
22
23# View all Pods
24kubectl get pod --all-namespaces
25
26# Torubleshooting Pending Pods
27kubectl describe pod frontend | grep -A 3 Events
28
29# Check Nodes Capacity
30kubectl describe nodes ip-192-168-104-36.ec2.internal
31
32# Check for Issues on Pod
33kubectl get pods --selector="name=bad-frontend"
34
35# Describe why POD is failing
36pod=$(kubectl get pods --selector="name=bad-frontend" --output=jsonpath={.items..metadata.name})
37kubectl describe pod $pod
38
39# Run a bash command on a POD
40kubectl exec $pod -- echo $PATH
41kubectl exec $pod -- /usr/bin/curl -s localhost/health
42kubectl exec -it secret-env-pod env | grep SECRET_
43
44# Export/Copy a service nodePort
45export PORT=$(kubectl get svc hello-webapp -o go-template='{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}')Created on 7/27/2018