James's Ramblings

Kubernetes: kubectl

Created: September 20, 2020 (Updated: April 06, 2021)

Documentation: kubectl Cheat Sheet

Quick command reference

Pods

Description Command
Get pods in the default namespace. kubectl get pods
Get pods in all namespaces. kubectl get pods –all-namespaces
Get a list of nodes. kubectl get nodes
Get a pod description. kubectl describe pod POD
Get a description of all pods. kubectl describe pods
Delete a pod. kubectl delete pod POD
Show labels on pods. kubectl get pods --show-labels
Add a label to a pod. kubectl label pods POD LABEL=VALUE

Nodes

Description Command
Get a description of a node. kubectl describe node NODE
Get a description of all nodes. kubectl describe nodes
Delete node. kubectl delete node NODE
Turn scheduling on a node off kubectl cordon NODE
Turn scheduling on a node on kubectl uncordon NODE

Namespaces

Description Command
Get a list of namespaces. kubectl get {namespaces or ns}
Create a namespace. kubectl create ns NAMESPACE
Get a description of a namespace. kubectl describe ns NAMESPACE
Delete a namespace. kubectl delete ns/NAMESPACE
Get the YAML of a namespace kubectl get ns/NAMESPACE -o yaml

Deployments

Description Command
List deployments kubectl get deployments
Get a description of a deployment. kubectl describe deployment DEPLOYMENT
Scale a deployment kubectl scale deploy/DEPLOYMENT --replicas=INT
Edit a deployment using a text editor kubectl edit deployment DEPLOYMENT
Roll out a new image to a deployment kubectl set image deployment/DEPLOYMENT image=IMAGE:VERSION --all
Roll back the last deployment kubectl rollout undo deployment/DEPLOYMENT
Roll back to a specific revision kubectl rollout undo deployment/DEPLOYMENT --to-revision=INT
Pause a rollout kubectl rollout pause deployment/DEPLOYMENT
Resume a paused rollout kubectl rollout resume deployment/DEPLOYMENT
See deployment history kubectl rollout history deployment/DEPLOYMENT

DaemonSets

Description Command
Get a list of daemonsets kubectl get daemonsets
Get a list of daemonsets kubectl get ds

Updating API objects

Description Command
Apply the contents of a YAML config file kubectl apply -f FILE.yaml
Edit a deployed YAML config file kubectl edit PATH/TO/FILE

Misc

Description Command
Get a list of services. kubectl get {svc or service}
Create a resource from a file. kubectl create -f FILE.{yaml or json}
View address of master and services. kubectl cluster-info
Show kubeconfig settings. show kubeconfig settings
View all resources. kubectl api-resources -o wide
Change Kubernetes context kubectl config use-context CONTEXT
Show the value of LABEL in get pods output. kubectl get pods -L LABEL
Annotate an object. kubectl annotate OBJECT_TYPE OBJECT_NAME KEY="VALUE"
Check access level kubectl auth can-i METHOD ACTION
View basic server config information kubectl config view
Configure credentials kubectl config set-credentials

Notes

  • The configuration file for kubectl is located at $HOME/.kube/config. Inside are cluster definitions, credentials, and contexts.

  • A context is essentially a profile. Changing context allows you to manage different Kubernetes clusters. Use kubectl config use-context CONFIG to change context.

kubectl describe node NODE

Shows:

  • CPU, memory, and other resource usage.
  • Requests.
  • Limits.
  • Capacity.
  • Pods allowed.
  • Current Pods.

Labels

  • One or more labels can be applied to objects at creation or after creation.
  • Labels are arbitrary key-value pairs.
  • Can be used to tag up resources and apply commands to the tagged resources.

Label commands

Description Command
Show labels on pods. kubectl get pods --show-labels
Add a label to a pod. kubectl label pods POD LABEL=VALUE
Show the value of LABEL in get pods output. kubectl get pods -L LABEL
Show pods that match a specific key/value pair. kubectl get pods -l LABEL=VALUE

Field Selectors

Kubernetes Documentation: Field Selectors

  • Field selectors can be used to filter the output of commands dependant on criteria.
  • Fields unsupported by an object type product the error:
    Error from server (BadRequest): FIELD is not a known field selector
    
  • Operators are =, ==, and !=. The first two operators are the same.

Field Selector examples

Get all running Pods:

kubectl get pods --field-selector status.phase=Running

Get all services not in the default namespace:

kubectl get services --all-namespaces --field-selector metadata.namespace!=default

Select all Pods that aren’t running but should always restart:

kubectl get pods --field-selector=status.phase!=Running,spec.restartPolicy=Always

Select all Statefulsets and Services that are not in the default namespace:

kubectl get statefulsets,services --all-namespaces --field-selector metadata.namespace!=default

Annotations

  • Annotations are notes or descriptions used to provide in-file documentation.
  • Annotations are key-value pairs and can be applied at object creation or retroactively.
kubectl annotate OBJECT_TYPE OBJECT_NAME KEY="VALUE"

Examples

Annotate all pods in a namespace:

$ kubectl annotate pods --all description='Production Pods' -n prod

Overwrite the annotation:

$ kubectl annotate --overwrite pod webpod description="Old Production Pods" -n prod

Delete the annotation:

$ kubectl -n prod annotate pod webpod description-

Useful commands

All pods in the default namespace with a custom view:

kubectl get pods -o custom-columns=POD:metadata.name,NODE:spec.nodeName --sort-by spec.nodeName -n kube-system

Watch as pods are created in the default namespace:

kubectl get pods -n kube-system -w

View system pods:

kubectl get pods -n kube-system

kubectl context

  • kubectl calls the kube-apiserver using curl commands.