Configuring Context Settings
Modifying Existing Contexts
In addition to switching between contexts, you can also modify the settings of an existing context using the kubectl config set-context
command. This allows you to change the cluster, user, or namespace associated with a particular context.
For example, to change the namespace of the "development" context, you can run the following command:
$ kubectl config set-context development --namespace=dev-namespace
Context "development" modified.
Now, when you switch to the "development" context, it will use the "dev-namespace" namespace instead of the default one.
Creating New Contexts
If you need to work with a Kubernetes cluster that is not yet configured in your ~/.kube/config
file, you can create a new context using the kubectl config set-context
command.
For example, to create a new context for a Kubernetes cluster with the following details:
- Cluster:
new-cluster
- User:
new-user
- Namespace:
new-namespace
You can run the following command:
$ kubectl config set-context new-context --cluster=new-cluster --user=new-user --namespace=new-namespace
Context "new-context" created.
After creating the new context, you can switch to it using the kubectl config use-context
command:
$ kubectl config use-context new-context
Switched to context "new-context".
Configuring Context Defaults
You can also set default values for the cluster, user, and namespace in your Kubernetes configuration file. This can be useful if you frequently work with a specific set of contexts.
To set the default cluster, user, and namespace, you can use the kubectl config set-cluster
, kubectl config set-credentials
, and kubectl config set-context
commands, respectively.
For example, to set the default cluster to "new-cluster", the default user to "new-user", and the default namespace to "new-namespace", you can run the following commands:
$ kubectl config set-cluster new-cluster --server=https://new-cluster.example.com
$ kubectl config set-credentials new-user --client-certificate=client.crt --client-key=client.key
$ kubectl config set-context default-context --cluster=new-cluster --user=new-user --namespace=new-namespace
Now, when you create a new context, it will automatically use these default values unless you specify otherwise.