Logo

dev-resources.site

for different kinds of informations.

My K8s Cheatsheet

Published at
1/12/2024
Categories
kubernetes
cheatsheet
cmd
devops
Author
barbara
Categories
4 categories in total
kubernetes
open
cheatsheet
open
cmd
open
devops
open
Author
7 person written this
barbara
open
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

kn(){
kubectl config set-context --current --namespace="$1"
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

  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

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


k --help

https://kubernetes.io/docs/home/

https://killercoda.com/killer-shell-ckad/

cmd Article's
30 articles in total
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
TryHackMe | Windows Command Line | RSCyberTech
Favicon
50+ Most Useful CMD Commands to Boost Your Windows Productivity
Favicon
Video: Enable IIS using CMD and PowerShell
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
🚀 Arch Linux Cheat Sheet: Essential Commands for new Users
Favicon
RIME Input | curl: (6) Could not resolve host: raw.githubusercontent.com
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! 🚀
Favicon
Maximizing IT Service Excellence with ServiceNow CMDB
Favicon
🍑understanding windows Command Line Interface
Favicon
Comandos Avanzados
Favicon
Comandos de Red
Favicon
Comandos de Fecha
Favicon
Comandos para Manipular Archivos y Directorios
Favicon
Tech notes 02 - Most Important Command Line Notes
Favicon
Comandos Básicos
Favicon
Bash Scripting Fundamentals
Favicon
Decoding the Linux Command Line: 75 Indispensable Utilities Explained
Favicon
How to restore a Mysql backup with XAMP
Favicon
Remotely Control Raspberry Pi via SSH from External Network
Favicon
SSH Raspberry Pi via Cell Phone
Favicon
Enhancing Internet Speed Through CMD Commands
Favicon
Install Oh-My-Posh On Windows Command Prompt (cmd) Via Clink
Favicon
Important CMD Commands
Favicon
Title: A Beginner's Guide to Command-Line File and Directory Manipulation
Favicon
Mastering Deployments in Kubernetes
Favicon
SetEnv 工具程式
Favicon
My K8s Cheatsheet
Favicon
Membuat Database Melalui CMD Pada Laragon
Favicon
Perbedaan perintah RUN dan CMD di dalam Docker

Featured ones: