home
kubernetes cheat sheet
Automated container deployment, scaling and management
print

kubernetes version: 1.22 - Date: June 2022

Kubectl Autocomplete

Setup autocomplete in bash apt install bash-completion source <(kubectl completion bash)

Context and Configuration

Show kubeconfig settings kubectl config view

Set the default context (cluster) kubectl config use-context CLUSTER-NAME

Creating Objects

Create resource(s) kubectl apply -f FILE.yaml [FILE2.yaml]

Create resource(s) from a directory kubectl apply -f PATH/DIR/

Create resource(s) from url kubectl apply -f https://URL

Run a single pod kubectl run NAME --image=IMAGE

Run a single pod and connect into it
kubectl run --ti NAME --image=IMAGE

Viewing, Finding Resources

List main resources kubectl get all

List all services in the namespace kubectl get services [--namespace NS]

List all pods in all namespaces kubectl get pods --all-namespaces

List a particular deployment kubectl get deployment DEPLOYMENT

Get the version label of all pods with label version=v1

kubectl get pods --selector=version=v1

Describe commands with verbose output kubectl describe nodes NODE kubectl describe pods POD

Updating Resources

Rolling update kubectl rolling-update POD ARGS

  • Rolling update pods of app-v1: kubectl rolling-update app-v1 -f app-v2.yaml

  • Change the name of the resource and update the image: kubectl rolling-update app-v1 app-v2 --image=image:v2

Force replace, delete and then re-create the resource. Will cause a service outage. kubectl replace --force -f POD.json


Editing Resources

Edit a resource in text editor kubectl edit TYPE/RESOURCE

  • Edit the service named app: kubectl edit svc/app

Scaling Resources

Scale a resource kubectl scale --replicas=NUM RESOURCE

  • Scale a deployment named foo to 3: kubectl scale --replicas=3 deployment/foo

  • Scale a resource specified in foo.yaml to 3: kubectl scale --replicas=3 -f foo.yaml

Deleting Resources

Delete a ressource kubectl delete TYPE RESOURCE

  • Delete pods and services with same names baz and foo: kubectl delete pods,service baz foo

  • Delete pods and services with label name=myLabel: kubectl delete pods,services -l name=myLabel

  • Delete all pods and services in namespace my-ns: kubectl -n my-ns delete pods,svc --all

  • Delete a pod using the type and name specified in pod.yaml: kubectl delete -f ./pod.yaml

Interacting with running Pods

Dump pod logs (stdout) kubectl logs POD

Stream pod logs (stdout) kubectl logs -f POD

Run pod as interactive shell (example with busybox) kubectl run -i --tty busybox --image=busybox -- sh

Attach to running container kubectl attach POD -i

Forward port 6000 of Pod to 5000 on your local machine kubectl port-forward POD 5000:6000

Run command in existing pod kubectl exec POD -- COMMAND

Interacting with Nodes and Cluster

Mark node as unschedulable kubectl cordon NODE

Drain node in preparation for maintenance kubectl drain NODE

Mark node as schedulable kubectl uncordon NODE

Show metrics for a given node kubectl top node NODE

Display addresses of the master and services kubectl cluster-info