How to configure kubectl with cluster information from a .conf file?

11/6/2016

I have an admin.conf file containing info about a cluster, so that the following command works fine:

kubectl --kubeconfig ./admin.conf get nodes

How can I config kubectl to use the cluster, user and authentication from this file as default in one command? I only see separate set-cluster, set-credentials, set-context, use-context etc. I want to get the same output when I simply run:

kubectl get nodes
-- Assen Kolov
kubectl
kubernetes

6 Answers

11/6/2016

Here are the official documentation for how to configure kubectl

http://kubernetes.io/docs/user-guide/kubeconfig-file/

You have a few options, specifically to this question, you can just copy your admin.conf to ~/.kube/config

-- Gleeb
Source: StackOverflow

1/7/2019

The best way I've found was to use an environment variable:

export KUBECONFIG=/path/to/admin.conf
-- stasdeep
Source: StackOverflow

2/7/2019

I just alias the kubectl command into separate ones for my dev and production environments via .bashrc

alias k8='kubectl'
alias k8prd='kubectl --kubeconfig ~/.kube/config_prd.conf'

I prefer this method as it requires me to define the environment for each command.. whereas using an environment variable could potentially lead you to running a command within the wrong environment

-- Jeff Beagley
Source: StackOverflow

11/6/2016

kubectl uses ~/.kube/config as the default configuration file. So you could just copy your admin.conf over it.

-- SeMeKh
Source: StackOverflow

6/10/2019

I name all cluster configs as .kubeconfig and this lives in project directory.

Then in .bashrc or .bash_profile I have the following export:

export KUBECONFIG=.kubeconfig:$HOME/.kube/config

This way when I'm in the project directory kubectl will load local .kubeconfig. Hope that helps

-- Mirceac21
Source: StackOverflow

8/13/2019

Before answers have been very solid and informative, I will try to add my 2 cents here

Configure kubeconfig file knowing its precedence

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

  1. use --kubeconfig flag, if specified
  2. use KUBECONFIG environment variable, if specified
  3. use $HOME/.kube/config file

With this, you can easily override kubeconfig file you use per the kubectl command:

#
# using --kubeconfig flag
#
kubectl get pods --kubeconfig=file1
kubectl get pods --kubeconfig=file2

#
# or 
# using `KUBECONFIG` environment variable
#
KUBECONFIG=file1 kubectl get pods
KUBECONFIG=file2 kubectl get pods

#
# or 
# merging your kubeconfig file w/ $HOME/.kube/config (w/ cp backup)
#
cp $HOME/.kube/config $HOME/.kube/config.backup.$(date +%Y-%m-%d.%H:%M:%S)
KUBECONFIG= $HOME/.kube/config:file2:file3 kubectl config view --merge --flatten > \
~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
kubectl get pods --context=cluster-1
kubectl get pods --context=cluster-2

NOTE: The --minify flag allows us to extract only info about that context, and the --flatten flag allows us to keep the credentials unredacted.

For your example

kubectl get pods --kubeconfig=/path/to/admin.conf

#
# or:
#
KUBECONFIG=/path/to/admin.conf kubectl get pods

#
# or: 
#
cp $HOME/.kube/config $HOME/.kube/config.backup.$(date)
KUBECONFIG= $HOME/.kube/config:/path/to/admin.conf kubectl config view --merge --flatten > \
~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
kubectl get pods --context=cluster-1
kubectl get pods --context=cluster-2

Although this precedence list not officially specified in the documentation it is codified here. If you’re developing client tools for Kubernetes, you should consider using cli-runtime library which will bring the standard --kubeconfig flag and $KUBECONFIG detection to your program.

ref article: https://ahmet.im/blog/mastering-kubeconfig/

-- Exequiel Barrirero
Source: StackOverflow