In this cheatsheet I summed up the most used commands.
In doubt you can always consult
kubectl --help
- The K8s documentation or play around out on killercoda
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
kn(){
kubectl config set-context --current --namespace="$1"
}
call it like: kn crazynamespace
Run a command from a temp container
tmp(){
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
Pod
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
Serviceaccount
k create serviceaccount yourServiceAccount
add to pod
kind: Pod
metadata:
name: yourpod
namespace: yourns
spec:
serviceAccountName: yourServiceAccount
Secrets
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
Configmaps
k create configmap myConfigmap --from-literal key=value $do > configmap.yaml
k create configmap myconfigmap --from-file=path/to/file $do > configmap.yaml
Clusterrole
k create clusterrole myclusterrole --verb=get, list, create, delete --resource=tralala
Clusterrolebinding
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
Patch
// 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"
Networking
Expose
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
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
- deploy both
- switch version
- scale down deploy1
- 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
Storage
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
Troubleshooting
try to call outside:
k exec frontend-789cbdc677-c9v8h -- wget -O- www.google.com
check if env variables exist in a pod
k exec pod1 -- env | grep "<key>=<value>"
check if volume is mounted
k exec pod1 -- cat /path/to/mount
PODMAN
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
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
Top comments (2)
Thank you for sharing 🤗
You have some that I don't have yet, and I have some that you don't have yet, if you wanna check : dev.to/zenika/kubernetes-a-pragmat...
Wow - Thank you for sharing, I like the structure of your post.
It is a very useful help for my ongoing work with K8s.
Bookmarked. 😎.