Skip to content

Kubernetes Fusklapp (Cheat Sheet)

Kubernetes, ofta förkortat K8s (där “8” ersätter de åtta bokstäverna i “ubernete”), är ett open-source-system för hantering av containeriserade applikationer över flera värdar i en molnplattform. Målet med Kubernetes är att göra distributionen av containeriserade applikationer enkel och effektiv (kraftfull). Kubernetes tillhandahåller mekanismer för applikationsdistribution, schemaläggning, uppdateringar och underhåll.

Visa resursinformation

Noder (Nodes)

Resursnamn: nodes, Förkortning: no

$ kubectl get no          # Visa alla noder
$ kubectl get no -o wide  # Visa mer information om alla noder
$ kubectl describe no     # Visa detaljer om noder
$ kubectl get no -o yaml  # Visa detaljer om noder i YAML-format
$ kubectl get node --selector=[label_name] # Filtrera noder efter specifik etikett
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Visa fältinformation definierad av jsonpath-uttryck
$ kubectl top node [node_name] # Visa nodanvändning (CPU/Minne/Lagring)

Poddar (Pods)

Resursnamn: pods, Förkortning: po

$ kubectl get po 				# Visa alla poddar
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# Visa etiketter för poddar
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml 
# Exportera pod-information till en YAML-fil
$ kubectl get pods --field-selector status.phase=Running 		
# Filtrera pod-information med hjälp av fältselektorer

Namnrymder (Namespaces)

Resursnamn: namespaces, Förkortning: ns

$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns

Deploymenter (Deployments)

Resursnamn: deployments, Förkortning: deploy

$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide 
$ kubectl get deploy -o yaml

Tjänster (Services)

Resursnamn: services, Förkortning: svc

$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide 
$ kubectl get svc -o yaml
$ kubectl get svc --show-labels

DaemonSets

Resursnamn: daemonsets, Förkortning: ds

$ kubectl get ds
$ kubectl describe ds --all-namespaces
$ kubectl describe ds [daemonset_name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml

Händelser (Events)

Resursnamn: events, Förkortning: ev

$ kubectl get events 
$ kubectl get events -n kube-system
$ kubectl get events -w

Loggar (Logs)

$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log

Tjänstekonton (Service Accounts)

Resursnamn: serviceaccounts, Förkortning: sa

$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml

ReplicaSets

Resursnamn: replicasets, Förkortning: rs

$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide 
$ kubectl get rs -o yaml

Roller (Roles)

$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml

Hemligheter (Secrets)

$ kubectl get secrets
$ kubectl get secrets --all-namespaces 
$ kubectl get secrets -o yaml

ConfigMaps

Resursnamn: configmaps, Förkortning: cm

$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml

Ingresser (Ingresses)

Resursnamn: ingresses, Förkortning: ing

$ kubectl get ing
$ kubectl get ing --all-namespaces

Persistenta volymer (PersistentVolumes)

Resursnamn: persistentvolumes, Förkortning: pv

$ kubectl get pv 
$ kubectl describe pv

Krav på persistenta volymer (PersistentVolumeClaims)

Resursnamn: persistentvolumeclaims, Förkortning: pvc

$ kubectl get pvc
$ kubectl describe pvc

Lagringsklasser (StorageClasses)

Resursnamn: storageclasses, Förkortning: sc

$ kubectl get sc
$ kubectl get sc -o yaml

Flera resurser

$ kubectl get svc, po
$ kubectl get deploy, no
$ kubectl get all
$ kubectl get all --all-namespaces

Ändra resursattribut

Taints

$ kubectl taint [node_name] [taint_name]

Etiketter (Labels)

$ kubectl label [node_name] disktype=ssd 
$ kubectl label [pod_name] env=prod

Cordon / Uncordon

$ kubectl cordon [node_name]   # Markera nod som ej schemaläggningsbar
$ kubectl uncordon [node_name] # Markera nod som schemaläggningsbar

Tömma noder (Draining Nodes)

$ kubectl drain [node_name]    # Töm noden

Noder / Poddar

$ kubectl delete node [node_name] 
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]

Deploymenter / Namnrymder

$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]

Tjänster

$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]

DaemonSets

$ kubectl edit ds [ds_name] -n kube-system 
$ kubectl delete ds [ds_name]

Tjänstekonton

$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]

Anteckningar (Annotations)

$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]

Lägg till resurser

Skapa poddar

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never

Skapa tjänster

$ kubectl create svc nodeport [svc_name] --tcp=8080:80

Skapa deploymenter

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx

Interaktion med container

$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh

Utdata YAML-filer

$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml --export > pod.yaml

Få hjälp

$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec

Begäranden

API-anrop

$ kubectl get --raw /apis/metrics.k8s.io/

Klusterinformation

$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus