In Kubernetes, every operation is executed in a context that groups access settings for clusters, namespaces, and users. Using contexts is useful when managing multiple clusters, namespaces, or users, as it eliminates the need to specify their values for every executed kubectl command.
The short answer
To create a new context or modify an existing one, you can use the kubectl config set-context command as follows:
$ kubectl config set-context <name> [--cluster=cluster_name] [--user=user] [--namespace=namespace]
Run in Warp
Where:
- name is the custom name assigned to the context.
- cluster_name is the name of the cluster associated with the context.
- user is the name of the user associated with the context.
- namespace is the name of the namespace associated with the context.
Note that the --cluster , --namespace , and --user flags are optional. If you omit any of these, they will be inherited from the current context.
For example:
$ kubectl config set-context prod_context --namespace=prod_namespace --cluster=my_cluster --user=prod_user
Run in Warp
Upon execution, the above command will create a context named prod_context that will use the namespace named prod_namespace , the cluster named my_cluster , and the user named prod_user for all the subsequent commands in this context.
If you specify a context name that already exists, the above command will modify the existing values of cluster, namespace, and user with the above-specified ones, leaving other contexts untouched.
Easily retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering kubernetes set context in the AI Command Search will prompt an kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER .
Modifying the current context
To modify the values of the cluster, the namespace, or the user of the current context, you can use the kubectl config set-context command with the --current flag as follows:
$ kubectl config set-context --current [--cluster=cluster_name] [--user=user] [--namespace=namespace]
Run in Warp
For example:
$ kubectl config set-context --current --namespace=prod_namespace --cluster=my_cluster --user=prod_user
Run in Warp
Upon execution, the above command will modify the current context, and replace its existing values with the namespace named prod_namespace , the cluster named my_cluster and the user named prod_user .
Setting context for a specific kubeconfig file
The configuration of all the contexts are stored in kubeconfig files. To manage multiple clusters and configurations, you can use several kubeconfig files, for example, for the development, staging, and production environments.
To set a context for a specific kubeconfig file, you can use the --kubeconfig flag as follows:
$ kubectl config set-context --kubeconfig= <config_file_path> <name> [--cluster=cluster_name] [--user=user] [--namespace=namespace]
Run in Warp
Where:
- config_file_path is the absolute or relative path to the kubeconfig file.
For example:
$ kubectl config set-context --kubeconfig=$HOME/.kube/configFile prod-context --namespace=prod_namespace --cluster=my_cluster --user=prod_user
Run in Warp
Upon execution, the above command will create a context named prod_context and set the specified value for the cluster, the namespace, and the user in the kubeconfig file located at $HOME/.kube/configFile .
To verify the changes made to your context, you can use the kubectl config view command. This command will output the content of your kubeconfig file, allowing you to view the configuration of all your contexts. You can refer to the official documentation to learn more about kubeconfig files.
Setting a context using aliases
In Kubernetes, you can use shell’s built-in alias command to create custom shortcuts for tasks that involve repetitive or lengthy commands, such as setting a context.
To create a new alias, you can use the alias command as follows:
$ alias <name>=<value>
Run in Warp
Where:
- name is the name of the alias.
- value is the custom command or function associated with the alias.
For example**,** you can use the following command to create an alias for the kubectl config set-context command, to facilitate the creation of a namespace for a given context:
$ alias setContextForNamespace='f() { [ "$1" ] && [ "$2" ] && kubectl config set-context $1 --namespace $2; } ; f'
Run in Warp
Where:
- f() { … ; } defines a bash function named f , and f is invoked at the end to execute the function.
- $1 is the first positional argument representing the context name.
- $2 is the second position argument representing the namespace.
For example:
$ setContextForNamespace my_context my_namespace
Run in Warp
Upon execution, the above command will execute the setContextForNamespace alias and create a context named my_context that uses the namespace named my_namespace .
Switching between contexts
To switch from the current context to another one, you can use the kubectl config use-context command as follows:
$ kubectl config use-context <name>
Run in Warp
Where:
- name is the context name you want to switch to.
For example:
$ kubectl config use-context my_context
Run in Warp
Upon execution, the above command will switch from the current context to the context named my_context .
Note that if you attempt to switch to a context that does not exist, the above command will output an error message.
Renaming a context
To modify the name of an existing context, you can use the kubectl config rename-context command as follows:
$ kubectl config rename-context <old_name> <new_name>
Run in Warp
Where:
- old_name is the context name that you want to rename.
- new_name is the new name you want to assign to the context.
For example:
$ kubectl config rename-context prod_context production_context
Run in Warp
Upon execution, the above command will rename the context from prod_context to production_context .
Note that, if you rename a context that you are currently using, the change will not be immediately reflected in your active session. Therefore, switch to the context with the new name or restart the terminal session.
Deleting a context
To delete a context, you can use the kubectl config delete-context command as follows:
$ kubectl config delete-context <name>
Run in Warp
Where:
- name is the context name that you want to delete.
For example:
$ kubectl config delete-context stage_context
Run in Warp
Upon execution, the above command will delete the context named stage_context .
Note that if you delete a context you are currently using, the above command will output a warning, asking you to switch to a different context to execute further operations. Along with this warning, the command will also output a success message confirming the deletion of the specified context.
Written by
Mansi Manhas
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
List Kubernetes Namespaces With kubectl
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
List Pods With kubectl
Learn how to list and filter Kubernetes Pods by name, namespaces, labels, manifests, and more using the kubectl command.