Mastering the kubeconfig File: A Friendly Guide

Anju
8 min readOct 4, 2024

--

If you’re working with Kubernetes, you’ve likely encountered the kubeconfig file. It’s one of those things that, at first glance, can seem a bit mysterious—full of YAML structures, cryptic paths, and names. But once you understand its purpose, it becomes an invaluable tool for managing multiple clusters and users with ease.

In this post, I’ll walk you through the kubeconfig file, explain its components, and share some useful commands to help you navigate Kubernetes more effectively.

1. What’s a Kubeconfig File, Anyway?

So, let’s start with the basics. The kubeconfig file is essentially your Kubernetes access pass. It tells kubectl how to find and authenticate with a Kubernetes cluster. Think of it as a blueprint that stores information about clusters, contexts, and users.

Here’s a glimpse of what a typical kubeconfig file looks like:

apiVersion: v1
kind: Config
clusters:
- name: prod-cluster
cluster:
certificate-authority: /etc/kubernetes/pki/ca-prod.crt
server: https://prod-cluster-api.example.com:6443
- name: staging-cluster
cluster:
certificate-authority: /etc/kubernetes/pki/ca-staging.crt
server: https://staging-cluster-api.example.com:6443
- name: dev-cluster
cluster:
certificate-authority: /etc/kubernetes/pki/ca-dev.crt
server: https://dev-cluster-api.example.com:6443
- name: test-cluster
cluster:
certificate-authority: /etc/kubernetes/pki/ca-test.crt
server: https://test-cluster-api.example.com:6443

contexts:
- name: prod-admin@prod-cluster
context:
cluster: prod-cluster
user: prod-admin
namespace: production
- name: staging-dev@staging-cluster
context:
cluster: staging-cluster
user: staging-dev
namespace: staging
- name: dev-user@dev-cluster
context:
cluster: dev-cluster
user: dev-user
namespace: development
- name: test-user@test-cluster
context:
cluster: test-cluster
user: test-user
namespace: testing

current-context: dev-user@dev-cluster

users:
- name: prod-admin
user:
client-certificate: /etc/kubernetes/pki/users/prod-admin/prod-admin.crt
client-key: /etc/kubernetes/pki/users/prod-admin/prod-admin.key
- name: staging-dev
user:
client-certificate: /etc/kubernetes/pki/users/staging-dev/staging-dev.crt
client-key: /etc/kubernetes/pki/users/staging-dev/staging-dev.key
- name: dev-user
user:
client-certificate: /etc/kubernetes/pki/users/dev-user/dev-user.crt
client-key: /etc/kubernetes/pki/users/dev-user/dev-user.key
- name: test-user
user:
client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
client-key: /etc/kubernetes/pki/users/test-user/test-user.key

The first time I saw this, I thought, “YAML, clusters, certificates… what could possibly go wrong?” Well, the short answer is: plenty, if you don’t understand what’s going on here. But don’t worry — we’ll break it down.

2. Diving Into the Kubeconfig File: Breaking It Down

a. Clusters

This section is where you define the Kubernetes clusters you want to connect to. Each entry has:

  • name: This is the human-friendly name for your cluster. You’ll use this to reference the cluster later.
  • certificate-authority: This is the path to the CA (certificate authority) file that validates the Kubernetes API server's identity. Without this, you might be chatting with a stranger (not good!).
  • server: The URL for the cluster’s API server.

certificate-authority vs certificate-authority-data

certificate-authority (File-based CA)

The certificate-authority field specifies the path to the file containing the Certificate Authority (CA) certificate. This CA certificate is used by kubectl to verify the authenticity of the Kubernetes API server it is communicating with.

  • Purpose: It ensures that kubectl is talking to the real Kubernetes API server and not an impersonator.
  • Format: This field holds a reference to a file path that contains the CA certificate in PEM format.
clusters:
- cluster:
certificate-authority: /etc/kubernetes/pki/ca-prod.crt
server: https://prod-cluster-api.example.com:6443
name: prod-cluster

In this example, the kubectl client will read the CA certificate from /etc/kubernetes/pki/ca-prod.crt to verify the server's identity.

certificate-authority-data (Embedded Base64-encoded CA)

The certificate-authority-data field is used to embed the actual CA certificate directly in the kubeconfig file as a Base64-encoded string. This eliminates the need for a separate file on disk. It’s particularly useful when you want to distribute a kubeconfig file that contains everything necessary to connect to the Kubernetes API, without needing extra files.

  • Purpose: It allows the CA certificate to be included directly in the kubeconfig file, avoiding the need to reference an external file.
  • Format: The CA certificate is embedded as a Base64-encoded string.
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JS...
server: https://staging-cluster-api.example.com:6443
name: staging-cluster

In this example, the CA certificate is directly included in the kubeconfig as a Base64 string, ensuring the certificate is bundled with the config.

Key Differences:

  • File-based (certificate-authority): Refers to a certificate stored in a file on the local filesystem.
  • Embedded (certificate-authority-data): Contains the certificate inline as a Base64 string within the kubeconfig itself.

Pro tip: If you’re like me, juggling different clusters (dev, prod, staging), you’ll want to make sure these names are clear and descriptive. It’s easy to accidentally deploy something to production when you meant to deploy it to development — trust me, I’ve been there!

b. Contexts

A context links a cluster with a specific user. This is super useful when you’re managing different clusters, environments, or even multiple user roles (like admin vs. read-only). Contexts help you switch between different environments easily.

In the example:

  • The context test-user@development uses the development cluster and authenticates as test-user.

