The kubeconfig file is a YAML file containing grops of clusters, users, and contexts.

Kubernetes components like kubelet, kube-controller-manager, or kubectl use the kubeconfig file to interact with the Kubernetes API. Usually, the kubectl or oc commands use the kubeconfig file.

The default location for the kubeconfig file is in the ~/.kube directory and the file is named config. (e.g. ~/.kube/config). You can also specify the KUBECONFIG environment variable to point to a different kubeconfig file.

Contents:

  • cluster: A kubernetes cluster
  • user: A credential used to interact with the kubernetes api
  • context: a cluster-user pair. Every command references a context

Example:

apiVersion: v1
kind: Config

clusters:
  - name: my-cluster
    cluster:
      certificate-authority: ~/.minikube/ca.crt
      server: https://192.168.39.217:8443

users:
  - name: my-user
    user:
      client-certificate: ~/.minikube/profiles/minikube/client.crt
      client-key: ~/.minikube/profiles/minikube/client.key

contexts:
  - name: my-context
    context:
      cluster: my-cluster
      user: my-user
      namespace: default

current-context: my-context

If you use a tool like kubectl or oc, you can switch the context with the following:

kubectl config use-context my-context

The kubectl config use-context command sets the current context to my-context.

You can also use the kubectl config get-contexts command to list all the contexts in the kubeconfig file.

kubectl config get-contexts

The kubectl config get-contexts command lists all the contexts in the kubeconfig file.

The kubectl config current-context command shows the current context.

kubectl config current-context

The kubectl config current-context command shows the current context.