Sign In

Curriculum 20: Kubeconfig & Contexts

kubectl config Commands

15 min · 35 XP

kubectl config Commands

The kubectl config subcommand is your primary tool for managing kubeconfig entries. It lets you view, create, modify, and switch between contexts without manually editing YAML files.

Viewing Configuration

# Show the full merged kubeconfig
kubectl config view

# Show config with secrets redacted (default behavior)
kubectl config view

# Show raw secret values
kubectl config view --raw

# Show only the current context name
kubectl config current-context

Listing Contexts

# List all contexts with details
kubectl config get-contexts

# Output:
# CURRENT   NAME    CLUSTER        AUTHINFO      NAMESPACE
# *         dev     dev-cluster    dev-admin     default
#           prod    prod-cluster   prod-user     production

# List just context names
kubectl config get-contexts -o name

Switching Contexts

# Switch to a different context
kubectl config use-context prod

# Verify the switch
kubectl config current-context
# Output: prod

Creating and Modifying Contexts

# Create a new context
kubectl config set-context my-context \
  --cluster=my-cluster \
  --user=my-user \
  --namespace=my-namespace

# Update the namespace on an existing context
kubectl config set-context dev --namespace=testing

# Modify the current context's namespace
kubectl config set-context --current --namespace=kube-system

Managing Clusters and Credentials

# Add or update a cluster entry
kubectl config set-cluster new-cluster \
  --server=https://api.example.com:6443

# Add or update user credentials
kubectl config set-credentials new-user \
  --token=my-bearer-token

# Remove a context
kubectl config delete-context old-context

# Remove a cluster entry
kubectl config delete-cluster old-cluster

# Remove a user entry
kubectl config delete-user old-user

Key Takeaways

  • config view displays the merged kubeconfig
  • config get-contexts lists all available contexts
  • config use-context switches your active cluster connection
  • config set-context --current modifies your current context in place
  • Delete commands clean up unused entries