What is kubeconfig, and how can we use it with kubectl?

A file that is used to configure access to Kubernetes clusters is conventionally referred to as the kubeconfig file.

You can use the kubeconfig file to organize information about your Kubernetes clusters. This information is then used by the kubectl command-line tool to access and communicate with clusters through the API server of a cluster.

kubeconfig details

The kubeconfig file is used together with the kubectl command-line tool to configure access to your Kubernetes clusters.

The KUBECONFIG environment variable holds a list of kubeconfig files.

A context element exists within the kubeconfig file. You can use this element to group access parameters. Each context element has three parameters.

  • Cluster: the relevant Kubernetes cluster
  • Namespace: Kubernetes objects that partition a single cluster into multiple virtual clusters
  • Users: user’s details

Working with kubeconfig and kubectl

To begin interacting with your Kubernetes clusters through your local machine, you will need to install kubectl binary.

kubectl is used to control the Kubernetes cluster manager.

The kubectl command-line tool uses the kubeconfig file to access and communicate with your Kubernetes clusters. You can use this tool to view logs and access/inspect clusters.

kubectl can be used to run commands against your kubernetes clusters. A list of kubectl options is available here, the Kubernetes official documentation.

If the KUBECONFIG environment variable is not set, kubectl will merge the files listed in the KUBECONFIG environment variable. Hence, the KUBECONFIG environment variable is not essential for configuration.

By default, kubectl checks ~/.kube/config. for a kubconfig file. However, the directory can be changed using the --kubeconfig flag.

To communicate with clusters, kubectl uses the parameters from the current context. You can choose the current context by running kubectl config use-context.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved