Kubectl basics

Apoorv Saran
3 min readJan 16, 2021

--

If you are new to kubectl and you are being pressurized to access the pods of your application ASAP on a windows machine. Then the steps mentioned here might prove of some help to you.

Kubernetes is an open-source system built for orchestrating, scaling, and deploying containerized apps.

The default Kubernetes command-line interface is called kubectl. Kubectl is used to directly manage cluster resources and provide instructions to the Kubernetes API server. The API server then automatically adds and removes containers in your cluster to make sure that the defined desired state and the actual state of the cluster always match.

So, if you delete a pod, its spun up again by kubernetes, to maintain the desired state.

Configuration files ease the process of accessing a cluster, they contain the secrets or key to authenticate the access. (ref. step-5)

STEPS:

  1. Download kubectl.exe executable file.
  2. Add the path of folder where kubectl.exe is kept to the “path” or “PATH” in user or system environment variable.
  3. Like if kubectl.exe is kept at (D:\kubernetes\kubectl.exe), add “D:\kubernetes” in path variable.
  4. Check it in command prompt with the command “kubectl version”. It should not give unknown command.
  5. Get the configuration yaml file for your cluster, it could be different for different environments(like dev, sit, prod etc.)(referred to as contexts in kubernetes). Configuration file can be in JSON or YAML. If it’s in JSON, then convert it into YAML through online converters.
  6. Make sure the yaml file for each environment is in a proper format. apiVersion, clusters, contexts, current-context, kind, preferences, users. These titles(whichever present) should be aligned in a straight line. Save the files with a “.yaml” extension.
  7. Now declare a new environment variable named “KUBECONFIG”, and give its value as the path of every yaml file, separated by a semicolon. Like “D:\kubernetes\Configs\dev.yaml;D:\kubernetes\Configs\sit.yaml;”
  8. If everything is done correctly, the command “kubectl config view”, will display all the config files in the same order as they are declared in KUBECONFIG variable.
  9. If the previous step works fine you can try the command “kubectl get pods” to see the running pods of the current-context. The pods will be from the first context(referring to first config file) you have defined in KUBECONFIG path.

While running in your local machine, if one has multiple configurations(multiple config file paths), priority will be given to the first one.

Few Commands that come in handy:

  1. To set a new context , run the command
  • kubectl config set-context <context-name>

2. To list the contexts

  • kubectl config get-contexts

3. To use a context, or to switch contexts

  • kubectl config use-context <context-name>

4. To view the current context

  • kubectl config current-context

5. To get the list of running pods in the current context.

  • kubectl get pods

6. To get deployment file

  • kubectl get deployments

This blog covers very beginner level information necessary for any engineer to overcome fear of the mighty Kubernetes.

In reality, kubernetes is too vast a topic to be covered in few blogs.

Happy learning!

--

--