avatarMarc Sanmiquel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2213

Abstract

</span>

<span class="hljs-attr">users:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">minikube</span> <span class="hljs-attr">user:</span> <span class="hljs-attr">client-certificate:</span> <span class="hljs-string">/Users/test/.minikube/profiles/minikube/client.crt</span> <span class="hljs-attr">client-key:</span> <span class="hljs-string">/Users/test/.minikube/profiles/minikube/client.key</span></pre></div><p id="3c48"><b>Clusters:</b> this section lists each cluster you have access to, each one containing details about the cluster name, the URL of the K8s API server, and how to access it (in this case, via a certificate authority).</p><p id="04fc"><b>Contexts:</b> this section lists each context that you can use to specify, for instance, which user and cluster to use for a given command. It comes with information such as cluster and context name, user, and namespace.</p><p id="2b6e"><b>Users: </b>this section lists each user you can use to authenticate with the K8s API by providing a unique user name and authentication credentials such as credentials, client certificates, bearer tokens, or even a proxy.</p><p id="0f9a">Now that we all know what a kubeconfig file is and what it consists of, let’s see some useful tips to manage it like a pro!</p><h1 id="35c4">Tips</h1><h2 id="48fb">1. Loading hierarchy</h2><p id="535e">Assuming we are using the <code>kubectl</code> tool, this is the loading hierarchy from most to least preference to choose which kubeconfig file is used.</p><ol><li>The <code>--kubeconfig</code> flag.</li><li>The <code>KUBECONFIG</code> environment variable.</li><li>Otherwise, <code>{HOME}/.kube/config</code> default file is used.</li></ol><div id="0ea2"><pre><span class="hljs-comment">#1</span> kubectl get deployments --kubeconfig=custom_config <span class="hljs-comment">#2</span> KUBECONFIG=custom_config kubectl get deployments</pre></div><h2 id="719f">2. Concatenate multiple kubeconfig files</h2><p id="10e6">Imagine you need to work with multiple clusters, and you don’t want to be switching from one kubeconfig file to another one every time you switch the cluster. To avoid this, you can

Options

load multiple files to be used at once as follows:</p><div id="7ecb"><pre>KUBECONFIG=/.kube/config:file2:file3 kubectl --context=minikube get pods</pre></div><p id="8e3b">Note the <code>--context</code> flag to decide on what kubeconfig context we are performing the request to get the pods.</p><h2 id="37a8">3. Merging and extracting multiple kubeconfig files</h2><p id="5f5d">Alternatively, to tip number 2 and to avoid having to search for multiple files on your disk, we can merge all the files into one where we’ll have all of our contexts in a single place.</p><div id="118a"><pre>KUBECONFIG=/.kube/config:file2:file3
kubectl config view --merge --flatten > config.yaml</pre></div><p id="716e">Reversely, you can extract a given context from a kubeconfig file into a separate file:</p><div id="db58"><pre>KUBECONFIG=config.yaml kubectl config view
--minify --context?minikube > minikube.yaml</pre></div><h2 id="067b">4. Using kubectl without a config file</h2><p id="8f8a">Probably we’ll never need nor want to do this, but there’s a way of running <code>kubectl</code> without a kubeconfig file. We need to set the <code>$KUBECONFIG</code>environment variable to blank and pass the server, user, client key, and certificate as flags.</p><div id="8c06"><pre>KUBECONFIG= kubectl --server=https://127.0.0.1:62040
--user=minikube
--client-certificate=client.crt
--client-key=client.key</pre></div><h1 id="e566">Interesting Tools</h1><p id="d7c5">I work with a couple of tools that I strongly recommend for their ease of use when working with multiple clusters:</p><ul><li><a href="https://github.com/ahmetb/kubectx">kubectx</a>: a tool to switch between contexts on kubectl easily. Bonus: the repository comes with the <code>kubens</code> tool as well to switch between Kubernetes namespaces.</li><li><a href="https://github.com/jonmosco/kube-ps1">kube-ps1</a>: a Kubernetes prompt for <code>bash</code> and <code>zsh</code> that adds the current Kubernetes context and namespace to your prompt.</li></ul><p id="b0e9">Thank you for reading! I hope this article makes your experience with kubeconfig files a much easier and more rewarding experience!</p></article></body>

