Kubectl#

kubectl is the command-line tool for controlling Kubernetes clusters. The operator runs it from the bastion or from a compromised service account to navigate, manipulate, and tear down workloads.

Context#

$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2                  # use multiple kubeconfig files
$ kubectl config view                                           # show merged config
$ kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'   # password for e2e
$ kubectl config view -o jsonpath='{.users[].name}'             # first user
$ kubectl config view -o jsonpath='{.users[*].name}'            # list of users
$ kubectl config get-contexts                                   # list contexts
$ kubectl config current-context                                # current context
$ kubectl config use-context my-cluster-name                    # set default context
$ kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
$ kubectl config set-context --current --namespace=ggckad-s2    # set ns permanently
$ kubectl config set-context gce --user=cluster-admin --namespace=foo && kubectl config use-context gce
$ kubectl config unset users.foo                                # delete user

Create objects#

$ kubectl apply -f ./my-manifest.yaml
$ kubectl apply -f ./my1.yaml -f ./my2.yaml
$ kubectl apply -f ./dir
$ kubectl apply -f https://git.io/vPieo
$ kubectl create deployment nginx --image=nginx
$ kubectl explain pods,svc

View / find resources#

$ kubectl get services                                          # all services in namespace
$ kubectl get pods --all-namespaces                             # all pods, all namespaces
$ kubectl get pods -o wide                                      # detailed pods
$ kubectl get deployment my-dep                                 # specific deployment
$ kubectl get pods
$ kubectl get pod my-pod -o yaml
$ kubectl get pod my-pod -o yaml --export                       # without cluster info

$ kubectl describe nodes my-node
$ kubectl describe pods my-pod

$ kubectl get services --sort-by=.metadata.name
$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
$ kubectl get pv --sort-by=.spec.capacity.storage
$ kubectl get pods --selector=app=cassandra -o jsonpath='{.items[*].metadata.labels.version}'
$ kubectl get node --selector='!node-role.kubernetes.io/master'
$ kubectl get pods --field-selector=status.phase=Running
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
$ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
$ kubectl get events --sort-by=.metadata.creationTimestamp
$ kubectl diff -f ./my-manifest.yaml                            # compare cluster vs manifest

Updating resources#

$ kubectl set image deployment/frontend www=image:v2            # rolling update
$ kubectl rollout history deployment/frontend                   # rollout history
$ kubectl rollout undo deployment/frontend                      # rollback
$ kubectl rollout undo deployment/frontend --to-revision=2
$ kubectl rollout status -w deployment/frontend                 # watch rollout
$ kubectl rollout restart deployment/frontend                   # rolling restart

Scaling, editing, deleting.

$ kubectl autoscale deployment foo --min=2 --max=10
$ kubectl edit svc/docker-registry
$ KUBE_EDITOR="nano" kubectl edit svc/docker-registry
$ kubectl scale --replicas=3 rs/foo
$ kubectl scale --replicas=3 -f foo.yaml
$ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
$ kubectl scale --replicas=5 rc/foo rc/bar rc/baz
$ kubectl delete -f ./pod.json
$ kubectl delete pod,service baz foo
$ kubectl delete pods,services -l name=myLabel
$ kubectl -n my-ns delete pod,svc --all
$ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod

Interact pods#

$ kubectl logs my-pod                                           # dump logs (stdout)
$ kubectl logs -l name=myLabel                                  # dump logs by label
$ kubectl logs my-pod --previous                                # previous instance
$ kubectl logs my-pod -c my-container                           # specific container
$ kubectl logs -f my-pod                                        # stream logs
$ kubectl logs -f -l name=myLabel --all-containers              # stream logs by label
$ kubectl run -i --tty busybox --image=busybox -- sh            # interactive shell
$ kubectl run nginx --image=nginx --restart=Never -n mynamespace
$ kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml
$ kubectl attach my-pod -i
$ kubectl port-forward my-pod 5000:6000
$ kubectl exec my-pod -- ls /
$ kubectl exec my-pod -c my-container -- ls /
$ kubectl top pod POD_NAME --containers

Nodes and cluster#

$ kubectl cordon my-node                                        # mark unschedulable
$ kubectl drain my-node                                         # drain for maintenance
$ kubectl uncordon my-node                                      # mark schedulable
$ kubectl top node my-node
$ kubectl cluster-info
$ kubectl cluster-info dump
$ kubectl cluster-info dump --output-directory=/path/to/cluster-state

Resource types#

$ kubectl api-resources --namespaced=true
$ kubectl api-resources --namespaced=false
$ kubectl api-resources -o name                                 # resource names only
$ kubectl api-resources -o wide                                 # expanded output
$ kubectl api-resources --verbs=list,get
$ kubectl api-resources --api-group=extensions

References#