Show Menu
Cheatography

Kubernetes Cheat Sheet (DRAFT) by

Kubernetes cheatsheet from https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Abbrev­iations

configmaps
cm
daemonsets
ds
endpoints
ep
event
ev
namespaces
ns
nodes
no
pods
po
replic­asets
rs
servic­eac­count
sa
services
svc

Creating Objects

kubectl create -f ./my-m­ani­fes­t.yaml
create resour­ce(s)
kubectl create -f ./my1.yaml -f ./my2.yaml
create from multiple files
kubectl create -f ./dir
create resour­ce(s) in all manifest files in dir
create resour­ce(s) from url
kubectl run nginx --imag­e=nginx
start a single instance of nginx
kubectl explain pods,svc
get the docume­ntation for pod and svc manifests
cat <<EOF | kubectl create -f - apiVer­sion: v1 kind: Secret metadata: name: mysecret type: Opaque data: password: $(echo "­s33­msi­4" | base64) username: $(echo "­jan­e" | base64) EOF
Create a secret with several keys

Scaling Resources

kubectl scale --repl­icas=3 rs/foo
Scale a replicaset named 'foo' to 3
kubectl scale --repl­icas=3 -f foo.yaml
Scale a resource specified in "­foo.ya­ml" to 3
 

Viewing, Finding Resources

kubectl get services
List all services in the namespace
kubectl get pods --all-­nam­espaces
List all pods in all namespaces
kubectl get pods -o wide
List all pods in the namespace, with more details
kubectl get deployment my-dep
List a particular deployment
kubectl describe nodes my-node
Describe nodes
kubectl describe pods my-pod
Describe pods

Editing Resources

kubectl edit svc/do­cke­r-r­egistry
Edit the service named docker­-re­gistry

Intera­cting with running Pods

kubectl logs my-pod
dump pod logs (stdout)
kubectl logs -f my-pod
stream pod logs (stdout)
kubectl run -i --tty busybox --imag­e=b­usybox -- sh
Run pod as intera­ctive shell
kubectl attach my-pod -i
Attach to Running Container
kubectl port-f­orward my-pod 5000:6000
Forward port 6000 of Pod to your to 5000 on your local machine
kubectl exec my-pod -- ls /
Run command in existing pod (1 container case)
kubectl exec my-pod -c my-con­tainer -- ls /
Run command in existing pod (multi­-co­ntainer case)

Intera­cting with Nodes and Cluster

kubectl cordon my-node
Mark my-node as unsche­dulable
kubectl drain my-node
Drain my-node in prepar­ation for mainte­nance
kubectl uncordon my-node
Mark my-node as schedu­lable
kubectl cluste­r-info
Display addresses of the master and services

Kubectl AutoCo­mplete

source <(kubectl completion bash)
 

Updating Resources

kubectl rollin­g-u­pdate fronte­nd-v1 -f fronte­nd-­v2.json
Rolling update pods of fronte­nd-v1
cat pod.json | kubectl replace -f -
Replace a pod based on the JSON passed into stdin
kubectl expose rc nginx --port=80 --targ­et-­por­t=8000
Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000
kubectl get pod mypod -o yaml | sed 's/\(i­mage: myimag­e\)­:.*­$/­\1:v4/' | kubectl replace -f -
Update a single­-co­ntainer pod's image version (tag) to v4
kubectl autoscale deployment foo --min=2 --max=10
Auto scale a deployment "­foo­"

Deleting Resources

kubectl delete -f ./pod.json
Delete a pod using the type and name specified in pod.json
kubectl delete pod,se­rvice baz foo
Delete pods and services with same names "­baz­" and "­foo­"

Kubectl Context and Config­uration

kubectl config view
Show Merged kubeconfig settings.
kubectl config curren­t-c­ontext
Display the curren­t-c­ontext
kubectl config use-co­ntext my-clu­ste­r-name
set the default context to my-clu­ste­r-name
kubectl config set-cr­ede­ntials kubeus­er/­foo.ku­ber­net­es.com --user­nam­e=k­ubeuser --pass­wor­d=k­ube­pas­sword
add a new cluster to your kubeconf that supports basic auth

Formatting output

-o=cus­tom­-co­lum­ns=­<sp­ec>
Print a table using a comma separated list of custom columns
-o=cus­tom­-co­lum­ns-­fil­e=<­fil­ena­me>
Print a table using the custom columns template in the <fi­len­ame> file
-o=json
Output a JSON formatted API object
-o=name
Print only the resource name and nothing else
-o=wide
Output in the plain-text format with any additional inform­ation, and for pods, the node name is included
-o=yaml
Output a YAML formatted API object