DevOps Notebook: Most useful kubectl Commands !

Kubernetes Family !
Kubernetes Family !

Kubectl is a command line utility which allows you to interact with kubernetes cluster and run commands against it. Behind the curtain, It actually interacts with Kubernetes API to perform all operations.

Kubectl connect to K8s cluster with help of a config file. Default location of this config file kube directory which stays at You can pass custom location by using --kubeconfig flag.

Syntex for Kubectl commands -

kubectl [command] [TYPE] [NAME] [flags]command - Type of operation eg: get, create, delete, apply etctype - Resource TypeName - name of the resourceflags - optional flags

Now lets go back a little bit and try to understand how this works. Kubernetes API is an HTTP REST API and main door to connect or interact with K8s cluster. All operations happen in K8s cluster are actually happen through it. I am not going to cover how HTTP REST API works but spending sometime will be very helpful for you.

Kubectl also connects with this API endpoint using HTTP requests. So when you run a command using Kubectl, is actually a communication with API. So if you run a command like creating a POD !

kubectl create -f pod.yaml

You run this command in terminal but kubectl is communicating with API Endpoint behind the scene.

POST /api/v1/namespaces/{namespace}/podsRef: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#podlist-v1-core

This happens in every case and this is actual power of kubernetes. You can find the API endpoints of all Kubernetes operations in the API reference.

Now lets get back to our main topic. The commands !

There is long list of kubectl commands which you have to use in day to day operations. You can search it on google and help yourself. But some are important ones which can make your like easy if you remember them.

api-versions — It will List the API versions that are available. It is really helpful sometimes if you want to verify that required API is available to use or not.

[root@3fdadaa4500d Kubernetes]# kubectl api-versions
adca.evops.eu/v1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
...

api-resources — List the API resources that are available to use. This is helpful when you forgot your resource name to use for operations. It will list short names too. You can list all registered API resources.

help — As the name says it will help you to list the options available to run the commands.

So now you have got resources,versions and help. They complete this command line thing in a larger aspect. But there are few more. Bare with me.

explain — It gives you a lot information of the k8s resources and can help you to create yaml structure too. You can see full schema for service resources in below example. (This is one of my favourite command)

exec — Allows you to execute a command inside a container. It opens a channel in which you can login in a container too ! You can either directly run the command or login inside the container, your choice. But very useful !

kubectl exec nginx -- datekubectl exec nginx -it /bin/bash# if you want to go for specific container in multi-container podkubectl exec <pod-name> -c <container-name> -- date

set-context — You may need to interact with multiple k8s clusters at a time. In that case set-context will help you to organise and work. A context is a group of access parameters. Each context contains a Kubernetes cluster, a user and a namespace. Kubectl by default runs command against default context.

That’s all for this blog.

Keep Learning !

In quest of understanding How Systems Work !

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store