Skip to main content

Working with Contexts and Kubeconfig

1. Viewing Current Context

Command Description Example
kubectl config current-context Displays the current context name kubectl config current-context
kubectl config view Displays the full kubeconfig file content kubectl config view
kubectl config view -o json Displays the kubeconfig in JSON format kubectl config view -o json

Example: Current Context Output

minikube
Note: The current context is used by default when running kubectl commands unless specified otherwise.

2. Listing All Contexts

Command Description Example
kubectl config get-contexts List all contexts in the kubeconfig file kubectl config get-contexts
kubectl config get-contexts -o name Output only context names kubectl config get-contexts -o name
kubectl config get-contexts --no-headers Display contexts without headers kubectl config get-contexts --no-headers

Example: Output of kubectl config get-contexts

CURRENT   NAME           CLUSTER        AUTHINFO       NAMESPACE
*         minikube       minikube       minikube
          kind-kind      kind-kind      kind-admin
          docker-desktop docker-desktop docker-desktop

3. Switching Contexts

Context Field Description Example Value
name Unique identifier for the context production
cluster Name of the cluster to use production-cluster
user Name of the user to authenticate as admin

Example: Switching Contexts

kubectl config use-context production
Note: The context must exist in your kubeconfig file to switch to it.

Example: Viewing Current Context

kubectl config current-context

Example: Listing All Contexts

kubectl config get-contexts

4. Creating New Context

Example: Create a new context using kubectl

kubectl config set-context my-cluster --cluster=production-cluster --user=prod-user
Option Description Usage
--cluster Name of the cluster to use in the context kubectl config set-context my-context --cluster=production-cluster
--user Name of the user to use in the context kubectl config set-context my-context --user=prod-user
--namespace Default namespace to use in the context kubectl config set-context my-context --namespace=default
Note: The new context is automatically set as the current context if you do not specify --current.

5. Renaming Contexts

Operation Description Command Notes
kubectl config rename-context Renames a context in kubeconfig kubectl config rename-context <old-name> <new-name> Updates the kubeconfig file directly

Example: Renaming a Context

kubectl config rename-context prod-cluster prod-staging
Note: The context name must be unique within the kubeconfig file. Ensure no other context uses the new name before renaming.

6. Deleting Contexts

Context Property Description Value Example
name Name of the context to delete my-context

Example: Delete a Context

kubectl config delete-context my-context
Note: Deleting a context removes the association between the context name and its configuration, but does not delete the actual cluster or user credentials.

7. Setting Context Namespace

Property Description Value
namespace The default namespace for resources in this context default

Example: Setting Namespace in kubeconfig

apiVersion: v1
clusters:
- cluster:
    server: https://kubernetes.example.com
  name: production
contexts:
- context:
    cluster: production
    namespace: frontend-app
    user: admin
  name: prod-frontend
current-context: prod-frontend
users:
- name: admin
  user:
    client-certificate-data: <cert-data>
    client-key-data: <key-data>
Note: Setting the namespace in a context affects all commands run with that context. It does not change the actual namespace of existing resources.

8. Merging Kubeconfig Files

Example: Merge kubeconfig files

kubectl config view --raw | kubectl config merge <file1> <file2>
Option Description Usage
--raw Output raw kubeconfig content without merging kubectl config view --raw
--merge Merge multiple kubeconfig files into one kubectl config merge <file1> <file2>
Note: Merging kubeconfig files can overwrite existing configurations. Always backup your current config before merging.

9. Viewing Kubeconfig Content

Example: Display current kubeconfig

kubectl config view
Field Description Example Value
clusters List of cluster configurations [{"cluster": {"certificate-authority-data": "...", "server": "https://k8s.example.com"}, "name": "prod-cluster"}]
contexts List of context configurations [{"context": {"cluster": "prod-cluster", "user": "admin"}, "name": "prod-context"}]
users List of user configurations [{"name": "admin", "user": {"client-certificate-data": "...", "client-key-data": "..."}}]

Example: View specific context

kubectl config view --context=prod-context

Example: Show current context

kubectl config current-context
Note: The kubeconfig file is typically located at ~/.kube/config.

10. Setting Cluster Server

Property Value Description
clusters.name my-cluster Name of the cluster as defined in kubeconfig
clusters.cluster.server https://192.168.1.100:6443 API server URL for the cluster

Example: Setting Cluster Server URL

clusters:
- cluster:
    server: https://192.168.1.100:6443
  name: my-cluster

11. Setting User Credentials

Property Description Example
user.name Name of the user admin
user.client-certificate Path to client certificate file /path/to/client.crt
user.client-key Path to client key file /path/to/client.key
user.token Authentication token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
user.username Username for basic auth admin
user.password Password for basic auth secret123

Example: Configure User Credentials

apiVersion: v1
kind: Config
clusters:
- cluster:
    server: https://k8s.example.com
  name: example-cluster
contexts:
- context:
    cluster: example-cluster
    user: admin-user
  name: example-context
current-context: example-context
users:
- name: admin-user
  user:
    client-certificate: /path/to/client.crt
    client-key: /path/to/client.key
Note: Ensure the paths to certificate and key files are correct and accessible by the kubeconfig file.

Was this article helpful?