Kubernetes Cheatsheet

Kubernetes Cheatsheet

In the fast-paced world of Kubernetes, having a quick reference guide at your fingertips can be invaluable. Whether you’re a seasoned pro or just starting your Kubernetes journey, this cheatsheet has got you covered from creating objects to managing labels and selectors. Let’s dive in!


  1. Creating Objects

Create Resource

kubectl apply -f ./<file_name>.yaml

Create from Multiple Files

kubectl apply -f ./<file_name_1>.yaml -f ./<file_name_2>.yaml

Create All Files in the Directory

kubectl apply -f ./<directory_name>

Create from URL

kubectl apply -f https://<url>

Create Pod

kubectl run <pod_name> –image <image_name>

Create Pod and Expose as Service

kubectl run <pod_name> –image <image_name> –port <port> –expose

Create Pod YAML File

kubectl run <pod_name> –image <image_name> –dryrun=client -o yaml > <file_name>.yaml

Create Deployment

kubectl create deployment <deployment_name> –image <image_name>

Create Deployment YAML File

kubectl create deployment <deployment_name> –image <image_name> –dry-run=client -o yaml > <file_name>.yaml

Create Service

kubectl create service <service-type> <service_name> –tcp=<port:target_port>

Create Service YAML File

kubectl create service <service-type> <service_name> –tcp=<port:target_port> –dry-run=client -o yaml > <file_name>.yaml

Expose Service from Pod/Deployment

kubectl expose deployment <pod/deployment_name> –type=<service-type> –port <port> –target-port <target_port>
  1. ConfigMaps Commands

Create config Map from key-value

kubectl create configmap <configmap_name> –fromliteral=<key>:<value> –from-literal=<key>:<value>

Create Config Map from File

kubectl create configmap <configmap_name> –fromfile=<file_name>

Create Config Map from Env File

kubectl create configmap <configmap_name> –from-envfile=<file_name>
  1. Secret Commands

Create a Secret from the Key-Value

kubectl create secret generic <secret_name> –fromliteral=<key>:<value> –from-literal=<key>:<value>

Create a Secret from the File

kubectl create secret generic <secret_name> –fromfile=<file_name>
  1. Monitoring Usage Commands

Get Node CPU and Memory Utilization

kubectl top node <node_name>

Get Pod CPU and Memory Utilization

kubectl top pods <pod_name>
  1. Node Commands

Describe Node

kubectl describe node <node_name>

Get Node in YAML

kubectl get node <node_name> -o yaml

Get Node

kubectl get node <node_name>

Drain Node

kubectl drain node <node_name>

Cordon Node

kubectl cordon node <node_name>

Uncordon Node

kubectl uncordon node <node_name>
  1. Pod Commands

Get Pod

kubectl get pod <pod_name>

Get Pod in YAML

kubectl get pod <pod_name> -o yaml

Get Pod Wide Information

kubectl get pod <pod_name> -o wide

Get Pod with Watch

kubectl get pod <pod_name> -w

Edit Pod

kubectl edit pod <pod_name>

Describe Pod

kubectl describe pod <pod_name>

Delete Pod

kubectl delete pod <pod_name>

Log Pod

kubectl logs pod <pod_name>

Tail -f Pod

kubectl logs pod -f <pod_name>

Execute into Pod

kubectl exec -it pod <pod_name> /bin/bash

Running Temporary Image

kubectl run <pod_name> –image=curlimages/curl –rm -it –restart=Never — curl <destination>
  1. Deployment Commands

Get Deployment

kubectl get deployment <deployment_name>

Get Deployment in YAML

kubectl get deployment <deployment_name> -o yaml

Get Deployment Wide Information

kubectl get deployment <deployment_name> -o wide

Edit Deployment

kubectl edit deployment <deployment_name>

Describe Deployment

kubectl describe deployment <deployment_name>

Delete Deployment

kubectl delete deployment <deployment_name>

Log Deployment

kubectl logs deployment/deployment_name -f

Update Image

kubectl set image deployment <deployment_name> <container_name>=<new_image_name>

Scale Deployment with Replicas

kubectl scale deployment <deployment_name> –replicas <replicas>
  1. Service Commands

Get Service

kubectl get service <service>

Get Service in YAML

kubectl get service <service> -o yaml

Get Service Wide Information

kubectl get service <service> -o wide

Edit Service

kubectl edit service <service>

Describe Service

kubectl describe service <service>

Delete Service

