Most useful kubectl Command for Kubernetes Administrator or Developer

Kubectl is a command line tool which is use to manage Kubernetes clusters. Using kubectl tool, Administrator or Developer can perform day to day operations of container and cluster management. Such as; configuration change to a resource; attach Kubernetes to a running container; access container logs; and run a specified docker image on your cluster.

So let’s understand the kubectl command syntax structure.

Kubectl Syntax Structure

There are four major parameters in Kubectl syntax which is very simple to understand.

kubectl <command> <type> <name> <flags>

[Command]- Parameter is the operation/task that we want to performed on a resource. Kubectl supports many of operations, suach as create, get, describe, logs,execute and delete.

[Type]-Parameter specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms.

[Name]-Parameter specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed.

[Flags]-Parameter adds optional flags to the command line. Flags vary with the command, that means not all flags are available for all kubernetes commands.

Useful list of Kubectl commands

Now let’s understand more about the kubectl commands which are most useful for the kubernetes cluster management and operations.

Kubectl create command;

With kubectl, you can create cluster resources-

  • service
  • deployment
  • job
  • namespace (ns)
$ kubectl create namespace [namespace-name]

Or you can use YAML file to create resource using command-

$ kubectl create -f filename<YAML orJASON>

Such as to create namespace requires another parameter as name of the namespace.

$ kubectl create ns dev
  namespace/dev created

Kubectl get command;

This command is used to get the list of one or more pods, replication controllers, services, or daemon sets.

a) To see the list of all namespaces-

$ kubectl get namespaces

b) To see the list of all Pods-

$ kubectl get pods

And for more insight details of Pods use the command-

$ kubectl get pods -o wide

c) To see the list of all replictioncontroller-

$ kubectl get replicationcontroller

d) To see the list of all demaonset-

$ kubectl get daemonset

e)To see the list of all service-

$ kubectl get service

Kubectl describe command;

The describe command allows you to see the details about you object like Pods, service, deployment.

1- Existing pods details;

$ kubectl describe pod <pod name>

2- Deplyment details;

$ kubectl describe deployment [DEPLOYMENT_NAME]

Kubectl exec command;

This command is used to intract/login or working within the Pod running in your clusters.

$ kubectl exec -it [POD_NAME] -- [command]
$ kubectl exec -it dev -- /bin/bash

kubectl apply command;

The apply command allows to apply configurations via files(YAML or JASON) for resources in the cluster.

$ kubectl apply -f <your service-name>.yaml

If it is a deployment;

kubectl apply -f <your deplyment-name>.yaml

Kubectl logs command;

It shows logs which help you to troubleshoot issues inside the application and Kubernetes.

$ kubectl logs [POD_NAME]

Kubectl delete command;

To remove resources or object, use the kubectl delete command.

$ kubectl delete pods --all

This command will remove all pods.

That’s all in this tutorial, hope you enjoy !!

I hope this tutorial provided you with the most useful kubectl commands to help you manage your Kubernetes cluster.

About Shachindra Yadav

I, Shachindra Yadav, a Cloud Certified, DevOps and technology enthusiastic person, more passionate to learn and write blogs in my free time.

View all posts by Shachindra Yadav →