kubectl cheatsheet
List namespaces:
kubectl --context=[context] get ns
List pods in namespace:
kubectl --context=[context] --namespace=[namespace] get pods
View deployment:
kubectl --context=[context] --namespace=[namespace] get deployments
Get deployment details:
kubectl --context=[context] --namespace=[namespace] describe deployments.apps [deploymentname]
Edit running deployment applying changes immediately:
kubectl --context=[context] --namespace=[namespace] edit deployments.apps [deploymentname]
Edit current configmap applying changes immediately:
kubectl --context=[context] --namespace=[namespace] edit configmap [configmapname]
Scale deployment to number of replicas:
kubectl --context=[context] --namespace=[namespace] scale deployment [deploymentname] --replicas=1
List secrets:
kubectl --context=[context] --namespace=[namespace] get secrets
List configmaps:
kubectl --context=[context] --namespace=[namespace] describe configmaps
View secrets:
kubectl --context=[context] --namespace=[namespace] describe secrets [secretname]
Decode a secret:
kubectl --context=[context] --namespace=[namespace] get secrets/[secretname] -o json| jq '.data | map_values(@base64d)'
Forward port 1080 to a pod:
kubectl --context=[context] port-forward --namespace=[namespace] [podname] 1080:1080
Get shell inside running pod:
kubectl --context=[context] --namespace=[namespace] exec --stdin --tty [podname] -- /bin/bash
View pod logs:
kubectl --context=[context] --namespace=[namespace] logs [podname]
Run pod with overrides:
kubectl --context=[context] run [podname] --namespace=[namespace] --rm -ti --image=ubuntu --overrides='{ "apiVersion": "v1", "spec": { "serviceAccountName": "workload-somedude-id" } }' -- bash
Display ingress as YAML:
kubectl --context=[context] --namespace=[namespace] get ingress [ingressname] -o yaml
Describe ingress:
kubectl --context=[context] --namespace=[namespace] describe ingress [ingressname]
Run container with inifinite loop, exit using CTRL+P followed by CTRL+Q:
kubectl --context=[context] --namespace=[namespace] run [containername] -ti --image ubuntu -- /bin/bash -c 'while true; do sleep 60; done'
Then enter the container interactively:
kubectl --context=[context] --namespace=[namespace] exec -ti [containername] -- /bin/bash