Important Kubernetes Command | Kubectl Command | 2021

Kubernetes Commands

In this blog, I will share with you all the Kubernetes commands and explain YAML File. If you have not seen our previous blog of Kubernetes then Please visit [1] [2]. I have explained everything from beginner to Average Level that you need to know for orchestration. I have explained the components of the YAML file and kubectl commands.

YAML File Explained

Every Configuration File in Kubernetes have 3 Part

  1. MetaData: It consists of name components
  2. Specification: Each configuration has a different kind of Parameter based on kind(Deployment or Service)
  3. Status: Automatically Generated and added by Kubernetes.  If Desired State is not the same as an actual state then there is a problem and need to be fixed. It gets its value from Etcd(For clear understanding Visit our architecture Blog).In summary, Etcd holds the current status of any K8s component.

There is a Template in Specification. It has its own metadata and specification. The reason is that templates are applied for Pods. It consists of the name of the container, the Image of the container and a Port in which the image should run. Pods get the label through the template blueprint.

YAML configuration file

  1. Human-Friendly data serialization standard for all programming language
  2. Syntax – Strict Indentation

kubernetes command



Create a resource from a file JSON  and YAML format are accepted.

SYNTAX : kubectl create -f FILENAME

Screenshot 2021 08 13 at 3.57.36 PM

kubectl create deployment using direct image

SYNTAX : kubectl create deployment NAME --image =image --[COMMAND] [ARGS]

kubectl create deployment deployments-nginx --image=nginx --replicas=3

If your deployment status is not Ready and in describe you are getting errors:” networkPlugin and failed to set up pod network: open /run/flannel/subnet.env: no such file or directory” Then you can follow below steps

Log into every worker node. Make sure it has /run/flannel/subnet.env with the following four lines:



It is used to get the resource information and print a table of resource information. You can filter the resource information from the list using a label selector and the –selector flag. If the desired resource type is the namespace you will only see results in your current namespace unless you pass –all-namespaces.

SYNTAX : kubectl get {resource name}

List all pods in ps output format

kubectl get pods

kubectl commands

List all pods in ps output format with more information

kubectl get pods -o wide

kubectl commands

To get all Deployment

kubectl commands


Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that resource as the selector for a new service on the specified port. A deployment or replica set will be exposed as a service only if its selector is convertible to a selector that the service supports, i.e. when the selector contains only the matchLabels component. Note that if no port is specified via –port and the exposed resource has multiple ports, all will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the resource it exposes.

SYNTAX : kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]

kubectl expose deployment deployment-nginx --port=80 --target-port=8000

kubectl commands


Delete resources by filename, stdin, resources and names or by resource or label selector. JSON or YAML  formats are accepted. Only one type of arguments may be specified: filename, resources and name, or resources and label selector.

SYNTAX: kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)])

  # Delete a pod using the type and name specified in pod.json
  kubectl delete -f ./pod.json
  # Delete resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl delete -k dir
  # Delete a pod based on the type and name in the JSON passed into stdin
  cat pod.json | kubectl delete -f -
  # Delete pods and services with same names "baz" and "foo"
  kubectl delete pod,service baz foo
  # Delete pods and services with label name=myLabel
  kubectl delete pods,services -l name=myLabel
  # Delete a pod with minimal delay
  kubectl delete pod foo --now
  # Force delete a pod on a dead node
  kubectl delete pod foo --force
  # Delete all pods
  kubectl delete pods --all

kubectl commands

Everything will be deleted from what we have created from that file.


Creates a autoscaler that automatically chooses and set the number of pods that run in a Kubernetes cluster. Looks up a Deployment, Replica Set, StatefulSet or Replication Controller by name and creates an autoscaler that uses the given resource as reference. An autoscaler can automatically increase or decrease the number of pods deployed within the system as needed.

SYNTAX : kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU]

kubectl autoscale deployment foo --min=2 --max=8


Set a new size for Deployment, ReplicaSet, Replication Controller, or StatefulSet. Scale also allows users to specify one or more preconditions for the scale action. If –current-replicas or –resource-version is specified, it is validated before the scale is attempted, and it is guaranteed that the precondition holds true when the scale is sent to the server.

Syntax: kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)

kubectl commands


As a name suggest it prints out a detailed description of selected resources, including related resources such as event or controller. You may select object by name, all objects of that type, provide a name prefix or label selector.

SYNTAX: kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]

kubectl describe pods deployments-nginx-658f96977c-6crz7

kubectl commands


Print the logs for a container or specified resource. If the pod has only one container, the container name is optional.

SYNTAX: kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]

kubectl logs deployments-nginx-658f96977c-6crz7


Execute a command in a container

SYNTAX: kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...] [options]

Screenshot 2021 08 13 at 6.21.42 PM

If you are getting error “unable to upgrade connection: Forbidden” executing kubectl exec then run

kubectl create clusterrolebinding apiserver-kubelet-admin --user=client --clusterrole=system:kubelet-api-admin

If you want to open pods then

kubectl exec -it deployments-nginx-d8d584575-8chhh sh


If you have several versions of your deployment and you want to get back to certain previous versions.

SYNTAX:kubectl rollout SUBCOMMAND [options]

kubectl rollout history deployment deployments-nginx

kubectl commands

Inorder to rollback to certain revision you can use below command.

kubectl rollout history deployment deployments-nginx  --ro-revision=1


kubectl port-forward svc/deployments-nginx 8820:80

kubectl commands

If you are stuck in the Setup of Kubernetes Cluster then  Please see how it is done.

Setup Kubernetes Cluster in 15 minutes | 2021

Hope You Like our Kubernetes Command Blog.  Please Subscribe to our blog for the Upcoming Blog. If you want to visit more such blogs then please visit our Tech Blog.


Leave a comment

Your email address will not be published.