kubectl Commands Cheat Sheet

kubectl is the command line configuration tool for Kubernetes that allows you to create, inspect, update, and delete Kubernetes objects.

List of kubectl Commands

Reference the kubectl commands listed below when working with Kubernetes.

kubectl get command

Use the kubectl get command to list Kubernetes resources such as:

  • Namespaces
  • Pods
  • Nodes
  • Deployments
  • Services
  • ReplicaSets

Nodes

$ kubetcl get nodes # show all nodes

$ kubetcl get nodes - o wide # show all nodes with more details

$ kubetcl describe nodes # show all nodes with full details

$ kubectl get node --selector =[label_name] # show the nodes with a particular label name

Instead of nodes you can also use the short code which is no

$ kubectl get no

$ kubetcl get no -o yaml # show the YAML for all nodes

$ kubetcl get no -o yaml # show the YAML for all nodes

Pods

$ kubetcl get pods # show all pods

$ kubetcl get pods -o wide # show all pods with more details

$ kubectl get pods --all-namespaces # show all pods in all namespaces

# show labels for all pods (or any other Kubernetes object that supports labelling)
$ kubectl get pods --show-labels

$ kubectl get pod my-pod -o yaml # show a specific pod’s YAML

$ kubetcl describe pods # show all pods with full details

Instead of pods you can also use the short code which is po

# show the labels for all pods in the kube-system namespace
$ kubectl get po -n kube-system --show-labels

# show all pods in the kube-system namespace with the label “app=kindnet”
$ kubectl get po -n kube-system --selector=app=kindnet

Namespaces

$ kubectl get namespaces # show all namespaces

Instead of namespace you can use the short code ns

$ kubectl get ns

$ kubectl get ns -o yaml # show YAML for all namespaces

$ kubectl describe ns # show details for all namespaces

The get command can focus in on a given namespace with the -n flag

$ kubetcl get pods -n kube-system # show all pods in the kube-system namespace

To set the default namespace

$ kubectl config set-context --current --namespace=[NAMESPACE]

Deployments

$ kubectl get deploy

$ kubectl describe deploy

$ kubectl get deploy -o wide

$ kubectl get deploy -o yaml

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 [ds_name] -n [ns_name]

$ kubectl get ds [ ds_name] - n [ ns_name] -o yaml

ReplicaSets

$ kubectl get rs

$ kubectl describe rs

$ kubectl get rs -o wide

$ kubectl get rs -o 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

PersistentVolume

$ kubectl get pv

$ kubectl describe pv

kubectl create command

To create a resource such as a service, deployment, job, or namespace using the kubectl create command.

For example, to create a new namespace, type:

$ kubectl create namespace [namespace-name] # create a namespace

To create a resource from a JSON or YAML file:

$ kubectl create -f ./my1.yaml # create a resource defined in YAML file called my1.yaml

$ kubectl create -f ./my1.yaml -f ./my2.yaml # create a resource defined in multiple YAML files

$ kubectl apply -f https://git.io/vPieo # create a resource from a URL

$ kubectl create deployment nginx --image=nginx # start a single instance of nginx

# create a job which prints “Hello World”
$ kubectl create job hello --image=busybox -- echo "Hello World"

# create a CronJob that prints “Hello World” every minute
$ kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World"

Leave a Reply