Configuring Kubernetes (kubectl) involves managing various settings and credentials through the kubeconfig file. This file dictates how kubectl interacts with Kubernetes clusters.
Option: config
Title: Configuring kubectl and kubeconfig Files
Below are several examples demonstrating different configurations using kubectl:
Example 1: View Current Context
Command: kubectl config current-context
Description: Displays the current context in use, which determines the cluster and user configuration.
Output (example): my-cluster-name
Verification: Check the context name matches the expected cluster.
Example 2: List All Contexts
Command: kubectl config get-contexts
Description: Lists all available contexts defined in the kubeconfig file.
Output (example):
CURRENT NAME CLUSTER AUTHINFO NAMESPACE my-cluster-name my-cluster-name my-username *
Verification: Ensure all expected contexts are listed.
Example 3: Switch Context
Command: kubectl config use-context my-other-cluster
Description: Changes the current context to ‘my-other-cluster’.
Output (example): Switched to context "my-other-cluster".
Verification: Confirm the context has switched by checking the current context.
Example 4: Set Cluster Server
Command: kubectl config set-cluster my-cluster --server=https://new-server-url --kubeconfig=/path/to/kubeconfig
Description: Configures the server endpoint for the cluster ‘my-cluster’.
Output (example): Cluster "my-cluster" set.
Verification: Verify the new server URL is correctly set in the kubeconfig file.
Example 5: Set Credentials
Command: kubectl config set-credentials my-user --username=admin --password=my-password --kubeconfig=/path/to/kubeconfig
Description: Sets credentials for ‘my-user’ with username and password.
Output (example): Credentials "my-user" set.
Verification: Ensure the credentials are correctly stored in the kubeconfig file.
Example 6: Set Context
Command: kubectl config set-context my-context --cluster=my-cluster --user=my-user --kubeconfig=/path/to/kubeconfig
Description: Defines a new context ‘my-context’ using ‘my-cluster’ and ‘my-user’.
Output (example): Context "my-context" created.
Verification: Confirm the new context is added and configured correctly.
Example 7: Rename Context
Command: kubectl config rename-context old-name new-name
Description: Renames an existing context from ‘old-name’ to ‘new-name’.
Output (example): Context "old-name" renamed to "new-name".
Verification: Check the context list to ensure the rename was successful.
Example 8: Delete Context
Command: kubectl config delete-context my-context
Description: Removes the context ‘my-context’ from the kubeconfig file.
Output (example): Context "my-context" deleted.
Verification: Verify the context is no longer listed in the available contexts.
Example 9: View Configuration
Command: kubectl config view
Description: Displays the current kubeconfig file settings including clusters, users, and contexts.
Output (example):
apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://my-cluster-url name: my-cluster contexts: - context: cluster: my-cluster user: my-user name: my-context
Verification: Review the output to confirm all configurations are as expected.
Example 10: Edit Configuration
Command: kubectl config edit
Description: Opens the kubeconfig file in an editor to make manual adjustments.
Output (example): Opens the kubeconfig file for editing.
Verification: After editing, save the changes and verify using kubectl config view
.
Also check similar articles.
Exploring Kubernetes API Versions with kubectl api-versions
Understanding Kubernetes API Resources with kubectl api-resources
Generating Shell Completion Code with kubectl completion
Managing Kubernetes Annotations with kubectl annotate
Updating Kubernetes Labels with kubectl label
Discussion about this post