Terminus
Set Context With kubectl

Set Context With kubectl

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 [.inline-code] kubectl[.inline-code]  command.

The short answer

To create a new context or modify an existing one, you can use the [.inline-code] kubectl config set-context[.inline-code] command as follows:

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

Where:

  • [.inline-code] name[.inline-code]  is the custom name assigned to the context.
  • [.inline-code] cluster_name[.inline-code]  is the name of the cluster associated with the context.
  • [.inline-code] user[.inline-code]  is the name of the user associated with the context.
  • [.inline-code] namespace[.inline-code]  is the name of the namespace associated with the context.

Note that the [.inline-code] --cluster[.inline-code] , [.inline-code] --namespace[.inline-code] , and [.inline-code] --user[.inline-code]  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

Upon execution, the above command will create a context named [.inline-code] prod_context[.inline-code]  that will use the namespace named [.inline-code] prod_namespace[.inline-code] , the cluster named [.inline-code] my_cluster[.inline-code] , and the user named [.inline-code] prod_user[.inline-code]  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-recall-with-ai] Easily retrieve this command using Warp’s AI Command Search [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code] kubernetes set context[.inline-code]  in the AI Command Search will prompt an [.inline-code] kubectl[.inline-code]  command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#modify-the-current-context] Modifying the current context [#modify-the-current-context]

To modify the values of the cluster, the namespace, or the user of the current context, you can use the [.inline-code] kubectl config set-context[.inline-code]  command with the [.inline-code] --current[.inline-code]  flag as follows:

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

For example:

 $ 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 [.inline-code] prod_namespace[.inline-code] , the cluster named [.inline-code] my_cluster[.inline-code]  and the user named [.inline-code] prod_user[.inline-code] .

[#set-context-for-kubeconfig] Setting context for a specific kubeconfig file [#set-context-for-kubeconfig]

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 [.inline-code] --kubeconfig[.inline-code] flag as follows:

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

Where:

  • [.inline-code] config_file_path[.inline-code]  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

Upon execution, the above command will create a context named [.inline-code] prod_context[.inline-code]  and set the specified value for the cluster, the namespace, and the user in the kubeconfig file located at [.inline-code] $HOME/.kube/configFile[.inline-code] .

To verify the changes made to your context, you can use the [.inline-code] kubectl config view[.inline-code]  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.

[#set-context-using-aliases] Setting a context using aliases [#set-context-using-aliases]

In Kubernetes, you can use shell’s built-in [.inline-code] alias[.inline-code]  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 [.inline-code] alias[.inline-code]  command as follows:

 $ alias  <name>=<value>

Where:

  • [.inline-code] name[.inline-code]  is the name of the alias.
  • [.inline-code] value[.inline-code]  is the custom command or function associated with the alias.

For example, you can use the following command to create an alias for the [.inline-code] kubectl config set-context[.inline-code]  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'

Where:

  • [.inline-code] f() { … ; }[.inline-code]  defines a bash function named [.inline-code] f[.inline-code] , and [.inline-code] f[.inline-code]  is invoked at the end to execute the function.
  • [.inline-code] $1[.inline-code]  is the first positional argument representing the context name.
  • [.inline-code] $2[.inline-code]  is the second position argument representing the namespace.

For example:

$ setContextForNamespace my_context my_namespace

Upon execution, the above command will execute the [.inline-code] setContextForNamespace[.inline-code]  alias and create a context named [.inline-code] my_context[.inline-code]  that uses the namespace named [.inline-code] my_namespace[.inline-code] . 

[#switch-between-contexts] Switching between contexts [#switch-between-contexts]

To switch from the current context to another one, you can use the [.inline-code] kubectl config use-context[.inline-code]  command as follows:

$ kubectl config use-context <name>

Where:

  • [.inline-code] name[.inline-code]  is the context name you want to switch to.

For example:

$ kubectl config use-context my_context

Upon execution, the above command will switch from the current context to the context named [.inline-code] my_context[.inline-code] .

Note that if you attempt to switch to a context that does not exist, the above command will output an error message.

[#rename-a-context] Renaming a context [#rename-a-context]

To modify the name of an existing context, you can use the [.inline-code] kubectl config rename-context[.inline-code] command as follows:

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

Where:

  • [.inline-code] old_name[.inline-code]  is the context name that you want to rename.
  • [.inline-code] new_name[.inline-code]  is the new name you want to assign to the context. 

For example:

$ kubectl config rename-context prod_context production_context

Upon execution, the above command will rename the context from [.inline-code] prod_context[.inline-code]  to [.inline-code] production_context[.inline-code] .

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.

[#delete-a-context] Deleting a context [#delete-a-context]

To delete a context, you can use the [.inline-code] kubectl config delete-context[.inline-code]  command as follows:

$ kubectl config delete-context <name>

Where:

  • [.inline-code] name[.inline-code]  is the context name that you want to delete. 

For example:

$ kubectl config delete-context stage_context

Upon execution, the above command will delete the context named [.inline-code] stage_context[.inline-code] . 

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.