KubeConfig
On this page
kubeconfig is a configuration file used by kubectl, the Kubernetes command-line tool, to interact with a Kubernetes cluster. It contains essential information and settings that kubectl needs to connect to a cluster, including the cluster’s API server address, authentication credentials, and context.
kubeconfig Breakdown
Here’s a breakdown of the key elements and purposes of a kubeconfig file:
Clusters
A kubeconfig file can specify multiple clusters. Each cluster entry contains information about a Kubernetes cluster, including the API server address, which is used to connect to the cluster.
Credentials
For each cluster, there is an associated credentials section. This section includes the authentication method and the necessary credentials for accessing the cluster. Common authentication methods include:
- Username and Password: Basic authentication using a username and password.
- Client Certificates: Authentication using TLS client certificates.
- Token: Authentication using an authentication token.
- Auth Provider: Authentication through an external identity provider, like OIDC or GCP.
Contexts
A context combines a cluster and credentials, allowing you to define a specific environment for kubectl to use. It specifies the cluster and user to use when executing commands. A kubeconfig file can have multiple contexts, making it easy to switch between different clusters or user identities.
Current Context
The current-context field in the kubeconfig file indicates which context is currently active. When you run kubectl commands, they use the settings from the current context by default.
User Preferences
A kubeconfig file can include additional preferences and settings, such as the default namespace to use when creating or interacting with resources.
Here’s an example of a simplified kubeconfig file:
---
apiVersion: v1
kind: Config
current-context: my-cluster-context
clusters:
- name: my-cluster
cluster:
server: https://api.my-k8s-cluster.com
certificate-authority: /path/to/ca.crt
- name: another-cluster
cluster:
server: https://api.another-k8s-cluster.com
certificate-authority: /path/to/another-ca.crt
users:
- name: my-user
user:
client-certificate: /path/to/client.crt
client-key: /path/to/client.key
- name: another-user
user:
username: john
password: secretpassword
contexts:
- name: my-cluster-context
context:
cluster: my-cluster
user: my-user
namespace: my-namespace
- name: another-cluster-context
context:
cluster: another-cluster
user: another-user
In this example:
- There are two clusters (
my-clusterandanother-cluster), each with its API server address. - Two users (
my-userandanother-user) with different authentication methods. - Two contexts (
my-cluster-contextandanother-cluster-context) that specify which cluster and user to use. my-cluster-contextis set as thecurrent-context, so when you runkubectlcommands, it will use the settings defined in this context by default.- The
namespacefield is added to context definition (e.g.,namespace: my-namespace). This specifies the default namespace formy-cluster-contextcontext.
To switch between contexts and clusters, you can use the kubectl config use-context command. For example, to switch to the another-cluster-context, you’d run:
kubectl config use-context another-cluster-context
Converting certificate-authority to certificate-authority-data
You can specify the certificate-authority field either as a file path (certificate-authority) or as a base64-encoded certificate data (certificate-authority-data). If you have a CA certificate file and want to convert it to the certificate-authority-data format, you can do so using the base64 command.
Here are the steps to convert a CA certificate file to certificate-authority-data:
Encode the CA Certificate:
Use the
base64command to encode the contents of your CA certificate file:cat /path/to/ca.crt | base64This will output the base64-encoded certificate data. Copy this encoded data.
Update
kubeconfig:Update your
kubeconfigfile by replacing thecertificate-authorityfield withcertificate-authority-dataand paste the base64-encoded data. Here’s an example:clusters: - name: my-cluster cluster: server: https://api.my-k8s-cluster.com certificate-authority-data: <base64-encoded-certificate-data>Replace
<base64-encoded-certificate-data>with the actual base64-encoded certificate data you obtained in step 1.Save and Test:
Save your updated
kubeconfigfile. You can now usekubectlwith this configuration, and it will use thecertificate-authority-datafor the cluster’s certificate authority.kubectl get pods
This conversion allows you to keep the CA certificate directly within the kubeconfig file as base64-encoded data, which can be useful for cases where you don’t want to rely on external certificate files.
Cheatsheet
Here’s a Kubernetes kubectl configuration cheat sheet that summarizes some of the most commonly used commands and operations for managing kubeconfig files:
Viewing and Managing Configuration
View the current context, cluster, and user:
kubectl config current-contextList all available contexts:
kubectl config get-contextsSwitch to a different context:
kubectl config use-context <context-name>Set the namespace for the current context:
kubectl config set-context --current --namespace=<namespace-name>Create a new context (combine an existing cluster and user):
kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name>
Cluster Configuration
List all clusters defined in the
kubeconfig:kubectl config get-clustersDisplay detailed cluster information:
kubectl config view --minify --flatten -o jsonpath='{.clusters[0]}'
User Configuration
List all users defined in the
kubeconfig:kubectl config get-usersDisplay detailed user information:
kubectl config view --minify --flatten -o jsonpath='{.users[0]}'
Context Configuration
List all available contexts:
kubectl config get-contextsDisplay detailed context information:
kubectl config view --minify --flatten -o jsonpath='{.contexts[0]}'
Context and Cluster Switching
Switch to a different context:
kubectl config use-context <context-name>Switch to a different cluster within the current context:
kubectl config set-context --current --cluster=<cluster-name>Switch to a different user within the current context:
kubectl config set-context --current --user=<user-name>
Managing kubeconfig Files
Specify a custom
kubeconfigfile location:kubectl --kubeconfig=/path/to/custom/config.yaml <command>Merge two or more
kubeconfigfiles into one:KUBECONFIG=/path/to/first/config.yaml:/path/to/second/config.yaml kubectl config view --mergeSet a
kubeconfigfile temporarily for a single command:KUBECONFIG=/path/to/custom/config.yaml kubectl <command>
Remember to replace placeholders like <context-name>, <cluster-name>, <user-name>, and <namespace-name> with your actual configuration values. The kubectl config commands allow you to efficiently manage and switch between different Kubernetes clusters and contexts, making it easier to work with multiple environments.