k8s Cheat Sheet
k8s Cheat Sheet
com/ sheet
@k u b esk ills v1.24
set alias for commands below al i as k =k ubec t l
ex por t KUBECONFI G=c onf i g1: c onf i g2 use two kube config files simultaneously k ubeadm c onf i g pr i nt i ni t - def aul t s list default values kubeadm uses for cluster init
k c l us t er - i nf o view control plane and etcd information k ubeadm t ok en c r eat e - - pr i nt - j oi n- c ommand print the join command to add more nodes (sudo)
k get c omponent s t at us get system health k ubeadm t ok en l i s t list tokens that haven't expired
k api - r es our c es list available resources k ubeadm t ok en gener at e generate a new token to authenticate with cluster
k api - r es our c es - - names pac ed=t r ue list namespaced resources k ubeadm upgr ade pl an plan the upgrade of control plane components
k get - - r aw \ Get metrics data on nodes in the k8s cluster (replace k ubeadm upgr ade node upgrade local kubelet
/ api s / met r i c s . k 8s . i o/ v 1bet a1/ nodes nodes with pods to get pod metrics) k ex ec k ube- api s er v er - pod - n k ube- s y s t em - - view enabled admission controllers (can also look in
k ube- api s er v er - h | gr ep / etc/ kubernetes/ manifests/ kube-apiserver.yaml)
k get ev ent s - A list events in the all namespace enabl e- admi s s i on- pl ugi ns
k del et e ns r obot - s hop delete the namespace named 'robot-shop' k c onf i g us e- c ont ex t gk eCl us t er set the default context to 'gkeCluster'
k c onf i g get - c ont ex t s list available contexts from kube config k c onf i g del et e- c l us t er doc k er - des k t op remove the cluster 'docker-desktop' from kubeconfig
NODES
k get no list nodes in the default namespace k get no - o j s on | j q ' . i t ems [ ] . s pec . t ai nt s ' view taints on all nodes in the cluster
k get no - o wi de list nodes with IP address & runtime info k get nodes - o j s onpat h=" { r ange view taints on nodes including node names
. i t ems [ * ] } { . met adat a. name}
k get no - l list nodes with the label 'kubernetes.io/ control-plane' { . s pec . t ai nt s [ ?( @. ef f ec t ==' NoSc hedul e' ) ]
node- r ol e. k uber net es . i o/ c ont r ol - pl ane (k get no --show-labels to show labels) . ef f ec t } { \ " \ n\ " } { end} "
k des c r i be no describe all nodes in the cluster k t ai nt no my node1 taint node 'mynode1' to not schedule pods to it
node- r ol e. k uber net es . i o/ mas t er : NoSc hedul e
k l abel no my node1 di s k =s s d label node 'mynode1' with 'disk=ssd'
k t ai nt no my node1 taint node with key 'dedicated', value 'special-user'
k get no - - s how- l abel s shot labels on all nodes in the cluster dedi c at ed=s pec i al - us er : NoSc hedul e and effect 'NoSchedule'
k annot at e no my node1 az ur e=node add the annotation 'azure=node' to node 'mynode1' k t ai nt no my node1 dedi c at ed: NoSc hedul e- take away taint which allows pods to be scheduled
k get nodes - o list external IP addresses of all nodes in the default k t ai nt no my node1 dedi c at ed- take away taint key 'dedicated' from node
j s onpat h=' { i t ems [ * ] . s t at us . addr es s es [ ? namespace
( @. t y pe==" Ex t er nal I P" ) ] . addr es s es } ' k des c r i be no | gr ep Tai nt describe nodes and filter out the word 'Taint'
k get no - o list the first node only within the list of nodes in the k t ai nt no - l di s k =s s d taint node with label 'disk=ssd' and key 'dedicated'
j s onpat h=' { . i t ems [ 0] . met adat a. name} ' cluster dedi c at ed=my node1: Pr ef er NoSc hedul e
k t op node my node1 view cpu and memory metrics for node 'mynode1' k t ai nt no my node1 bar : NoSc hedul e taint node with key 'bar' and no value
ht t ps: / / sek8s. com/ sheet
@k u b esk ills v1.24
set alias for commands below al i as k =k ubec t l
NODES (cont.)
k dr ai n my node1 - - i gnor e- daemons et s - - f or c e remove pods from node 'mynode1' ignore daemonsets k del et e no my node1 delete node 'mynode1' from the cluster
k c or don my node1 set 'mynode1' to unschedulable (cordon) k edi t no my node1 edit the node configuration
k unc or don my node1 set node 'mynode1' to schedulable (uncordon)
PODS
k r un ngi nx - - i mage=ngi nx create pod using image 'nginx' k annot at e po ngi nx s pec i al =app1 annotate pod with key 'special' and value 'app1'
k r un bus y box - - i mage=bus y box - i t create a pod with image 'busybox' and get a shell to it k get po ngi nx - o y aml show the YAML configuration for pod named 'nginx'
k r un c ur l pod - - i mage=ni c ol ak a/ net s hoot - - r m create pod and get a shell to it when exiting the shell k get pod ngi nx - o y aml - - ex por t > export the YAML for a pod to a file named
- i t - - sh will delete the pod podc onf i g. y ml 'podconfig.yml'
k r un dns t ool s - - i mage i nf obl ox / dns t ool s - - r m create pod with image 'infoblox/ dnstools' and run k get po - - f i el d- s el ec t or list pods that are running only
- i t - - bas h bash s t at us . phas e=Runni ng
k r un debug- pod - - i mage=bus y box - i t describe the namespace configuration k ex ec ngi nx env list environment variables for pod named 'nginx'
k r un ngi nx - - i mage=ngi nx - - dr y - r un=c l i ent - o create the YAML file 'pod.yml' for a pod with image k r un c ur l - - i mage=ni c ol ak a/ net s hoot - - r m - i t create a pod and cat out the 'resolv.conf' file and then
y aml > pod. y ml 'nginx' - - r es t ar t =Nev er - - c at / et c / r es ol v . c onf delete the pod upon exit
k get po list all pods in the default namespace k r un net s hoot - - i mage=ni c ol ak a/ net s hoot create a pod and run the command 'sleep 3600' inside
- - c ommand s l eep - - c ommand " 3600" the container
k get po - A - w tail the pods in all namespaces
k ex ec ?t ngi nx ? c at / et c / r es ol v . c onf cat the 'resolv.conf' file (dns info) on running pod
k wai t - - f or =c ondi t i on=r eady pod - l app=ngi nx tail the pods until pod with label 'app=nginx' is ready
k l ogs ngi nx get the logs for pod named 'nginx'
k get po - - al l - names pac es list pods in all namespaces
k l ogs - l app=ngi nx get the logs for all pods that have label 'app=nginx'
k get pods - A - o wi de - - f i el d- s el ec t or list running pods that reside on the node named
s pec . nodeName=c ont r ol - pl ane k l ogs ngi nx > pod. l og output the logs to file 'pod.log' for pod named 'nginx'
'control-plane'
k get al l - A list all cluster resources in all namespaces k l ogs ngi nx - - s i nc e=1h get the logs from pod 'nginx' forom the last hour
k get po, no, s v c - A list pods, nodes and services in all namespaces k l ogs ngi nx - - t ai l =20 get the last 20 lines from the logs from pod 'nginx'
k get po - o wi de list pods with IP addresses and node assignments k l ogs - f ngi nx - c l og stream the logs for container 'named 'log'
k des c r i be po describe all pods in the default namespace k l ogs ngi nx - f stream (follow) the logs from pod 'nginx'
k l abel ngi nx app=pr od add label 'app=prod' on the pod named 'nginx' k del et e po ngi nx delete pod named 'nginx'
k get po - - s how- l abel s show labels on all pods in the default namespace k edi t po ngi nx edit the pod configuration for pod named 'nginx'
k get po - l app=ngi nx list pods that have the label 'app=nginx' k por t - f or war d ngi nx 8080: 80 port forward from 80 on container to 8080 on host
k ex ec - i t mongodb mongo run the command 'mongo' on pod named 'mongo' k por t - f or war d el as t i c s ear c h- pod 9200: 9200 & port forward from 9200 and run in background (&)
k ex ec - i t my pod - c c ar t - - / bi n/ bas h get a shell to container 'cart' in pod named 'mypod' c ur l - - head ht t p: / / l oc al hos t : 9200 curl the forwarded port from control plane node
k ex ec - i t bux - - s h - c " whi l e t r ue; do ec ho run command in a running pod named 'bux' to echo
hel l o; s l eep 2; done" 'hello' and sleep for 2 seconds on a loop
k get s v c - - s how- l abel s view labels applied to all services in default namespace k c r eat e i ng moo- i ng create ingress 'moo-ing' where all requests go to
- - r ul e=" moo. c om/ =mi l k - s v c : 80" service 'milk-svc' on port 80 but requests
k edi t s v c app- s er v i c e edit the service named 'app-service' - - r ul e=" moo. c om/ f l av or s =f l av or - s v c : 8080" 'moo.com/ flavors' go to 'flavor-svc' on port 8080
k del et e s v c app- s er v i c e delete servcie named 'app-service'
DAEMONSETS
k get ds list all daemonsets in default namespace k edi t ds k ube- pr ox y - n k ube- s y s t em edit 'kube-proxy' daemonset in kube-system ns
k get ds - - al l - names pac es list all daemonsets in all namespaces k edi t ds k ube- pr ox y - n k ube- s y s t em edit 'kube-proxy' daemonset in kube-system ns
k des c r i be ds k ube- pr ox y - n k ube- s y s t em describe 'kube-proxy' daemonset in kube-system ns k del et e ds k ube- pr ox y - n k ube- s y s t em delete daemonset named 'kube-proxy'
k get ds k ube- pr ox y - n k ube- s y s t em - o y aml get YAML output of 'kube-proxy' daemonset