Skip to main content

Kubeconfig

Now, we have a local minikube cluster environment. When we run the commands like:

kubectl get nodes

we are getting some output like

NAME STATUS ROLES AGE VERSION minikube Ready control-plane 5m v1.24.3

This means that our kubectl command-line tool is able to connect to the Kubernetes cluster and retrieve the information about the nodes in the cluster.

How does kubectl know about the cluster we just created?

The kubectl tool uses a configuration file called kubeconfig to determine how to connect to the cluster. The kubeconfig file contains information about the clusters, users, and contexts that kubectl can use to connect to different Kubernetes clusters.

The kubeconfig file is a YAML file that contains information about clusters, users, and contexts. It is used by the kubectl command-line tool to connect to Kubernetes clusters.

The kubeconfig file typically resides in the user's home directory under .kube/config, but you can specify a different location using the KUBECONFIG environment variable.

And, the minikube updated this file with the information about the cluster that it created.

kubeconfig Structure

A kubeconfig file consists of three main sections:

  1. clusters: Contains information about the Kubernetes clusters you want to connect to, including the cluster name, server URL, and certificate authority data.
  2. users: Contains information about the users who can access the clusters, including the user name and authentication details (e.g., client certificates, tokens).
  3. contexts: Contains information about the contexts, which are combinations of clusters and users. Each context specifies which cluster and user to use when connecting.

Example kubeconfig File

apiVersion: v1
clusters:
- cluster:
certificate-authority-data: <base64-encoded-ca-cert>
server: https://my-cluster-api-server:6443
name: my-cluster
contexts:
- context:
cluster: my-cluster
user: my-user
name: my-context
users:
- name: my-user
user:
client-certificate-data: <base64-encoded-client-cert>
client-key-data: <base64-encoded-client-key>

Managing kubeconfig

You can manage multiple clusters and users in a single kubeconfig file. Use the following commands to view and switch contexts:

# View current contexts
kubectl config get-contexts

# Switch to a different context
kubectl config use-context my-context

Conclusion

The kubeconfig file is essential for managing access to Kubernetes clusters. Understanding its structure and how to manage contexts allows you to efficiently work with multiple clusters and users.

Next Steps

Now that we have a cluster which we are able to connect and access, let's start our learning journey with core Kubernetes objects and then move to advanced topics.