0% found this document useful (0 votes)
55 views

Kubectl CheatSheet v2

The document is a cheat sheet for common Kubectl commands for managing Kubernetes clusters. It provides a table listing common Kubectl commands with brief descriptions. It also includes additional notes on enabling shell autocompletion for Kubectl and applying YAML files from a folder. The cheat sheet is available as a PDF and is categorized under cloud resources on the author's website.

Uploaded by

Rpl Marseille
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Kubectl CheatSheet v2

The document is a cheat sheet for common Kubectl commands for managing Kubernetes clusters. It provides a table listing common Kubectl commands with brief descriptions. It also includes additional notes on enabling shell autocompletion for Kubectl and applying YAML files from a folder. The cheat sheet is available as a PDF and is categorized under cloud resources on the author's website.

Uploaded by

Rpl Marseille
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

C H E AT S H E E T
( H T T P S : // C H E AT S H E E T. D E N N Y Z
us c e s e s to ap y be p a t

January 1, 2019

KUBECTL KUBERNETES FREE CHEATSHEET

(https://github.com/dennyzhang/cheatsheet-kubernetes-A4)

(https://www.linkedin.com/in/dennyzhang001)

(https://github.com/dennyzhang)

(https://www.dennyzhang.com/slack)

PRs welcome (http://makeapullrequest.com)

PDF Link: cheatsheet-kubernetes-A4.pdf


(https://github.com/dennyzhang/cheatsheet-kubernetes-
A4/blob/master/cheatsheet-kubernetes-A4.pdf), Category: Cloud
(https://cheatsheet.dennyzhang.com/category/cloud/)
Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-A4
(https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-A4)
Related posts: Kubectl CheatSheet (https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-A4), Kubernetes Yaml
(https://cheatsheet.dennyzhang.com/kubernetes-yaml-templates), #denny-cheatsheets
(https://github.com/topics/denny-cheatsheets)

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 1/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet
File me Issues (https://github.com/dennyzhang/cheatsheet.dennyzhang.com/issues) or star this repo
(https://github.com/dennyzhang/cheatsheet.dennyzhang.com).

1.1 COMMON COMMANDS

Name Command

Run curl test temporarily kubectl run --rm mytest --image=yauritux/busybox-curl -it

Run wget test temporarily kubectl run --rm mytest --image=busybox -it

Run nginx deployment with 2 replicas kubectl run my-nginx --image=nginx --replicas=2 --port=80

kubectl run my-nginx --restart=Never --image=nginx --


Run nginx pod and expose it
port=80 --expose

Run nginx deployment and expose it kubectl run my-nginx --image=nginx --port=80 --expose

kubectl config set-context <context_name> --namespace=


Set namespace preference
<ns_name>

List pods with nodes info kubectl get pod -o wide

List everything kubectl get all --all-namespaces

Get all services kubectl get service --all-namespaces

Get all deployments kubectl get deployments --all-namespaces

Show nodes with labels kubectl get nodes --show-labels

Get resources with json output kubectl get pods --all-namespaces -o json

Validate yaml le with dry run kubectl create --dry-run --validate -f pod-dummy.yaml

Start a temporary pod for testing kubectl run --rm -i -t --image=alpine test-$RANDOM -- sh

kubectl run shell command kubectl exec -it mytest -- ls -l /etc/hosts

Get system conf via con gmap kubectl -n kube-system get cm kubeadm-config -o yaml

Get deployment yaml kubectl -n denny-websites get deployment mysql -o yaml

Explain resource kubectl explain pods , kubectl explain svc

Watch pods kubectl get pods -n wordpress --watch

Query healthcheck endpoint curl -L http://127.0.0.1:10250/healthz

Open a bash terminal in a pod kubectl exec -it storage sh

Check pod environment variables kubectl exec redis-master-ft9ex env

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 2/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

echo "source <(kubectl completion bash)" >>~/.bashrc , and


Enable kubectl shell autocompletion
reload

eval $(minikube docker-env) , No need to push docker hub any


Use minikube dockerd in your laptop
more

Kubectl apply a folder of yaml les kubectl apply -R -f .

Get services sorted by name kubectl get services –sort-by=.metadata.name

kubectl get pods –sort-


Get pods sorted by restart count
by=’.status.containerStatuses[0].restartCount’

kubectl get pods -o=’custom-


List pods and images
columns=PODS:.metadata.name,Images:.spec.containers[*].image’

list-all-images.sh (https://github.com/dennyzhang/cheatsheet-
List all container images
kubernetes-A4/blob/master/list-all-images.sh#L14-L17)

skip-tls-verify.md (https://github.com/dennyzhang/cheatsheet-
kubecon g skip tls veri cation
kubernetes-A4/blob/master/skip-tls-verify.md)

Ubuntu install kubectl


(https://kubernetes.io/docs/tasks/tools/install- "deb https://apt.kubernetes.io/ kubernetes-xenial main"
kubectl/)

GitHub: kubernetes releases


Reference
(https://github.com/kubernetes/kubernetes/tags)

minikube cheatsheet
(https://cheatsheet.dennyzhang.com/cheatsheet-minikube-A4),
docker cheatsheet
Reference
(https://cheatsheet.dennyzhang.com/cheatsheet-docker-A4),
OpenShift CheatSheet
(https://cheatsheet.dennyzhang.com/cheatsheet-openshift-A4)

1.2 CHECK PERFORMANCE

Name Command

Get node resource usage kubectl top node

Get pod resource usage kubectl top pod

Get resource usage for a given pod kubectl top <podname> --containers

List resource utilization for all containers kubectl top pod --all-namespaces --containers=true

1.3 RESOURCES DELETION

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 3/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

Delete pod kubectl delete pod/<pod-name> -n <my-namespace>

Delete pod by force kubectl delete pod/<pod-name> --grace-period=0 --force

Delete pods by labels kubectl delete pod -l env=test

Delete deployments by labels kubectl delete deployment -l app=wordpress

Delete all resources ltered by labels kubectl delete pods,services -l name=myLabel

Delete resources under a namespace kubectl -n my-ns delete po,svc --all

Delete persist volumes by labels kubectl delete pvc -l app=wordpress

Delete state fulset only (not pods) kubectl delete sts/<stateful_set_name> --cascade=false

(https://cheatsheet.dennyzhang.com)

1.4 LOG & CONF FILES

Name Comment

Con g folder /etc/kubernetes/

Certi cate les /etc/kubernetes/pki/

Credentials to API server /etc/kubernetes/kubelet.conf

Superuser credentials /etc/kubernetes/admin.conf

kubectl con g le ~/.kube/config

Kubernets working dir /var/lib/kubelet/

Docker working dir /var/lib/docker/ , /var/log/containers/

Etcd working dir /var/lib/etcd/

Network cni /etc/cni/net.d/

Log les /var/log/pods/

log in worker node /var/log/kubelet.log , /var/log/kube-proxy.log

log in master node kube-apiserver.log , kube-scheduler.log , kube-controller-manager.log

Env /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Env export KUBECONFIG=/etc/kubernetes/admin.conf

1.5 POD
https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 4/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

List all pods kubectl get pods

List pods for all namespace kubectl get pods -all-namespaces

List all critical pods kubectl get -n kube-system pods -a

List pods with more info kubectl get pod -o wide , kubectl get pod/<pod-name> -o yaml

Get pod info kubectl describe pod/srv-mysql-server

List all pods with labels kubectl get pods --show-labels

List all unhealthy pods kubectl get pods – eld-selector=status.phase!=Running –all-


(https://github.com/kubernetes/kubernetes/issues/49387) namespaces

List running pods kubectl get pods – eld-selector=status.phase=Running

kubectl get pod --template '{{.status.initContainerStatuses}}'


Get Pod initContainer status
<pod-name>

kubectl run command kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log”

Watch pods kubectl get pods -n wordpress --watch

kubectl get pods –selector=”app=syslog” -o


Get pod by selector
jsonpath='{.items[*].metadata.name}’

kubectl get pods -o=’custom-


List pods and images
columns=PODS:.metadata.name,Images:.spec.containers[*].image’

-o=’custom-
List pods and containers
columns=PODS:.metadata.name,CONTAINERS:.spec.containers[*].name’

Link: kubernetes yaml templates


Reference
(https://cheatsheet.dennyzhang.com/kubernetes-yaml-templates)

1.6 LABEL & ANNONTATION

Name Command

Filter pods by label kubectl get pods -l owner=denny

Manually add label to a pod kubectl label pods dummy-input owner=denny

Remove label kubectl label pods dummy-input owner-

Manually add annonation to a pod kubectl annotate pods dummy-input my-url=https://dennyzhang.com

1.7 DEPLOYMENT & SCALE

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 5/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

Scale out kubectl scale --replicas=3 deployment/nginx-app

online rolling
kubectl rollout app-v1 app-v2 --image=img:v2
upgrade

Roll backup kubectl rollout app-v1 app-v2 --rollback

List rollout kubectl get rs

Check update
kubectl rollout status deployment/nginx-app
status

Check update
kubectl rollout history deployment/nginx-app
history

Pause/Resume kubectl rollout pause deployment/nginx-deployment , resume

Rollback to
previous kubectl rollout undo deployment/nginx-deployment
version

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-yaml-


templates), Link: Pausing and Resuming a Deployment
Reference
(https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#pausing-and-
resuming-a-deployment)

(https://cheatsheet.dennyzhang.com)

1.8 QUOTA & LIMITS & RESOURCE

Name Command

List Resource Quota kubectl get resourcequota

List Limit Range kubectl get limitrange

Customize resource
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m
de nition

Customize resource
kubectl set resources deployment nginx -c=nginx --limits=memory=512Mi
de nition

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-


Reference
yaml-templates)

1.9 SERVICE

Name Command

List all services kubectl get services

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 6/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

List service endpoints kubectl get endpoints

Get service detail kubectl get service nginx-service -o yaml

Get service cluster ip kubectl get service nginx-service -o go-template='{{.spec.clusterIP}}’

Get service cluster port kubectl get service nginx-service -o go-template='{{(index .spec.ports 0).port}}’

Expose deployment as lb
kubectl expose deployment/my-app --type=LoadBalancer --name=my-service
service

Expose service as lb
kubectl expose service/wordpress-1-svc --type=LoadBalancer --name=ns1
service

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-


Reference
yaml-templates)

1.10 SECRETS

Name Command

List secrets kubectl get secrets --all-namespaces

Generate
echo -n 'mypasswd' , then redirect to base64 --decode
secret

Get secret kubectl get secret denny-cluster-kubeconfig

Get a speci c
eld of a kubectl get secret denny-cluster-kubecon g -o jsonpath=”{.data.value}”
secret

Create secret
kubectl create secret generic db-user-pass –from- le=./username.txt
from cfg le

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-yaml-


Reference
templates), Link: Secrets (https://kubernetes.io/docs/concepts/con guration/secret/)

1.11 STATEFULSET

Name Command

List statefulset kubectl get sts

Delete statefulset only (not


kubectl delete sts/<stateful_set_name> --cascade=false
pods)

Scale statefulset kubectl scale sts/<stateful_set_name> --replicas=5

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 7/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-


Reference
yaml-templates)

1.12 VOLUMES & VOLUME CLAIMS

Name Command

List storage class kubectl get storageclass

Check the mounted


kubectl exec storage ls /data
volumes

Check persist volume kubectl describe pv/pv0001

Copy local le to pod kubectl cp /tmp/my <some-namespace>/<some-pod>:/tmp/server

Copy pod le to local kubectl cp <some-namespace>/<some-pod>:/tmp/server /tmp/my

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-


Reference
yaml-templates)

1.13 EVENTS & METRICS

Name Command

View all events kubectl get events --all-namespaces

List Events sorted by timestamp kubectl get events –sort-by=.metadata.creationTimestamp

1.14 NODE MAINTENANCE

Name Command

Mark node as unschedulable kubectl cordon $NDOE_NAME

Mark node as schedulable kubectl uncordon $NDOE_NAME

Drain node in preparation for maintenance kubectl drain $NODE_NAME

1.15 NAMESPACE & SECURITY

Name Command

List authenticated
kubectl config get-contexts , ~/.kube/config
contexts

Set namespace
kubectl config set-context <context_name> --namespace=<ns_name>
preference

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 8/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Command

Load context from


kubectl get cs --kubeconfig kube_config.yml
con g le

Switch context kubectl config use-context <cluster-name>

Delete the speci ed


kubectl config delete-context <cluster-name>
context

List all namespaces


kubectl get namespaces
de ned

List certi cates kubectl get csr

Link: kubernetes yaml templates (https://cheatsheet.dennyzhang.com/kubernetes-


Reference
yaml-templates)

1.16 NETWORK

Name Command

Temporarily add a port-forwarding kubectl port-forward redis-134 6379:6379

Add port-forwaring for deployment kubectl port-forward deployment/redis-master 6379:6379

Add port-forwaring for replicaset kubectl port-forward rs/redis-master 6379:6379

Add port-forwaring for service kubectl port-forward svc/redis-master 6379:6379

Get network policy kubectl get NetworkPolicy

1.17 PATCH

Name Summary

Patch service to loadbalancer kubectl patch svc $svc_name -p '{"spec": {"type": "LoadBalancer"}}'

1.18 EXTENSTIONS

Name Summary

List api group kubectl api-versions

List all CRD kubectl get crd

List storageclass kubectl get storageclass

List all supported resources kubectl api-resources

(https://cheatsheet.dennyzhang.com)

1.19 COMPONENTS & SERVICES

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 9/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet
1.19.1 SERVICES ON MASTER NODES

Name Summary

kube-apiserver
exposes the Kubernetes API from master
(https://github.com/kubernetes/kubernetes/tree/master/cmd/kube-
nodes
apiserver)

etcd (https://coreos.com/etcd/) reliable data store for all k8s cluster data

kube-scheduler
(https://github.com/kubernetes/kubernetes/tree/master/cmd/kube- schedule pods to run on selected nodes
scheduler)

kube-controller-manager node controller, replication controller,


(https://github.com/kubernetes/kubernetes/tree/master/cmd/kube- endpoints controller, and service account
controller-manager) & token controllers

1.19.2 SERVICES ON WORKER NODES

Name Summary

kubelet makes sure that containers are running in a


(https://github.com/kubernetes/kubernetes/tree/master/cmd/kubelet) pod

kube-proxy
(https://github.com/kubernetes/kubernetes/tree/master/cmd/kube- perform connection forwarding
proxy)

Kubernetes supported runtimes: Docker, rkt,


runc and any OCI runtime-spec
Container Runtime (https://github.com/docker/engine)
(https://github.com/opencontainers/runtime-
spec) implementation.

1.19.3 ADDONS: PODS AND SERVICES THAT IMPLEMENT CLUSTER FEATURES

Name Summary

DNS serves DNS records for Kubernetes services

Web UI a general purpose, web-based UI for Kubernetes clusters

Container Resource Monitoring collect, store and serve container metrics

Cluster-level Logging save container logs to a central log store with search/browsing interface

1.19.4 TOOLS

Name Summary

kubectl
the command line util to talk to k8s cluster
(https://github.com/kubernetes/kubernetes/tree/master/cmd/kubectl)

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 10/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet

Name Summary

kubeadm
the command to bootstrap the cluster
(https://github.com/kubernetes/kubernetes/tree/master/cmd/kubeadm)

kubefed (https://kubernetes.io/docs/reference/setup-
the command line to control a Kubernetes Cluster Federation
tools/kubefed/kubefed/)

Link: Kubernetes Components


Kubernetes Components
(https://kubernetes.io/docs/concepts/overview/components/)

1.20 MORE RESOURCES


License: Code is licensed under MIT License (https://www.dennyzhang.com/wp-content/mit_license.txt).

https://kubernetes.io/docs/reference/kubectl/cheatsheet/ (https://kubernetes.io/docs/reference/kubectl/cheatsheet/)

https://codefresh.io/kubernetes-guides/kubernetes-cheat-sheet/ (https://codefresh.io/kubernetes-guides/kubernetes-cheat-
sheet/)

(https://cheatsheet.dennyzhang.com)
(https://cheatsheet.dennyzhang.com)

(https://www.linkedin.com/in/dennyzhang001)
(https://github.com/dennyzhang)
(https://www.dennyzhang.com/slack)

Author: cheatsheetdenny (https://cheatsheet.dennyzhang.com/author/cheatsheetdenny)


Filed Under: Cloud (https://cheatsheet.dennyzhang.com/category/cloud)
Tags: kubernetes (https://cheatsheet.dennyzhang.com/tag/kubernetes)

 KUBERNETES YAML TEMPLATES (HT TPS: //CHEATSHEET.DENNYZHANG .COM/KUBERNETES -YAML-


TEMPLATES)

LEAVE A REPLY
Your email address will not be published. Required elds are marked *

NAME*

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 11/12
17/04/2020 Kubectl Kubernetes Free CheatSheet – CheatSheet
EMAIL ADDRESS*

WEBSITE

COMMENT

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

Kale (https://www.lyrathemes.com/kale/) by LyraThemes.com.

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 12/12

You might also like