Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Last active October 19, 2022 12:48
Show Gist options
  • Save budiantoip/50f88fe2d82994a15d485b707ce94a3a to your computer and use it in GitHub Desktop.
Save budiantoip/50f88fe2d82994a15d485b707ce94a3a to your computer and use it in GitHub Desktop.
Kubernetes Cheatsheet

Playlist :

References:

kubectl commands

Submit config file to kubernetes cluster

kubectl create -f filepod.yaml

1. Pod

Check the existing pod

kubectl get pod

Check the existing pod, plus display more information

kubectl get pod -o wide

Get pod details

kubectl describe pod podname

Temporarily access a pod

kubectl port-forward podname AccessPort:PodPort

Delete a pod

kubectl delete pod podname

Delete multiple pods

kubectl delete pod podname1 podname2 podname3

Delete pods based on specific labels

kubectl delete pod -l environment=development

Delete all pods within a namespace, but the namespace itself will remain intact

kubectl delete pod --all --namespace namespacename

Delete all pods within the "default" namespace, but the namespace itself will remain intact

kubectl delete pod --all

Some notes for YAML file

  1. Label values cannot contain space characters

2. Labels

Check the existing pod, plus their labels

kubectl get pod --show-labels

add a new label

kubectl label pod podname key=value

modify an existing label

kubectl label pod podname key=value --overwrite

Query a label

kubectl get pod -l key
kubectl get pod -l key-value
kubectl get pod -l '!key'
kubectl get pod -l key!=value
kubectl get pod -l 'key in (value1,value2)'
kubectl get pod -l 'key notin (value1,value2)'

Query multiple labels

kubectl get pod key1,key2=value
kubectl get pod key1=value,key2=value

3. Annotation

  1. Almost like label, but cannot be filtered
  2. Used to store lots of information
  3. Can store information up to 256 KB

add a new annotation

kubectl annotate pod podname key=value

modify an existing annotation

kubectl annotate pod podname key=value --overwrite

4. Namespace

When to use namespace?

  • When the resources in kubernetes are too many
  • When we need to separate multi-tenant resources, team or environment
  • Resources can have same names but within different namespaces

Check the existing namespace

kubectl get namespaces
kubectl get namespace
kubectl get ns

Get pod on a specific namespace

kubectl get pod --namespace namespacename
kubectl get pod -n namespacename

to submit yaml file to a specific namespace

kubectl create -f pod.yaml --namespace namespacename

delete a namespace, and automatically remove any resources within it

kubectl delete namespace namespacename

5. Probe / checking in kubernetes

  • Probe here mainly focusing on liveness, readiness, and startup probe in kubernetes
  • kubelet uses liveness probe to check when to restart the pods for example, when the liveness probe on a pod does not respond, it will then restart the pod automatically
  • kubelet uses readiness probe to check whether a pod is ready to accept incoming traffic
  • kubelet uses startup probe to check whether a pod has run or not, if not yet, then kubelet will not check for liveness and readiness
  • startup probe is suitable for pods having long startup time
  • Probe checking mechamism: HTTP Get, TCP Socket, Command Exec

Probe parameters

  • initialDelaySeconds: seconds needed to wait for the initial container start, default 0
  • periodSeconds: seconds needed to wait between check intervals, default 10
  • timeoutSeconds: seconds needed to wait for a container to respond before flagging it as a timeout, default 1
  • successThreshold: number of retries to be considered as a successful retry, default 1
  • failureThreshold: number of retries to be considered as a failed retry, default 3

to find out the reason of a restart

kubectl describe pod podname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment