Kubernetes Cheat Sheet

Kubernetes is an open source container orchestration engine which provides basic mechanisms for deployment, maintenance, and scaling of applications. Kubernetes is hosted by the Cloud Native Computing Foundation (CNCF). In this tutorial, we will explore Kubernetes commands | Kubernetes cheat sheet which can be used in day to day life by sysadmins and developers.

Getting Started with Kubernetes Cheat Sheet

Viewing Kubernetes Resource Information

Pods

$ kubectl get po
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po –-show-labels
$ kubectl get po -l env=prod
$ kubectl get po -o yaml
$ kubectl get pod [ pod-name] -o yaml
$ kubectl get pod [pod-name] -o yaml > file.yaml

Nodes

$ kubectl get no
$ kubectl get no -o wide
$ kubectl describe no
$ kubectl get no -o yaml
$ kubectl label node node1.example.com color=green
$ kubectl get nodes -l="color=green"
$ kubectl top node [node-name]

Namespaces

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

Deployments

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

Events

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

Services

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

DaemonSets

$ kubectl get ds
$ kubectl get ds –-all-namespaces
$ kubectl describe ds [daemonset-name] -n [namespace-name]
$ kubectl get ds [ds-name] -n [ns-name] -o yaml

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

$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml > sa.yaml

ReplicaSets

$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide
$ kubectl get rs -o yaml
$ kubectl get rs -o yaml > rs.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

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

Ingress

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

PersistentVolume

$ kubectl get pv
$ kubectl describe pv
$ kubectl get pv -o yaml

PersistentVolumeClaim

$ kubectl get pvc
$ kubectl describe pvc
$ kubectl get pvc -o yaml 

StorageClass

$ kubectl get sc
$ kubectl get sc -o yaml

Multiple Resources

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

Modifying Resource Attributes

Taints

$ kubectl taint [node-name] [taint -name]

$ kubectl taint node node1.example.com zone=green:NoSchedule

To Remove taint add – add the end of taint name

$ kubectl taint node node1.example.com zone=green:NoSchedule-

Labels

$ kubectl label node [node-name] color=green
$ kubrectl label pod [pod-name] env=prod

Cordon/Uncordon

$ kubectl cordon [node-name]
$ kubectl uncordon [node-name]

Drain

$ kubectl drain [node-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 [ns-name]
$ kubectl edit ns [ns-name]

Nodes/Pods

$ kubectl delete node [node-name]
$ kubectl delete pod [pod-name]
$ kubectl edit node [node-name]
$ kubectl edit pod [pod-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]

ServiceAccounts

$ kubectl edit sa [sa-name]
$ kubectl delete sa [sa-name]

Annotate

$ kubectl annotate po [pod-name] [annotation]
$ kubectl annotate no [node-name]

Adding Resources

Creating a Pod

$ kubectl create -f [file-name]
$ kubectl apply -f [file-name]
$ kubectl run [pod-name] --image=nginx –-restart=Never

Creating a Service

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

Creating a Deployment

$ kubectl create -f [file-name]
$ kubectl apply -f [file-name]
$ kubectl create deploy [deploy-name] --image=nginx

Interactive Pod

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

Output YAML to a File

$ kubectl create deploy [deploy-name] --dry-run=client -o yaml > mydeploy.yaml
$ kubectl get po [pod-name] -o yaml  > pod.yaml

Cluster Info

$ kubectl config
$ kubectl cluster -info
$ kubectl get componentstatuses

Conclusion

This tutorial explained the common kubectl commands or we can say it as kubectl cheat sheet which you can use to manage your Kubernetes cluster. Kubernetes cheat sheet has all the commands in one place which can be referred as an when required.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments