My K8s Cheatsheet


In this cheatsheet I summed up the most used commands.
In doubt you can always consult

General Helper

Add aliases and functions to the .bashrc, to save time avoid typing:

Aliases and Functions

Alias for kubernetes
alias k = 'kubectl'

Do a dry-run and output it as yaml
export do = "-o yaml --dry-run="client"

k create deployment test --image="nginx:alpine" $do > deployment.yaml

Do it immediately
export now = "--force --grace-period=0"

k delete deployment test $now

Set the namespace

kubectl config set-context --current --namespace="$1"

call it like: kn crazynamespace

Run a command from a temp container

 kubectl run tmp --image="nginx:alpine" -i -rm --restart=Never -- sh -c "$1"

call it like: tmp curl http://servicename:namespace:port

Kubectl commands

Get a configuration as .yaml

k get deployment -o yaml > depl.yaml
k get pod -o yaml > pod.yaml

k create deployment -o yaml > depl.yaml
k run pod1 $do > pod.yaml


Create a pod that has a command:

k run pod1 image=imagetouse --comand -- sh -c "commandlinecommand" $do > pod.yaml

Search pods in a namepspace for label

k get pod -o yaml | grep searchitem

Create a service for a pot

k expose podname --name=servicename --port=3333 --target-port=3333


k create serviceaccount yourServiceAccount

add to pod

kind: Pod
    name: yourpod
    namespace: yourns
    serviceAccountName: yourServiceAccount


k get secrets
k create secret generic mySecret --from-literal key=value
k create secret generic mySecret --from-file=path/to/file
k get secret -o jsonpath='{.data.yourKey}' | base64 decode > supersecret.txt


k create configmap myConfigmap --from-literal key=value $do > configmap.yaml

k create configmap myconfigmap --from-file=path/to/file $do > configmap.yaml


k create clusterrole myclusterrole --verb=get, list, create, delete --resource=tralala


k create clusterrolebinding my-cluster-role-binding --clusterrole=my-cluster-role --serviceaccount=default:my-service-account

k create sa admin-user
k create clusterrolebinding admin-user --clusterrole cluster-admin --serviceaccount kubernetes-dashboard:admin-user
k create token admin-user


// to add a selector to the created service
k patch service old-app -p ‘{“spec”:{“selector”:{“app”: “new-app”}}}’
–> you can patch anything, need to know the level

Label and Annotate

k label pod -l type=runner another=label
k annotate pod -l type=runner type="i am a great type"



k expose deployment example --port=8765 --target-port=9376
--name=example-service --type=LoadBalancer

k expose podname --name=servicename --port=3333 --target-port=3333 --type=Nodeport

Curl with temp pod to test

k run tmp --restart=Never --rm --image=nginx:alpine -i -- curl http://servicename.namespace:port


Rollouts and rollbacks

k get deploy
k rollout history
k undo deploy deploymentname

Rolling update

k scale deploy/dev-web --replicas=4
k edit deployment yourdeployment

Canary rollout

depl1: repl: 2
depl2: repl: 8

Green Blue deployment

  1. deploy both
  2. switch version
  3. scale down deploy1
  4. update service

Scale a deployment

k scale deployment/my-nginx --replicas=1
k autoscale deployment/my-nginx --min=1 --max=3
k get pods -l app=nginx


k create pvc name > pvc.yaml
k create pv name > pv.yaml

–> get pv and pvc at the same time to see if it is working
k get pv, pvc
–> status is bound, storageClass is manual -> everything is working
–> if Storage class needed:
to try:
k create sc yourStorageClass -o yaml --dry-run="client" > sc.yaml


try to call outside:

k exec frontend-789cbdc677-c9v8h — wget -O-

check if env variables exist in a pod

k exec pod1 -- env | grep "="

check if volume is mounted

k exec pod1 -- cat /path/to/mount


podman build -t super:v1
podman run --name my-container super:v1
podman save -o /path/to/output/myimage.tar super:v1
(podman uses oci format as default, docker does not)


helm repo
helm repo list
helm repo update
helm search repo whatever

helm -n yourns upgrade
helm -n yourns install currentthingi imageToTake --set replicaCount=2

k –help

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Create a Money-Lending App

Next Post

24 Essential Types of Charts for Data Visualization: Examples & Use Cases

Related Posts