kubectl - kubernetes control Tips & tricks # get all common resources (pod, svc, sts) kubectl get all # shortcut to target pods kubectl get po # == kubectl get pods # shortcut to target namespace kubectl get ns # == kubectl get namespaces # shortcut to target replicasets kubectl get rs # == kubectl get replicasets # shortcut to target daemonsets kubectl get ds # == kubectl get daemonsets # shortcut to target services kubectl get svc # == kubectl get services # shortcut to target configmaps kubectl get cm # == kubectl get configmap # shortcut to target statefulsets kubectl get sts # == kubectl get statefulset Help # access to resource documentation kubectl explain pods # use json path to go further kubectl explain pods.spec.containers # list all supported resources kubectl api-resources # access to the list of resource specifications # each resource can be used in the YAML description # e.g. pods.spec.initContainers will be written in the # pod descriptor: # ```yaml # spec: # initContainers: # - name: foobar # ``` kubectl explain <resource_name>.spec kubectl explain pods.spec # nuke all kubectl delete all --all # get multiple resources in one line # e.g. get pods and replicasets kubectl get po,rs Pod management # start a pod kubectl run <pod_name> --image <image_name> --port <port> # list pods kubectl get po # get pods' metadata kubectl describe po # execute command in pod kubectl exec -it <pod_name> -- <command> # execute shell command on specific container kubectl exec -it <pod_name> -c <container_name> -- <command> # delete a pod kubectl delete po <pod_name> # generate k8s pod description file kubectl run <pod_name> --image <image_name> --port <port> --dry-run client -o yaml > <pod_name>.yml # get pod ip kubectl get -o go-template --template='{{.status.podIP}}' pod whoami # show pod labels kubectl get po --show-labels # add labels kubectl label pod <pod_name> <label_name>=<label_value> # update a label kubectl label pod <pod_name> <label_name>=<label_value> --overwrite # get pods with their labels displayed in columns kubectl get po --label-columns label1,label2 # get pods that have the label 'foobar' with value 'barfoo' kubectl get po --selector foobar=barfoo kubectl get po -l foobar=barfoo # get pods that do not have the label 'foobar' kubectl get po --selector '!foobar' # get pods that have label 'run' with value 'foo' or 'bar' and 'release' label is not specified kubectl get po --selector 'run in (foo,bar),!release' # get all annotations kubectl get po -o json <pod_name> | jq .metadata.annotations # get pod name kubectl get po -l app=front -o name # add annotation to a pod kubectl annotate po <pod_name> <annotation_name>=<annotation_value> # read logs # <container_name> mandatory if the pod has multiple containers kubectl logs <pod_name> -c <container_name> # follow kubectl logs -f <pod_name> kubernetes nodes management # get nodes kubectl get nodes # add label kubectl label node <node_name> <label_name>=<label_value> ReplicaSet management # get replicasets kubectl get rs # change scaling kubectl scale rs <rs_name> --replicas=<N> # delete a replicaset, it will also delete the associated pods! kubectl delete rs <rs_name>