Skip to content

Kubernetes Spiekbriefje (Cheat Sheet)

Kubernetes, vaak afgekort als K8s (waarbij de “8” de acht letters in “ubernete” vervangt), is een open-source systeem voor het beheer van gecontaineriseerde applicaties op meerdere hosts in een cloudplatform. Het doel van Kubernetes is om het implementeren van gecontaineriseerde applicaties eenvoudig en efficiënt (krachtig) te maken. Kubernetes biedt mechanismen voor applicatie-implementatie, scheduling, updates en onderhoud.

Resource-informatie weergeven

Nodes

Resourcenaam: nodes, Afkorting: no

$ kubectl get no          # Toon alle nodes
$ kubectl get no -o wide  # Toon meer informatie over alle nodes
$ kubectl describe no     # Toon node details
$ kubectl get no -o yaml  # Toon node details in YAML-formaat
$ kubectl get node --selector=[label_name] # Filter nodes op specifiek label
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Uitvoer van veldinformatie gedefinieerd door jsonpath-expressie
$ kubectl top node [node_name] # Toon node verbruik (CPU/Geheugen/Opslag)

Pods

Resourcenaam: pods, Afkorting: po

$ kubectl get po 				# Toon alle pods
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# Bekijk pod labels
$ 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 
# Exporteer pod-informatie naar een YAML-bestand
$ kubectl get pods --field-selector status.phase=Running 		
# Filter pod-informatie met behulp van veld-selectoren

Namespaces

Resourcenaam: namespaces, Afkorting: ns

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

Deployments

Resourcenaam: deployments, Afkorting: deploy

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

Services

Resourcenaam: services, Afkorting: svc

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

DaemonSets

Resourcenaam: daemonsets, Afkorting: 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

Events

Resourcenaam: events, Afkorting: ev

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

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

Service Accounts

Resourcenaam: serviceaccounts, Afkorting: 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

Resourcenaam: replicasets, Afkorting: rs

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

Roles

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

Secrets

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

ConfigMaps

Resourcenaam: configmaps, Afkorting: cm

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

Ingresses

Resourcenaam: ingresses, Afkorting: ing

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

PersistentVolumes

Resourcenaam: persistentvolumes, Afkorting: pv

$ kubectl get pv 
$ kubectl describe pv

PersistentVolumeClaims

Resourcenaam: persistentvolumeclaims, Afkorting: pvc

$ kubectl get pvc
$ kubectl describe pvc

StorageClasses

Resourcenaam: storageclasses, Afkorting: sc

$ kubectl get sc
$ kubectl get sc -o yaml

Meerdere resources

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

Resource-kenmerken wijzigen

Taints

$ kubectl taint [node_name] [taint_name]

Labels

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

Cordon / Uncordon

$ kubectl cordon [node_name]   # Node in onderhoud (niet inplanbaar)
$ kubectl uncordon [node_name] # Node weer inplanbaar maken

Nodes leegmaken (Draining Nodes)

$ kubectl drain [node_name]    # Node leegmaken

Nodes / Pods

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

Deployments / Namespaces

$ 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]

Services

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

DaemonSets

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

Service Accounts

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

Annotaties

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

Resources toevoegen

Pods maken

$ 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

Services maken

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

Deployments maken

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

Interactie met container

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

YAML-bestanden uitvoeren

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

Help verkrijgen

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

Verzoeken

API Calls

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

Cluster Informatie

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