KUBERNETES(K8s) CHEATSHEET

Viewing Resource Information

Nodes

kubectl get no # retrieve a list of all nodes in the current cluster
kubectl get no -o wide # retrieve a list of all nodes in the current cluster with additional details such as the node's IP address and role
kubectl describe no # retrieve detailed node information
kubectl get no -o yaml # retrieve a list of all nodes in the current cluster in YAML format
kubectl get node --selector=label-name # retrieve a list of all nodes with the specified label
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}' # retrieve a list of all node external IP addresses
kubectl top node <node-name> # display resource usage statistics for a specific node

Pods

kubectl get po # get a list of pods
kubectl get po -o wide # get a wide view of pods, including Node and IP
kubectl describe po # describe all the pods in the current namespace
kubectl get po --show-labels # get list of pods with labels
kubectl get po -l app=<app-name> # get list of pods with label "app" equal to <app-name>
kubectl get po -o yaml # get yaml definition of all the pods in the current namespace
kubectl get pod <pod-name> -o yaml --export > myoffline.yaml # save yaml definition of the specific pod in the file
kubectl get pods --field-selector=status.phase=Running # get list of pods with the status "Running"

Adding Resources

Creating A Pod

kubectl create -f <name.of.file> # create resources from a file
kubectl apply -f <name.of.file> # apply changes from a file
kubectl run <pod-name> --image=nginx --restart=Never # run a single instance of an nginx container using "run-pod/v1" generator

Creating A Service

kubectl create svc nodeport <svc-name> --tcp=8080 # create a NodePort service that exposes TCP port 8080 on the nodes and maps it to port 80 in the pods

Creating A Deployment

kubectl create -f <name.of.file> # create resources from a file
kubectl apply -f <name.of.file> # apply changes from a file
kubectl create deploy <deploy-name> --image=nginx # create a deployment with nginx container

Interactive Pod

kubectl run <pod-name> --image=busybox --rm -it --restart=Never -- <sh> # run a temporary busybox container, open an interactive shell and delete it when it exits

Output YAML to A File

kubectl create deploy <deploy-name> --image=nginx --dry-run -o yaml > deploy.yaml # create a deployment with an nginx container in YAML format without creating the resources
kubectl get po <pod-name> -o yaml --export > pod.yaml # get a pod and output the configuration to a file in YAML format, ignoring cluster-specific information

Changing Resource Attributes

Taint

kubectl taint <node-name> <taint-name> # add taint to a node

Labels

kubectl label <node-name> disktype=ssd # add label to a node
kubectl label <pod-name> env=prod # add label to a pod

Cordon/Uncordon

kubectl cordon <node-name> # mark a node as unschedulable
kubectl uncordon <node-name> # mark a node as schedulable

Drain

kubectl drain <node-name> # drain a node in preparation for maintenance

Nodes

kubectl delete node <node-name> # delete a node from the cluster
kubectl edit node <node-name> # edit a node's configuration

Namespaces

kubectl delete ns <namespace-name> # delete a namespace
kubectl edit ns <namespace-name> # edit a namespace's configuration

Deployments

kubectl delete deploy <deploy-name> # delete a deployment
kubectl edit deploy <deploy-name> # edit a deployment's configuration
kubectl get roles --all-namespaces kubectl expose deployment <deploy-name> --port=80 --type=NodePort # expose deployment as a NodePort service
kubectl scale deploy <deploy-name> --replicas=5 # scale a deployment to 5 replicas

Pods

kubectl delete pod <pod-name> # delete a pod
kubectl edit pod <pod-name> # edit a pod's configuration

Viewing Resource Information

Secrets

kubectl get secrets # display a list of secrets in the current namespace
kubectl get secrets --all-namespaces # display a list of secrets in all namespaces
kubectl get secrets -o yaml # display the secrets in the current namespace in YAML format

ConfigMaps

kubectl get cm # display a list of config maps in the current namespace
kubectl get cm --all-namespaces # display a list of config maps in all namespaces
kubectl get cm --all-namespaces -o yaml # display the config maps in all namespaces in yaml format

Ingress

kubectl get ing # display a list of ingresses in the current namespace
kubectl get ing --all-namespaces # display list of all ingresses in all namespaces

PersistentVolume

kubectl get pvc # display list of persistent volume claims in the current namespace
kubectl describe pvc # display detailed information about the persistent volume claims in the cluster

StorageClass

kubectl get sc # display list of all storage classes in the cluster
kubectl get sc -o yaml # display the storage classes in yaml format

API Call

kubectl get --raw /apis/metrics.k8s.io/ # get raw json data for the metrics API

Cluster Info

kubectl config # view and modify kubeconfig files
kubectl cluster-info # display information about the kubernetes cluster
kubectl get componentstatuses # get the status of the various components in the kubernetes cluster

Viewing Resource Information

Events

kubectl get events # display list of events in the current namespace
kubectl get events -n kube-system # display list of events in the kube-system namespace
kubectl get events -w # watch for new events in the current namespace

Logs

kubectl logs <pod-name> # display logs of the specified pod
kubectl logs --since=1h <pod-name> # display the logs of a pod for the past 1 hour
kubectl logs -tail=20 <pod-name> # display the last 20 lines for the logs for a pod
kubectl logs -f -c <container-name> <pod-name> # follow the logs for a specific container in the pod
kubectl logs <pod-name> > pod.log # save the logs for a pod to a file

Service Accounts

kubectl get sa # display a list of service accounts in the current namespace
kubectl get sa -o yaml # display a list of service accounts in yaml format
kubectl get serviceaccount default -o yaml # save the "default" service account in the yaml format to a file
kubectl replace serviceaccount default -f sa.yaml # replace the contents of the "default" service account with the contents of a yaml file

ReplicaSets

kubectl get rs # display a list of all replica sets in the current namespace
kubectl describe rs # display detailed information about replica sets in the current namespace
kubectl get rs -o wide # display a list of replica sets in the current namespace with additional details
kubectl get rs -o yaml # display the replica sets in the current namespace in yaml format

Multiple Resources

kubectl get svc,po # display list of services and pods in the current namespace
kubectl get deploy,no # display list of deployments and nodes in the current namespace
kubectl get all # display list of all resources in the current namespace
kubectl get all --all-namespaces # display list of all resources in all namespaces

Viewing Resource Information

Namespaces

kubectl get ns # display a list of all namespaces in the current cluster
kubectl get ns -o yaml # display the namespaces in the cluster in yaml format
kubectl describe ns # display detailed information about a namespace in the cluster

Deployments

kubectl get deploy # display list of all deployments in the current namespace
kubectl describe deploy # display detailed information about deployments in the current namespace
kubectl get deploy -o wide # display list of deployments in the current namespace with additional details
kubectl get deploy -o yaml # display the deployments in the current namespace in yaml format

Services

kubectl get svc # display a list of all services in the current namespace
kubectl describe svc # display detailed information about services in the current namespace
kubectl get svc -o wide # display a detailed list of services about the current namespace with additional details
kubectl get svc -o yaml # display the services in the current namespace in yaml format
kubectl get svc --show-labels # display a list of services in the current namespace including their labels

Real-Time Monitoring

kubectl top pod <pod-name> # display real-time CPU and memory usage for a specific pod
kubectl top node <node-name> # display real-time CPU and memory usage for a specific node

Roles

kubectl get roles --all-namespaces # display a list of all roles in all namespaces
kubectl get roles --all-namespaces -o yaml # display the roles in all namespaces in YAML format

🙏 Support

Buy Me A Coffee


Developed with ❤️ in India 🇮🇳