kubectl delete service <service>
  1. Endpoints Commands

Get Endpoints

kubectl get endpoints <endpoints_name>
  1. Ingress Commands

Get Ingress

kubectl get ingress

Get Ingress in YAML

kubectl get ingress -o yaml

Get Ingress Wide Information

kubectl get ingress -o wide

Edit Ingress

kubectl edit ingress <ingress_name>

Describe Ingress

kubectl describe ingress <ingress_name>

Delete Ingress

kubectl delete ingress <ingress_name>
  1. DaemonSet Commands

Get DaemonSet

kubectl get daemonset <daemonset_name>

Get DaemonSet in YAML

kubectl get daemonset <daemonset_name> -o yaml

Edit DaemonSet

kubectl edit daemonset <daemonset_name>

Describe DaemonSet

kubectl describe daemonset <daemonset_name>

Delete DaemonSet

kubectl delete daemonset <daemonset_name>
  1. StatefulSet Commands

Get StatefulSet

kubectl get statefulset <statefulset_name>

Get StatefulSet in YAML

kubectl get statefulset <statefulset_name> -o yaml

Edit StatefulSet

kubectl edit statefulset <statefulset_name>

Describe StatefulSet

kubectl describe statefulset <statefulset_name>

Delete StatefulSet

kubectl delete statefulset <statefulset_name>
  1. ConfigMaps Commands

Get ConfigMap

kubectl get configmap <configmap_name>

Get ConfigMap in YAML

kubectl get configmap <configmap_name> -o yaml

Edit ConfigMap

kubectl edit configmap <configmap_name>

Describe ConfigMap

kubectl describe configmap <configmap_name>

Delete ConfigMap

kubectl delete configmap <configmap_name>
  1. Secret Commands

Get Secret

kubectl get secret <secret_name>

Get Secret in YAML

kubectl get secret <secret_name> -o yaml

Edit Secret

kubectl edit secret <secret_name>

Describe Secret

kubectl describe secret <secret_name>

Delete Secret

kubectl delete secret <secret_name>
  1. Rollout Commands

Restart Deployment

kubectl rollout restart deployment <deployment_name>

Undo Deployment with the Latest Revision

kubectl rollout undo deployment <deployment_name>

Undo Deployment with Specified Revision

kubectl rollout undo deployment <deployment_name> –to-revision <revision_number>

Get All Revisions of the Deployment

kubectl rollout history deployment <deployment_name>

Get Specified Revision of Deployment

kubectl rollout history deployment <deployment_name> –revision=<revision_number>
  1. Job Commands

Get Job

kubectl get job <job_name>

Get Job in YAML

kubectl get job <job_name> -o yaml

Edit Job in YAML

kubectl edit job <job_name>

Describe Job

kubectl describe job <job_name>

Delete Job

kubectl delete job <job_name>
  1. Cronjob Commands

Get Cronjob

kubectl get cronjob <cronjob_name>

Get Cronjob in YAML

kubectl get cronjob <cronjob_name> -o yaml

Edit Cronjob

kubectl edit cronjob <cronjob_name>

Describe Cronjob

kubectl describe cronjob <cronjob_name>

Delete Cronjob

kubectl delete cronjob <cronjob_name>
  1. Network Policy Commands

Get NetworkPolicy

kubectl get networkpolicy <networkpolicy_name>

Get NetworkPolicy in YAML

kubectl get networkpolicy <networkpolicy_name> -o yaml

Get NetworkPolicy Wide Information

kubectl get networkpolicy <networkpolicy_name> -o wide

Edit NetworkPolicy

kubectl edit networkpolicy <networkpolicy_name>

Describe NetworkPolicy

kubectl describe networkpolicy <networkpolicy_name>

Delete NetworkPolicy

kubectl delete networkpolicy <networkpolicy_name>
  1. Labels and Selectors Commands

Show Labels of Node, Pod, and Deployment

kubectl get <node/pod/deployment> –show-labels

Attach Labels to Node, Pod, and Deployment

kubectl label <node/pod/deployment> <resource_name> <key>=<value>

Remove Labels from Node, Pod, and Deployment

kubectl label <node/pod/deployment> <resource_name> <key>-

Select Node, Pod, and Deployment by Using Labels

kubectl get <node/pod/deployment> -l <label_selector>

Stay tuned for more updates and advanced Kubernetes commands!

Join our DevOps community on Facebook for discussions and updates!

Leave a Reply

Your email address will not be published. Required fields are marked *