Kubernetes Commands 101 📝📝

Vibhor Chinda
Google Cloud - Community
6 min readOct 30, 2022

Basic kubectl commands you must know to start your k8s Journey

Introduction 🚩🚩

Hi fellow Readers 👋 :))
I have been writing about the basics of Kubernetes and it’s objects for a while now. The road ahead in the world of Kubernetes will be more advanced one. Topics will range from the concepts of Scheduling to Networking.

What I believe is before moving to those topics, we must learn about the basic Kubernetes commands first. I have discussed about some of them in my previous articles till now, but I thought it would be a good idea to collect and dump them in one single place :))

So In this article, we will try to learn about :

  • What is kubectl ??
  • Basic kubectl commands.
  • Imperative vs Declarative way of managing a Kubernetes cluster.

It will be a very short and crisp article.
So without any further delay, lets get started with it :))

What is kubectl ?? 🤔🤔

Kubernetes provides a command line tool for communicating with a Kubernetes cluster’s control plane, using the Kubernetes API. This tool is named kubectl .
Kubectl commands adhere to syntax or a common structure, which lets administrators to read and verify every kubectl command entered in the terminal window. There are four important parameters to each kubectl call.

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

kubectl is the common CLI tool that we use to query and manage a Kubernetes cluster.
kubectl uses the API interface of Kubernetes to view, control, and manage the cluster. It is supported across different platforms and can be easily set up to manage a cluster.

Basic kubectl Commands🤓🤓

  • Cluster Management
kubectl cluster-info
Display endpoint information regarding the services and master in the cluster
kubectl version
Show the Kubernetes version functioning on the client and server
kubectl config view
Get the configuration of the cluster
kubectl api-resources
Make a list of the available API resources
kubectl api-versions
Make a list of the available API versions
kubectl get all –all-namespaces
List everything
  • Node operations
kubectl get node
List one or more nodes
kubectl delete node <node_name>
Delete a node or multiple nodes
kubectl top node
Display Resource usage (CPU/Memory/Storage) for nodes
kubectl describe nodes | grep Allocated -A 5
Resource allocation per node
kubectl get pods -o wide | grep <node_name>
Pods running on a node
kubectl annotate node <node_name>
Annotate a node
kubectl cordon node <node_name>
Mark a node as unschedulable
kubectl uncordon node <node_name>
Mark node as schedulable
kubectl drain node <node_name>
Drain a node in preparation for maintenance
kubectl label node
Add the labels of one or more nodes
  • Namespaces [Shortcode = ns]
kubectl create namespace <namespace_name>
Create namespace <name>
kubectl describe namespace <namespace_name>
Show the detailed condition of one or more namespace
kubectl delete namespace <namespace_name>
Delete a namespace
kubectl edit namespace <namespace_name>
Edit and modify the namespace’s definition
kubectl top namespace <namespace_name>
Display Resource (CPU/Memory/Storage) usage for a namespace
  • Deployments
kubectl get deployment
List one or more deployments
kubectl describe deployment <deployment_name>
Show the in-depth state of one or more deployments
kubectl edit deployment <deployment_name>
Edit and revise the definition of one or more deployment on the server
kubectl create deployment <deployment_name>
Generate one a new deployment
kubectl delete deployment <deployment_name>
Delete deployments
kubectl rollout status deployment <deployment_name>
Check the rollout status of a deployment
  • Replication Controllers [Shortcode = rc]
kubectl get rc
Make a list of the replication controllers
kubectl get rc –namespace=<namespace_name>
Make a list of the replication controllers by namespace
  • ReplicaSets [Shortcode = rs]
kubectl get replicasets
List ReplicaSets
kubectl describe replicasets <replicaset_name>
Show the detailed state of one or more ReplicaSets
kubectl scale –replicas=[x]
Scale a ReplicaSet [x is a number here]
  • Listing Resources
kubectl get namespaces
Create a plain-text list of all namespaces
kubectl get pods
Create a plain-text list of all pods
kubectl get pods -o wide
Create a comprehensive plain-text list of all pods
kubectl get pods–field-selector=spec. nodeName=[server-name]
Create a list of all pods functioning on a certain node server
kubectl get replicationcontroller [replication-controller-name]
In plain text, make a lst a specific replication controller
kubectl get replicationcontroller, services
Generate a plain-text list of all replication services and controllers
  • Logs
kubectl logs <pod_name>
Print the logs for a pod
kubectl logs –since=1h <pod_name>
Print the logs for a pod for the last hour
kubectl logs –tail=20 <pod_name>
Get the current 20 lines of logs
kubectl logs -f <service_name> [-c <$container>]
Get logs from a service and choose which container optionally
kubectl logs -f <pod_name>
Adhere to new logs and print the logs for a pod
kubectl logs -c <container_name> <pod_name>
For a container in a pod, Print the logs
kubectl logs <pod_name> pod.log
Output the logs for a pod into a ‘pod.log’ file
kubectl logs –previous <pod_name>
View the logs for the last failed pod

In this section, we have seen the list of most basic kubectl commands which are used while administrating a Kubernetes Cluster.

Different ways of managing a Kubernetes cluster 💭 💭

Photo by Christin Hume on Unsplash

Imperative Way :
In simple words, it is a way in which we tell exactly what has to be done and how.

For example if we have to create or delete anything in Kubernetes cluster, we use those commands and tell exactly we want to do this.

kubectl delete deployment/nginxkubectl create service nodeport

Declarative Way :
In simple words, it is a way in which we tell what has to be done but do not tell how. Kubernetes itself detects the things it has to do seeing the changes done in the configuration files.

For example if we have to create or update anything in Kubernetes cluster, we make or update the specific configuration files in the cluster and then use kubectl apply command on those configuration files.

create a deployment yaml file [deployment.yaml]kubectl apply -f deployment.yaml

In above steps, we don’t tell what has to be done rather we use apply command. Kubernetes itself detects the changes done in “deployment.yaml” file and carry out the things it has to do :))

Hope now we are clear about both the Imperative and Declarative approaches of managing a Kubernetes cluster 🙈🙈

What next ? 👀 👀

Thanks a lot for reaching till here! It is the end of this article.
In this article, we have just scratched the basic kubectl commands.
But whatever we have covered is enough to make everyone get started with the K8s journey and no one will face any problems in understanding the basic Kubernetes commands from now onwards.

Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))

In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles

Twitter — https://twitter.com/ChindaVibhor

LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/

Previous articles written by Me :

I will still keep on coming with new articles covering a bunch of topics I am exploring.

That’s All folks !! Doodles :))

--

--

Vibhor Chinda
Google Cloud - Community

Software Developer 2 @Guidewire | Ex - VMware | CKA | Exploring Cloud Tech | Developing Patience ✨✨