Kubectl helpful commands

k8s cheatsheet.

Get all pods by label

$ kubectl --kubeconfig <config file> get po -lapp=<pod name without hash> -Lversion,cfg,infra-cfg

Exec into the pod

$ kubectl --kubeconfig <config file> exec -it <pod name> sh

Port forwarding

$ kubectl --kubeconfig <config file> port-forward <pod name> <local port>:<remote port>

Getting logs

$ kubectl --kubeconfig <config file> logs <pod name> <pod name without hash>

Get replica limits from configs:

$ kubectl --kubeconfig <config file> get hpa <pod name without hash>
# more details
$ kubectl --kubeconfig <config file> get deployment <pod name without hash> --output=yaml
$ kubectl --kubeconfig <config file> describe deployments <pod name without hash>
$ kubectl --kubeconfig <config file> describe pod <pod name without hash> # CPU and memory limits/requests (quota)

Remove pod from load balancing to debug startup errors:

$ kubectl --kubeconfig <config file> label pod <pod_name> <labels to remove>
# and remove (kill the pod) afterwards
$ kubectl --kubeconfig <config file> delete pod <pod_name>

Scale (up/down) pods:

$ kubectl --context=xx-live-default get pods -l app=boblin -o wide
$ kubectl --context=xx-live-default scale --current-replicas=12 --replicas=3 deployment/boblin
$ kubectl --context=xx-live-default delete pods boblin-3353777365-qjdpd
$ kubectl --kubeconfig <config file> scale deployment <pod name without hash> --current-replicas=0 --replicas=2
$ kubectl --kubeconfig <config file> get po -lapp=<label> -Lversion,cfg
$ watch -n 1 kubectl --kubeconfig <config file> get po -lapp=<label> -Lversion,cfg
$ kubectl --kubeconfig <config file> exec <exact pod hash> sh
$ kubectl --kubeconfig <config file> get hpa <label>

More commands here:
https://kubernetes.io/docs/reference/kubectl/overview/
https://kubernetes.io/docs/reference/kubectl/cheatsheet/

LEAVE A COMMENT