Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate kubectl using environment variables?

Tags:

kubectl

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?

like image 471
Gajus Avatar asked Jan 02 '17 21:01

Gajus


People also ask

How do you use Kubernetes secrets as environment variables?

When you create a Pod in kubernetes, you can set environment variables for the containers that run inside the Pod. To set environment variables you can use 'env' field in the deployment yaml configuration file which used to create the pod.

Which method is used for authentication in Kubernetes?

Kubernetes uses client certificates, bearer tokens, or an authenticating proxy to authenticate API requests through authentication plugins.

What is the use of environment variables in Kubernetes?

Handling Environment Variables With Kubernetes. Environment variables are a common way for developers to move application and infrastructure configuration into an external source outside of application code.


1 Answers

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
like image 60
Janos Lenart Avatar answered Jan 03 '23 17:01

Janos Lenart