Context Fileds:

  • name: This is the context’s name, which should clearly identify the environment it refers to, such as dev-context, prod-context, or staging-context.
  • cluster: This references the cluster the context is linked to. In production, make sure this matches exactly with one of the clusters defined in the clusters section.
  • user: This links to a user defined in the users section. Using service accounts or dedicated user accounts per environment is a best practice in production.
  • namespace: (Optional) Specifies the default namespace for the context. If not provided, operations will default to the default namespace. In production, it's safer to always explicitly set the namespace to avoid accidental resource modifications in unintended namespaces.

c. Users

This section contains the credentials used to authenticate with your Kubernetes clusters. You’ll typically see fields like:

  • name: Identifies the user. In production environments, names should be clear and descriptive, such as admin-user or service-account-prod. Avoid ambiguous names like user1.
  • client-certificate & client-key: These fields specify the paths to the user's client certificate and private key, used for mutual TLS authentication. Ensure these files are secure and their permissions are restricted. Certificates should be issued by a trusted CA and rotated regularly in production.
  • token: Alternatively, a bearer token can be used for authentication. This is common in production environments that integrate with cloud provider IAM systems. Tokens should be treated with the highest security, as they provide direct access to the cluster.
  • username & password: These fields support basic authentication but should not be used in production due to the inherent security risks. Instead, use certificates or tokens for authentication.

Here’s where I ran into some confusion early on. One day, I kept getting permission denied errors, and it turned out the client-certificate path in my kubeconfig was wrong. A quick update, and I was back in business. Lesson learned: double-check your file paths!

d. Current Context

The current-context field defines the active context that kubectl uses by default when no context is explicitly provided.

current-context: <context-name>

In a production environment, it’s critical to always verify the current context before running commands. This can prevent accidental operations in the wrong environment. Use kubectl config current-context to view the active context, and kubectl config use-context <context-name> to switch as needed.

e. Preferences

preferences: {}

This field is often left empty, but it allows for setting client-side preferences such as the colors used in kubectl output.

3. Where you can find it?

1. Default Location:
The default kubeconfig file is located in your home directory under .kube/config.

cat ~/.kube/config

2. Custom Locations:
If you manage multiple clusters, you may have several kubeconfig files in different directories. You can specify a custom kubeconfig file by setting the KUBECONFIG environment variable:

export KUBECONFIG=/path/to/your/custom-kubeconfig

Or you can pass the custom file directly to the kubectl command:

kubectl --kubeconfig=/path/to/custom-kubeconfig get pods

Precedence for kubeconfig Files

When using kubectl, Kubernetes determines which configuration file to use by following a specific order of precedence. This is important when managing multiple clusters or environments.

Precedence Order for kubeconfig Files:

  1. Command-Line Argument (--kubeconfig): If you explicitly specify the --kubeconfig option when running a kubectl command, it will always take precedence over other sources.
  2. KUBECONFIG Environment Variable: If the --kubeconfig option is not provided, Kubernetes will check if the KUBECONFIG environment variable is set. This variable can include multiple kubeconfig files separated by a colon (:) on Linux/macOS or a semicolon (;) on Windows.
export KUBECONFIG=/path/to/first-kubeconfig:/path/to/second-kubeconfig
kubectl get pods

3. Default Location (~/.kube/config):

  • If neither --kubeconfig nor the KUBECONFIG environment variable is set, Kubernetes will default to the configuration file located in ~/.kube/config (Linux/macOS) or %USERPROFILE%\.kube\config (Windows).

4. Commands You’ll Love Using with Kubeconfig

Once you’ve got your kubeconfig file set up, you’ll want to manage it like a pro. Here are some handy commands I’ve found useful.

View the Active Configuration:
$ kubectl config view

Set the Active Context:
$ kubectl config use-context <context-name>

Check the Current Context:
$ kubectl config current-context

Help:
$ kubectl config -h


Modify kubeconfig files using subcommands like "kubectl config set current-context my-context".

The loading order follows these rules:

1. If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes
place.
2. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the
last file in the list.
3. Otherwise, ${HOME}/.kube/config is used and no merging takes place.

Available Commands:
current-context Display the current-context
delete-cluster Delete the specified cluster from the kubeconfig
delete-context Delete the specified context from the kubeconfig
delete-user Delete the specified user from the kubeconfig
get-clusters Display clusters defined in the kubeconfig
get-contexts Describe one or many contexts
get-users Display users defined in the kubeconfig
rename-context Rename a context from the kubeconfig file
set Set an individual value in a kubeconfig file
set-cluster Set a cluster entry in kubeconfig
set-context Set a context entry in kubeconfig
set-credentials Set a user entry in kubeconfig
unset Unset an individual value in a kubeconfig file
use-context Set the current-context in a kubeconfig file
view Display merged kubeconfig settings or a specified kubeconfig file

Usage:
kubectl config SUBCOMMAND [options]

Use "kubectl config <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

5. Conclusion

Working with Kubernetes across multiple environments becomes much easier once you understand the kubeconfig file. It’s your key to managing clusters, users, and contexts effectively. Whether you’re a beginner or a seasoned Kubernetes user, mastering kubeconfig is essential for streamlining your workflow.

And remember — always check your context before deploying! It’s a small step, but it can save you from those “Oh no!” moments we all dread.

Happy Kubernetes-ing!

--

--

Anju
Anju

Written by Anju

A DevOps engineer who loves automating everything (almost), exploring new places, and finding peace in nature. Always looking for the next adventure!

No responses yet