Set Context With kubectl

Mansi Manhas
Mansi Manhas
Published: January 31, 2024

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:

Bash
$ kubectl config set-context  <name> [--cluster=cluster_name] [--user=user] [--namespace=namespace]

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:

Bash
$ kubectl config set-context prod_context --namespace=prod_namespace --cluster=my_cluster --user=prod_user

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.

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:

Bash
$ kubectl config set-context --current [--cluster=cluster_name] [--user=user] [--namespace=namespace]

For example:

Bash
$ kubectl config set-context --current --namespace=prod_namespace --cluster=my_cluster --user=prod_user

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:

Bash
$ kubectl config set-context --kubeconfig= <config_file_path>  <name> [--cluster=cluster_name] [--user=user] [--namespace=namespace]

Where:

  • config\_file\_path  is the absolute or relative path to the kubeconfig file.

For example:

Bash
$ kubectl config set-context --kubeconfig=$HOME/.kube/configFile prod-context --namespace=prod_namespace --cluster=my_cluster --user=prod_user

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:

Bash
$ alias  <name>=<value>

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:

Bash
$ alias setContextForNamespace='f() { [ "$1" ] && [ "$2" ] && kubectl config set-context $1 --namespace $2; } ; f'

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:

Bash
$ setContextForNamespace my_context my_namespace

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:

Bash
$ kubectl config use-context <name>

Where:

  • name  is the context name you want to switch to.

For example:

Bash
$ kubectl config use-context my_context

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:

Bash
$ kubectl config rename-context <old_name> <new_name>

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:

Bash
$ kubectl config rename-context prod_context production_context

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:

Bash
$ kubectl config delete-context <name>

Where:

  • name  is the context name that you want to delete.

For example:

Bash
$ kubectl config delete-context stage_context

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.

Related articles


Execute Commands in Pods With kubectl

Learn how to execute standalone commands and start interactive shell sessions in Pods using the `kubectl exec` 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.

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.

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.

Forward Ports In Kubernetes

Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward 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 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.

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.

Delete Kubernetes Deployments With kubectl

Learn how to delete Kubernetes deployments and its associated Pods using the kubectl command.

Deleting a Pod with `kubectl`

Learn how to delete a pod in Kubernetes using graceful shutdowns, labels, field selectors, and namespaces.