Skip to main content

Kubernetes Cheatsheet

Docs Version Dropdown

CREATING#

create resource(s)

kubectl apply -f ./my-manifest.yaml

create from multiple file

kubectl apply -f ./my1.yaml -f ./my2.yaml

create resource(s) in all manifest files in dir

kubectl apply -f ./dir

start a single instance of nginx

kubectl create deployment nginx --image=nginx

create a Job which prints "Hello World"

kubectl create job hello --image=busybox -- echo "Hello World"

create a CronJob that prints "Hello World" every minute

kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World"

VIEWING#

List all services in the namespace

kubectl get services

List all pods in all namespaces

kubectl get pods --all-namespaces

List all pods in the current namespace, with more details

kubectl get pods -o wide

Describe commands with verbose output

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

List Services Sorted by Name

kubectl get services --sort-by=.metadata.name

List pods Sorted by Restart Count

kubectl get pods --sort-by='. status.containerStatuses[0].restartCount'

List PersistentVolumes sorted by capacity

kubectl get pv --sort-by=.spec.capacity.storage

Get the version label of all pods with label app=cassandra

kubectl get pods --selector=app=cassandra -o \
jsonpath='{.items[*].metadata.labels.version}'

Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied

kubectl diff -f ./my-manifest.yaml

INTERACTING WITH RUNNING PODS#

stream pod logs (stdout)

kubectl logs -f my-pod

stream pod container logs (stdout, multi-container case) ku

kubectl logs -f my-pod -c my-container

stream all pods logs with label name=myLabel (stdout)

kubectl logs -f -l name=myLabel --all-containers

dump pod logs, with label name=myLabel (stdout)

kubectl logs -l name=myLabel

dump pod logs (stdout) for a previous instantiation of a container

kubectl logs my-pod --previous

Attach to Running Container

kubectl attach my-pod -i

Listen on port 5000 on the local machine and forward to port 6000 on my-pod

kubectl port-forward my-pod 5000:6000

Run command in existing pod (1 container case)

kubectl exec my-pod -- ls /

Interactive shell access to a running pod (1 container case)

kubectl exec --stdin --tty my-pod -- /bin/sh

Run command in existing pod (multi-container case)

kubectl exec my-pod -c my-container -- ls /

Interactive shell access to a running pod (1 container case)

kubectl exec --stdin --tty my-pod -- /bin/sh

Run command in existing pod (multi-container case)

kubectl exec my-pod -c my-container -- ls /

Show metrics for a given pod and its containers

kubectl top pod POD_NAME --containers

UPDATING#

Rolling update "www" containers of "frontend" deployment, updating the image

kubectl set image deployment/frontend www=image:v2

Check the history of deployments including the revision

kubectl rollout undo deployment/frontend

Rollback to the previous deployment

kubectl rollout history deployment/frontend

Rollback to a specific revision

kubectl rollout undo deployment/frontend --to-revision=2

Replace a pod based on the JSON passed into std

cat pod.json | kubectl replace -f -

Add a Label

kubectl label pods my-pod new-label=awesome

Add an annotation

kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq

Auto scale a deployment "foo"

kubectl autoscale deployment foo --min=2 --max=10

COPY FILES AND DIRECTORIES TO AND FROM CONTAINERS#

Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the current namespace

kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir

Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container

kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container

Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my

kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar

Copy /tmp/foo from a remote pod to /tmp/bar locally

kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar

SCALING RESOURCES#

Scale a replicaset named 'foo' to 3

kubectl scale --replicas=3 rs/foo

Scale a resource specified in "foo.yaml" to 3

kubectl scale --replicas=3 -f foo.yaml

If the deployment named mysql's current size is 2, scale mysql to 3

kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

Scale multiple replication controllers

kubectl scale --replicas=5 rc/foo rc/bar rc/baz

PATCHING RESOURCES#

Partially update a node

kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'

Update a container's image; spec.containers[*] .name is required because it's a merge key

kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

Update a container's image using a json patch with positional arrays

kubectl patch pod valid-pod --type='json'
-p='[{"op": "replace", "path": "/spec/containers/0/image",
"value":"new image"}]'

Disable a deployment livenessProbe using a json patch with positional arrays

"path": "/spec/template/spec/containers/0/livenessProbe"}]'

Add a new element to a positional array

kubectl patch sa default --type='json' -p='[{"op": "add",
"path": "/secrets/1", "value": {"name": "whatever" } }]'

DELETING RESOURCES#

Delete a pod using the type and name specified in pod.json

kubectl delete -f ./pod.json

Delete a pod with no grace period

kubectl delete pod unwanted --now

Delete pods and services with same names "baz" and "foo"

kubectl delete pod,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 pod,svc --all

Delete all pods matching the awk pattern1 or pattern2

kubectl get pods -n mynamespace --no-headers=true |
awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod