Often I see members of my team struggling to remember the somehow hard to remember commands for operating Kubernetes. Its quite hard to find a descent documentation that goes straight to the point. Not much lalala, but just give me the CMD I’m looking for.
This is a curated list that I’ve been updating periodically with many of the commands I use day to day on operations, so if you know anything that is not here, please let me know! 😀
Kubernetes DevOps day-to-day tasks and commands
Kubernetes – How to change the scope to another namespace
$ kubectl config set-context $(kubectl config current-context) --namespace=env-JIRA-1234
Kubernetes – How to List the active pods on a namespace:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
golang-api-1280745034-4s6jt 1/1 Running 0 17m
golang-api-1280745034-7knzq 1/1 Running 0 17m
``
Kubernetes – How to Get the logs for a POD:
$ kubectl logs golang-api-1280745034-4s6jt
Kubernetes – How to Get services the Load Balancer IP addresses for a Namespace:
$ kubectl get services --namespace=develop
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
(...)
Kubernetes – How to Get the ingres and services for a Namespace:
$ kubectl get ing,service --namespace=develop
Kubernetes – How to Get info about the services for a Namespace
$ kubectl describe services --namespace=develop
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
(...)
Kubernetes – How to Get info about the services for ALL Namespaces
$ kubectl get services --all-namespaces
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
(...)
Kubernetes – How to open a proxy to a POD and expose it locally OR (How to connect to a POD directly and expose it locally)
$ kubectl port-forward golang-api-1280745034-4s6jt 8888:3000
Kubernetes – How to configure kubectl to always point to same cluster
$ kubectl config set-context gke_apps_europe-west1-b_europa --namespace=prod
Kubernetes – How to Get the current context (The cluster kubectl is pointing to)
$ kubectl config current-context
Kubernetes – How to setup kubectl Auto Complete
$ source <(kubectl completion bash)
Kubernetes – How to check resource utilization (TOP) for all nodes (Cluster legs)
$kubectl top nodes
Kubernetes – How to check resource utilization (TOP) for all pods in all namespaces
$ kubectl top pods --all-namespaces
Kubernetes – How to WATCH constantly the resource utilization for all pods in a particular namespace and order the results so they don’t keep changing all the time.
$ watch "kubectl top pods --namespace=prod |sort"
Kubernetes – How to WATCH constantly the resource utilization for nodes in a particular namesmace (legs) and order the results so they don’t keep changing all the time.
$ watch "kubectl top nodes --namespace=prod |sort"
Kubernetes – How to WATCH constantly the resource utilization for all pods in ALL namespaces and order the results so they don’t keep changing all the time.
$ watch "kubectl top pods --all-namespaces |sort"
Kubernetes – How to kill all running jobs:
`kubectl delete jobs -l `
Kubernetes – How to SSH inside a node to execute commands
gcloud compute ssh gke-cluster-default-pool-123-4321s --zone europe-west1-c
Kubernetes – How to SSH inside the pod and perform a curl on the healthcheck endpoint. This is particular useful for troubleshot and assert that the service is really running and healthy.
$ kubectl exec -ti golang-api-1280745034-4s6jt curl http://localhost:3000/health`
Kubernetes – How to Change the configuration file ‘on the fly’
kubectl apply -f kubernetes/config/develop.yaml
Kubernetes – How to Restart all PODS in one go:
$kubectl delete pods -l app=golang-api --namespace=prod
Kubernetes – How to List all Environment variables for a particular POD
$ kubectl exec -ti golang-api-4135068084-3wpb5 env --namespace=prod
Kubernetes – How to Check health for a particular backend POD from inside the frontend pod in order to test that they connect with no issues
$ kubectl exec -ti front-end-355436834-whtnp curl http://golang-api/health --namespace=prod
Kubernetes – How to Check for all logs inside the main scheduler
$ journalctl -f
Kubernetes – How to List all pods and order them by how many times they have restarted. This is useful to detect memory leaks or debug the pods that are restarting more often.
$ kubectl get pods --sort-by=.status.containerStatuses[0].restartCount
Kubernetes – How to test that your API is returning something on a particular endpoint directly inside the POD (Useful when you deploy something but is not working .. you are pulling your hair out and hating kubernetes … keep call and curl it from inside 😀 )
$ kubectl exec -ti frontend-355436834-whtnp curl http://golang-api/find/1 --namespace=develop
Kubernetes – How to proxy a existing InfluxDB pod so you can use it locally:
$ kubectl port-forward --namespace monitoring $(kubectl get pods --namespace monitoring -l app=influxdb-influxdb -o jsonpath='{ .items[0].metadata.name }') 8086:8086
GCLOUD: How to list all images for your Docker eu.gcr.io:
$ gcloud container images list-tags eu.gcr.io/thomasmodeneis-apps/golang-api
HELM DevOps day-to-day tasks and commands
HELM – How to deploy a Docker image from eu.gcr.io to your cluster:
$ export NAMESPACE=develop
$ export VERSION=1.0.0
$ helm upgrade -i golang-api-$NAMESPACE ./helm_chart \
--namespace=$NAMESPACE
--values helm_values/develop.yaml
--set cluster=clustername,version=$VERSION
HELM – How to delete a chart and all its pods
helm delete redis-develop
HELM – How to delete a chart and all its pods for a specific namespace:
helm delete redis-develop --namespace=cache-develop
HELM – How to delete a chart and all its pods and purge it, in order to also delete PV and PVC’s associated to it: (Caution, this will delete all your data from stored disks).
helm delete redis-develop --purge
I hope it helps,
Happy opensource coding for everyone 😀