Demystifying Kubeconfig File

Tips to use it like a pro!

Source

Kube… what?

Kubeconfig is a file used to configure access to Kubernetes by storing information about clusters, users, and contexts.

It provides a way for users to easily switch between different clusters and accounts and specify authentication credentials to access the Kubernetes API.

It is normally used in conjunction with the kubectl command-line tool as well as by libraries and another tooling that interacts with the Kubernetes API.

A typical kubeconfig file could have the following structure:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
    certificate-authority: /Users/test/.minikube/ca.crt
    server: https://127.0.0.1:62040
  name: minikube

contexts:
- context:
    cluster: minikube
    user: minikube
    namespace: default
  name: minikube
current-context: minikube

users:
- name: minikube
  user:
    client-certificate: /Users/test/.minikube/profiles/minikube/client.crt
    client-key: /Users/test/.minikube/profiles/minikube/client.key

Clusters: this section lists each cluster you have access to, each one containing details about the cluster name, the URL of the K8s API server, and how to access it (in this case, via a certificate authority).

Contexts: this section lists each context that you can use to specify, for instance, which user and cluster to use for a given command. It comes with information such as cluster and context name, user, and namespace.

Users: this section lists each user you can use to authenticate with the K8s API by providing a unique user name and authentication credentials such as credentials, client certificates, bearer tokens, or even a proxy.

Now that we all know what a kubeconfig file is and what it consists of, let’s see some useful tips to manage it like a pro!

Tips

1. Loading hierarchy

Assuming we are using the kubectl tool, this is the loading hierarchy from most to least preference to choose which kubeconfig file is used.

  1. The --kubeconfig flag.
  2. The $KUBECONFIG environment variable.
  3. Otherwise, ${HOME}/.kube/config default file is used.
#1
kubectl get deployments --kubeconfig=custom_config
#2
KUBECONFIG=custom_config kubectl get deployments

2. Concatenate multiple kubeconfig files

Imagine you need to work with multiple clusters, and you don’t want to be switching from one kubeconfig file to another one every time you switch the cluster. To avoid this, you can load multiple files to be used at once as follows:

KUBECONFIG=~/.kube/config:file2:file3 kubectl --context=minikube get pods

Note the --context flag to decide on what kubeconfig context we are performing the request to get the pods.

3. Merging and extracting multiple kubeconfig files

Alternatively, to tip number 2 and to avoid having to search for multiple files on your disk, we can merge all the files into one where we’ll have all of our contexts in a single place.

KUBECONFIG=~/.kube/config:file2:file3 \
    kubectl config view --merge --flatten > config.yaml

Reversely, you can extract a given context from a kubeconfig file into a separate file:

KUBECONFIG=config.yaml kubectl config view \
    --minify --context?minikube > minikube.yaml

4. Using kubectl without a config file

Probably we’ll never need nor want to do this, but there’s a way of running kubectl without a kubeconfig file. We need to set the $KUBECONFIGenvironment variable to blank and pass the server, user, client key, and certificate as flags.

KUBECONFIG= kubectl --server=https://127.0.0.1:62040 \
    --user=minikube \
    --client-certificate=client.crt \
    --client-key=client.key

Interesting Tools

I work with a couple of tools that I strongly recommend for their ease of use when working with multiple clusters:

  • kubectx: a tool to switch between contexts on kubectl easily. Bonus: the repository comes with the kubens tool as well to switch between Kubernetes namespaces.
  • kube-ps1: a Kubernetes prompt for bash and zsh that adds the current Kubernetes context and namespace to your prompt.

Thank you for reading! I hope this article makes your experience with kubeconfig files a much easier and more rewarding experience!

Kubernetes
Programming
Software Development
Software Engineering
Development
Recommended from ReadMedium