Basic kubectl commands

⚠️ Important: Always add the namespace to be sure you are not touching unnexpected pods. eg: -n bones-production ⚠️

Get all pods: get pods

eg: `kubectl -n bones-production get pods`

Options:

  • No headers: --no-headers
  • Display only certain column (eg: Name column): -o custom-columns=":metadata.name"
  • Display only certain pod status:
    • eg: Running: --field-selector status.phase=Running
    • eg: not Running: --field-selector status.phase!=Running
    • eg: Failed: --field-selector status.phase=Failed

Get all CronJobs pods

eg: `kubectl get cronjob`

Get an specific pod info: describe pods

eg: `kubectl -n bones-production describe pods <choosen pod>`

Get the logs of pod: logs

eg: `kubectl -n bones-production logs <choosen pod>`

Delete a pod: delete

eg: `kubectl delete -n bones-production pod <choosen pod>`

Combinations

Combining the get pods and awk command, we can easily filter the pods we want to delete in one single command:

eg: Delete all pods that contains `bones-scheduler` in its name:
  `kubectl -n bones-production get pods --no-headers=true | awk '/bones-scheduler/{print $1}' | xargs kubectl delete -n bones-production pod`
eg: Delete all Failed pods:
  `kubectl -n bones-production get pods --no-headers=true -o custom-columns=":metadata.name" --field-selector status.phase=Failed | xargs kubectl delete -n bones-production pod`

Connect to a pod: exec

eg: `kubectl exec -it -n bones-production <choosen pod> -- /bin/bas

Create manually a pod: create job

eg: Create a CronJob: `kubectl create job -n bones-production --from=cronjob/<name of cronjob> <name of job>`