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 viewdisplays the merged kubeconfigconfig get-contextslists all available contextsconfig use-contextswitches your active cluster connectionconfig set-context --currentmodifies your current context in place- Delete commands clean up unused entries