Tuesday, October 15, 2024

Kubernetes - Advanced Kubeconfig setup

A Kubeconfig file is a YAML file that stores details about the cluster's API server, the namespace, and user authentication credentials.


Kubernetes tools like kubectl, Helm follow an order of precedence to locate the active Kubeconfig.


Lowest Priority: Default Location


This location is used unless another option overrides it.


Linux

~/.kube/config

macOS

/Users/<username>/.kube/config

Windows

%USERPROFILE%\.kube\config


Next priority: Environment variable


The KUBECONFIG environment variable overrides the default location by specifying one or more Kubeconfig files. For a single file named config-test, the syntax is:


export KUBECONFIG=config-1


Highest priority: Command line


The --kubeconfig command line option has the highest priority, meaning all other Kubeconfig files are ignored. For example, to use a test-kubeconfig file located in the ~/.kube/ directory to request secret objects, you would use the following command:


kubectl --kubeconfig=~/.kube/config-1 get secrets



Merge multiple kubeconfig files


To merge multiple files, list them in the environment variable separated with a colon on Linux


export KUBECONFIG=~/.kube/config-1:~/.kube/config-2


Physically merge config files


To physically merge multiple Kubeconfig files into a single file, first add them to the KUBECONFIG environment variable as before. Then, use the --flatten option to combine them, redirect the output to a new file, and use the merged file as your new Kubeconfig. Here's how you can do it:


export KUBECONFIG=config1:config2:config3

kubectl config view --flatten > merged-kubeconfig


You can then set the KUBECONFIG environment variable to point to the newly merged file:


export KUBECONFIG=merged-kubeconfig

No comments:

Post a Comment