Reputation: 4393
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
Upvotes: 106
Views: 317186
Reputation: 2587
For quickly changing between config files on Windows, I wrote this PowerShell function :
# Change k8s config file (KUBECONFIG)
# kubeconf Print current KUBECONFIG
# kubeconf $name Select "$HOME\.kube\$name"
# kubeconf . Select "config" in current dir
# kubeconf ./$name Select $name in current dir
function kubeconf($name) {
if ($name) {
$path = "$HOME\.kube\$name"
if ($name -eq '.') {
$path = ".\config"
} elseif ($name -is [string] -And $name.StartsWith('.')) {
$path = $name
}
[Environment]::SetEnvironmentVariable('KUBECONFIG', "$path", 'User')
$env:KUBECONFIG="$path"
}
echo $env:KUBECONFIG
}
Put this code in $HOME\Documents\PowerShell\Profile.ps1
(you might need to create the file if it doesn't already exist)
Now in a new PowerShell terminal, you can use the command kubeconf
, here's some example usage:
kubeconf # Print current KUEBCONFIG
kubeconf cluster1-config # Select "$HOME\.kube\cluster1-config"
kubeconf config # Select default "$HOME\.kube\config"
kubeconf . # Select "config" in current dir
kubeconf ./config2 # Select "config2" in current dir
It also persists the environment variable between sessions
While you're at it, you could add a couple of command aliases if you use k8s commands a lot:
Set-Alias k kubectl
Set-Alias kc kubeconf
# https://github.com/ahmetb/kubectx
Set-Alias kx kubectx
Set-Alias kn kubens
Upvotes: 0
Reputation: 11289
Here are the official documentation for how to configure kubectl
https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
You have a few options, specifically to this question, you can just copy your admin.conf
to ~/.kube/config
Upvotes: 74
Reputation: 125
This is possible:
export KUBECONFIG=~/.kube/config:~/.kube/cluster0:~/.kube/cluster1:~/.kube/cluster3
and:
kubectl config use-context cluster0
Upvotes: 1
Reputation: 2773
When you type kubectl
I guess you prefer to know which cluster you are pointing. Maybe it's worth creating an alias for that?
alias kube-mycluster='kubectl --kubeconfig ~/.kube/mycluster.conf'
Upvotes: 0
Reputation: 1
Manage your config files proper,place below in your profile file, source the .profile / .bash_profile
for kconfig in $HOME/.kube/config $(find $HOME/.kube/ -iname "*.config") do if [ -f "$kconfig" ];then export KUBECONFIG=$KUBECONFIG:$kconfig fi done
switch the contexts from kubectl
Upvotes: 0
Reputation: 21259
Kubernetes keeps the path to search for config files in $KUBECONFIG
If you want to add one more config path on top of the existing KUBECONFIG without overriding it (and keeping ~/.kube/config
as the default path to search).
Just run the following each time you want to add a conf file to the KUBECONFIG path
export KUBECONFIG=${KUBECONFIG:-~/.kube/config}:/path/to/admin.conf
You can check it worked by listing the available contexts
kubectl config get-contexts
Then select the one you want to use
kubectl config use-context <context-name>
Upvotes: 3
Reputation: 22068
Because there is no built-in kubectl config merge
command at the moment (follow this) you can add this function to your .bashrc
(or .zshrc
):
function kmerge() {
if [ $# -eq 0 ]
then
echo "Please pass the location of the kubeconfig you wish to merge"
fi
KUBECONFIG=~/.kube/config:$1 kubectl config view --flatten > ~/.kube/mergedkub && mv ~/.kube/mergedkub ~/.kube/config
}
Then you can just run from termial:
kmerge /path/to/admin.conf
and the config file will be merged to ~/.kube/config
.
You can now switch to the new context with:
kubectl config use-context <new-context-name>
Or if you're using kubectx (recommended) you can run: kubectx <new-context-name>
.
(The kmerge
function is based on @MichaelSp answer at this post).
Upvotes: 4
Reputation: 5848
Before answers have been very solid and informative, I will try to add my 2 cents here
kubeconfig
file knowing its precedenceIf you’re using kubectl
, here’s the preference that takes effect while determining which kubeconfig file is used.
--kubeconfig
flag, if specifiedKUBECONFIG
environment variable, if specified$HOME/.kube/config
fileWith 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.
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/
Upvotes: 27
Reputation: 1754
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
Upvotes: 13
Reputation: 1025
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
Upvotes: 45
Reputation: 3118
The best way I've found was to use an environment variable:
export KUBECONFIG=/path/to/admin.conf
Upvotes: 90
Reputation: 3917
kubectl
uses ~/.kube/config
as the default configuration file. So you could just copy your admin.conf
over it.
Upvotes: 8