Skip to Content

5 Useful Kubeconfig Examples and kubectl config Commands

we can use kubeconfig configuration file to store information about different clusters, users, namespaces, and authentication mechanisms.

Understanding kubeconfig

A context element in a kubeconfig file is used to group access parameters under a convenient name.

Each context has three parameters: cluster, namespace, and user. By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster.

Such configuration files are referred to as kubeconfig files.

Note that kubeconfig is a generic way to refer to kubectl configuration files and that it is not the name of the config file.

kubectl uses such files to store the information needed for us to choose a cluster and communicate with its API server.

By default, kubectl looks for the file in the $HOME/.kube directory. In most scenarios, we can specify a KUBECONFIG environment variable or use the –kubeconfig flag to specify the kubeconfig files. Those files are usually saved in $HOME/.kube/config.

If we are using kubectl, here’s the preference that takes effect while determining which kubeconfig file is used.

  • use –kubeconfig flag, if specified
  • use KUBECONFIG environment variable, if specified
  • use $HOME/.kube/config file

Show Merged kubeconfig settings

kubectl config view

We can use multiple kubeconfig files at the same time and view merged config
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2

kubectl config view

Alternatively, we can also use the following command:
cat $HOME/.kube/config

Use kubectl config to Get the password for the e2e user

kubectl config view -o jsonpath='{.users[?(@.name == “e2e”)].user.password}’

display the first user
kubectl config view -o jsonpath='{.users[].name}’

get a list of users
kubectl config view -o jsonpath='{.users[*].name}’ #

Use kubectl config to Display list of contexts

A context is a set of information that we need to access a cluster. It contains the name of the cluster, the user, and the namespace.

display the current-context
kubectl config current-context

set the default context to my-cluster-name
kubectl config use-context my-cluster-name

Use kubectl config to add a new user

kubectl config set-credentials kubeuser/foo.kubernetes.com –username=kubeuser –password=kubepassword

permanently save the namespace for all subsequent kubectl commands in that context
kubectl config set-context –current –namespace=ggckad-s2

set a context utilizing a specific username and namespace.
kubectl config set-context gce –user=cluster-admin –namespace=foo && kubectl config use-context gce

use kubectl config to delete user foo

kubectl config unset users.foo

kubectl config command

kubectl – kubectl controls the Kubernetes cluster manager
kubectl config current-context – Displays the current-context
kubectl config set – Sets an individual value in a kubeconfig file
kubectl config set-cluster – Sets a cluster entry in kubeconfig
kubectl config set-context – Sets a context entry in kubeconfig
kubectl config set-credentials – Sets a user entry in kubeconfig
kubectl config unset – Unsets an individual value in a kubeconfig file
kubectl config use-context – Sets the current-context in a kubeconfig file
kubectl config view – isplays merged kubeconfig settings or a specified kubeconfig file.