Kubectl of

Keywoards

  • pods
  • ns: namespace(s)
  • sc: storageclass(s)
  • pv: persistentvolumes
  • pvc: persistentvolumeclaim
  • deploy: deployment(s)

Actions

  • get
  • describe
  • delete
  • explain

Cheatsheet

kubectl get pods -A

Resources

kubectl get resource-type # pods,  ns etc
kubectl describe crd elasticsearch

Services

Create manifest yaml from currently running service

kubectl get svc wso2apim-service -o yaml

Endpoints

kubectl get endpoints

Delete

Delete namespace

kubectl delete ns elastic-system

Delete all resources of a name space

kubectl delete all --all -n default

Delete deployment

kubectl delete deploy name # kubectl get deploy

Managing node

ref

Mark node as unschedulable

kubectl cordon NODE

Mark node as schedulable

kubectl uncordon NODE

Evict pods

kubectl drain NODE
# to undo
kubectl uncordon NODE

Procedures

To delete a pod or deployment

  1. cordon: makes nodes unschedulable
  2. drain: deletes pods
  3. kubectl delete -f manifest.yaml

Debugging

Exec into the container

kubectl exec -it name-of-the-pod -- /bin/sh (command name)

Logs

kubectl logs -f pod-name
kubectl -n elastic-system logs -f statefulset.apps/elastic-operator # name of operator

Rollout status

kubectl rollout status deployment deployment-name # kubectl get deployments

Logs of pod before crashing

kubectl logs -f quickstart-es-default-0 -p #-p does the trick
import os
print(os.env)

Run busybox image temporarily

the pod will be removed once you are out of the pod shell.

kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox sh

Run a command inside a pod

kubectl exec shell-demo -- ps aux
kubectl exec shell-demo -- ls /
kubectl exec shell-demo -- cat /proc/1/mounts

Frequently used

  1. Service Port forwarding

kubectl port-forward svc/my-release-prometheus-server 8088:80 # host:service port