How to authenticate kubectl using environment variables?

6.8k views Asked by At

The only two ways I can find to authenticate is by creating a new authentication context, e.g.

kubectl config set-credentials gajus/foo --token=foo
kubectl config set-cluster foo --insecure-skip-tls-verify=true --server=https://127.0.0.1
kubectl config set-context default/foo/gajus --user=gajus/foo --namespace=default --cluster=foo
kubectl config use-context default/foo/gajus

and by using the command line options, e.g.

kubectl --server=https://127.0.0.1 --insecure-skip-tls-verify=true --token=foo get po

Is there a way to set values for --server and other authentication options using environment variables?

1

There are 1 answers

0
Janos Lenart On

The configuration file for credentials live under $HOME/.kube/config (kubeconfig). You can create multiple configuration files like that and use the KUBECONFIG environment variable to point to the file you want to use for the current session.

export KUBECONFIG=~/.kube/config-foo
kubectl config set-credentials gajus/foo --token=foo
kubectl config set-cluster foo --insecure-skip-tls-verify=true --server=https://127.0.0.1
kubectl config set-context default/foo/gajus --user=gajus/foo --namespace=default --cluster=foo
kubectl config use-context default/foo/gajus

export KUBECONFIG=~/.kube/config-bar
...

KUBECONFIG=$HOME/.kube/config-foo kubectl get pod
KUBECONFIG=$HOME/.kube/config-bar kubectl get pod