{"id":568,"date":"2025-05-30T05:33:00","date_gmt":"2025-05-30T05:33:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=568"},"modified":"2025-05-30T05:33:00","modified_gmt":"2025-05-30T05:33:00","slug":"kubectl-set-context","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=568","title":{"rendered":"kubectl set context Tutorial (Practical Examples)"},"content":{"rendered":"<p>In this guide, we will look at how to use kubectl set context command for managing multiple Kubernetes cluster contexts.<\/p>\n<p><strong>By the end of this guide, you will learn:<\/strong><\/p>\n<ul>\n<li>What <code>kubectl set-context<\/code> is<\/li>\n<li>How to create a context using <code>kubectl<\/code> with a real-world example<\/li>\n<li>How to manage contexts (create, switch, delete, rename, etc.)<\/li>\n<li>Setting up alias to mange contexts efficiently.<\/li>\n<\/ul>\n<p><code>kubectl config set-context<\/code> is a fundamental command for anyone working with Kubernetes. Before we look in to kubectl context command, lets first understand what is a context.<\/p>\n<h2 id=\"what-is-kubectl-context\">What is Kubectl Context?<\/h2>\n<p>When you work on Kubernetes projects, you will have to work with multiple <a href=\"https:\/\/devopscube.com\/setup-kubernetes-cluster-kubeadm\/\">kubernetes clusters<\/a> at a time in your workstation. <\/p>\n<p>It could be clusters that are part of the same environments or different environments. <\/p>\n<p>So what is a context?<\/p>\n<p>A context is a set of parameters (cluster, namespace, user) present in the <a href=\"https:\/\/devopscube.com\/kubernetes-kubeconfig-file\/\">kubeconfig file<\/a> that are required to connect to a kubernetes cluster. A single kubeconfig file can contain more than one context.<\/p>\n<p>Here is an example context block from <code>kubeconfig<\/code> file with three cluster contexts.<\/p>\n<pre><code class=\"language-yaml\">contexts:\n- context:\n    cluster: dev-cluster\n    user: dev-user\n  name: dev\n\n- context:\n    cluster: stage-cluster\n    user: stage-user\n  name: stage\n\n- context:\n    cluster: prod-cluster\n    user: prod-user\n  name: prod\n\ncurrent-context: dev<\/code><\/pre>\n<p>By defining or using contexts, you can easily switch between different clusters (e.g., development, staging, production) or different users with varying permissions on the same cluster, or even different default namespaces for your operations.<\/p>\n<p>Here is where <strong><code>kubectl config set context<\/code><\/strong> command comes in handy.<\/p>\n<p>Now that we have understood what a context is, let&#8217;s look at different practical examples to understand kubectl set context in detail.<\/p>\n<h2 id=\"what-is-kubectl-config-set-context\">What is kubectl config set context ?<\/h2>\n<p><strong><code>Kubectl set context<\/code><\/strong> command is used to modify an existing context in your kubeconfig file or create a new one if it doesn&#8217;t exist.<\/p>\n<p>The basic syntax is:<\/p>\n<pre><code class=\"language-bash\">kubectl config set-context [CONTEXT_NAME] [flags]<\/code><\/pre>\n<p>Common flags include:<\/p>\n<ul>\n<li><code>--cluster<\/code>: Specify which cluster the context refers to<\/li>\n<li><code>--user<\/code>: Specify which user credentials to use<\/li>\n<li><code>--namespace<\/code>: Set the default namespace for this context<\/li>\n<\/ul>\n<p>You can learn all the supported flags using help command.<\/p>\n<pre><code class=\"language-bash\">kubectl config set-context --help\n<\/code><\/pre>\n<h2 id=\"creating-a-context-using-kubectl-set-context-command\">Creating a Context Using kubectl set-context command<\/h2>\n<p>We will look at the kubectl set-context command usage with a real-world devops example workflow.<\/p>\n<p>Lets say you are working in a DevOps team and the cluster administrators gave you  the following.<\/p>\n<ol>\n<li>Cluster endpoint URL (e.g., <code>https:\/\/k8s-prod-cluster.company.com:6443<\/code>)<\/li>\n<li>CA certificate (typically a file like <code>cluster-ca.crt<\/code>)<\/li>\n<\/ol>\n<h3 id=\"1-create-the-cluster-entry\">1. Create the Cluster Entry<\/h3>\n<p>First, we&#8217;ll configure the cluster entry using the provided endpoint and CA certificate.<\/p>\n<pre><code class=\"language-bash\">$ kubectl config set-cluster prod-cluster \\\n  --server=https:\/\/k8s-prod-cluster.company.com:6443 \\\n  --certificate-authority=\/path\/to\/cluster-ca.crt\n\nCluster \"prod-cluster\" set.<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">If you received the CA certificate as a file, make sure to specify the correct path. If you received it as a base64-encoded string, you might need to decode it first and save it to a file.<\/div>\n<\/div>\n<h3 id=\"2-set-up-user-credentials\">2. Set Up User Credentials<\/h3>\n<p>Next, you&#8217;ll need authentication credentials. The administrators might provide these in several ways:<\/p>\n<p>If you received a client certificate and key:<\/p>\n<pre><code class=\"language-bash\">kubectl config set-credentials prod-devops-user \\\n  --client-certificate=\/path\/to\/user.crt \\\n  --client-key=\/path\/to\/user.key<\/code><\/pre>\n<p>If you need to use a token:<\/p>\n<pre><code class=\"language-bash\">kubectl config set-credentials prod-devops-user \\\n  --token=&lt;TOKEN-HERE&gt;<\/code><\/pre>\n<p>If using <a href=\"https:\/\/devopscube.com\/github-actions-oidc-aws\/\" rel=\"noreferrer\">OIDC authentication<\/a> (common in enterprise environments):<\/p>\n<pre><code class=\"language-bash\">kubectl config set-credentials prod-devops-user \\\n  --auth-provider=oidc \\\n  --auth-provider-arg=idp-issuer-url=https:\/\/oidc.company.com \\\n  --auth-provider-arg=client-id=kubernetes \\\n  --auth-provider-arg=refresh-token=refresh-token-value<\/code><\/pre>\n<h3 id=\"3-create-the-context\">3. Create the Context<\/h3>\n<p>Now, create a context that links the cluster with your user credentials:<\/p>\n<pre><code class=\"language-bash\">kubectl config set-context prod-devops \\\n  --cluster=prod-cluster \\\n  --user=prod-devops-user \\\n  --namespace=devops\n\nContext \"prod-devops\" created.<\/code><\/pre>\n<h3 id=\"4-set-this-as-your-current-context\">4. Set this as your current context<\/h3>\n<p>You can set the newly created context as you current active context using the following command.<\/p>\n<pre><code class=\"language-bash\">$ kubectl config use-context prod-devops\n\nSwitched to context \"prod-devops\".<\/code><\/pre>\n<h3 id=\"5-verify-cluster-connection\">5. Verify Cluster Connection<\/h3>\n<p>Verify the cluster connection using,<\/p>\n<pre><code class=\"language-bash\">kubectl get nodes\nkubectl get pods <\/code><\/pre>\n<h2 id=\"creating-a-context-for-a-specific-namespace\">Creating a context for a specific namespace<\/h2>\n<p>By default, all the commands get executed in the default namespace if you don&#8217;t specify a namespace with the <strong><code>-n<\/code> <\/strong>flag. You can change that behavior by setting the current namespace using the <strong><code>set-context<\/code><\/strong> command.<\/p>\n<p>For example, if you want to set <strong><code>kube-system <\/code><\/strong>as your default namespace, you can use the following command systax.<\/p>\n<pre><code class=\"language-bash\">kubectl config set-context --current --namespace [namespace-name]<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code class=\"language-bash\">kubectl config set-context --current --namespace kube-system<\/code><\/pre>\n<p>Once you set the current namespace, you don&#8217;t have to use the <strong><code>-n<\/code><\/strong> flag with the kubectl command. All the kubectl command gets executed in the current namespace set by the set-context command.<\/p>\n<h2 id=\"managing-your-contexts\">Managing Your Contexts<\/h2>\n<p>Once you&#8217;ve set up your contexts, you&#8217;ll use these related commands frequentl<\/p>\n<h3 id=\"1-listing-cluster-contexts\">1. Listing Cluster Contexts<\/h3>\n<p>You can view all the contexts and its configuration using the following <code>kubectl<\/code> command<\/p>\n<pre><code class=\"language-bash\">kubectl config get-contexts<\/code><\/pre>\n<p>To list all the available contexts names, you can use the following kubectl command. The <strong><code>-o=name<\/code><\/strong> flag lists only the context names.<\/p>\n<pre><code class=\"language-bash\">kubectl config get-contexts -o=name<\/code><\/pre>\n<p>You will get the list of contexts as shown below.<\/p>\n<pre><code class=\"language-bash\">arn:aws:eks:us-west-2:814200988517:cluster\/custom-cluster\ndo-sfo3-k8s-1-28-2-do-0-sfo3-1699501871578\nkubernetes-admin@kubernetes\nkubernetes-the-hard-way<\/code><\/pre>\n<h3 id=\"2-get-the-current-context\">2. Get the Current Context<\/h3>\n<p>You can get the current context using the following comand. It will list the name of the currently active context.<\/p>\n<pre><code class=\"language-bash\">kubectl config current-context<\/code><\/pre>\n<h3 id=\"3-switch-context\">3. Switch Context<\/h3>\n<p><strong><code>kubectl config use context<\/code><\/strong> command is primarily used to switch to a different cluster context.<\/p>\n<p>Here is the syntax to set the current context.<\/p>\n<pre><code class=\"language-bash\">kubectl config use-context &lt;CONTEXT_NAME&gt;<\/code><\/pre>\n<p>For example, here is the list of contexts available in my Kubeconfig file.<\/p>\n<pre><code class=\"language-bash\">$ kubectl config get-contexts -o=name\n\narn:aws:eks:us-west-2:814200988517:cluster\/custom-cluster\ndo-sfo3-k8s-1-28-2-do-0-sfo3-1699501871578\nkubernetes-admin@kubernetes\nkubernetes-the-hard-way<\/code><\/pre>\n<p>Here is an example to switch the content.<\/p>\n<pre><code class=\"language-bash\">kubectl config use-context kubernetes-admin@kubernetes<\/code><\/pre>\n<p>When you use a new context, the value is also set in the Kubeconfig file. You can find a parameter called <strong>current-context:<\/strong> in the Kubeconfig file.<\/p>\n<h3 id=\"3-renaming-kubectl-contexts\">3. Renaming Kubectl contexts<\/h3>\n<p>You can also rename a context using the <code>rename-context<\/code> command.<\/p>\n<p>For example, lets say you have a context named <code>dev-cluster<\/code> and want it to be renamed to <code>dev01<\/code>. Here is how the command looks.<\/p>\n<pre><code class=\"language-bash\">$ kubectl config rename-context dev-cluster dev01        \n\nContext \"kind-dev-cluster\" renamed to \"kind-dev01\".<\/code><\/pre>\n<h3 id=\"4-delete-a-context\">4. Delete a context<\/h3>\n<p>You can delete the context created by kubectl using the delete-context command.<\/p>\n<p>Here is the command syntax.<\/p>\n<pre><code class=\"language-bash\">kubectl config delete-context [context-name]<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code class=\"language-bash\">kubectl config delete-context stage-k8s-cluster<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-red\">\n<div class=\"kg-callout-emoji\">\u26a0\ufe0f<\/div>\n<div class=\"kg-callout-text\">Delete context only deletes the context section, not the related cluster or user entries in the <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">~\/.kube\/config<\/code> file.<\/div>\n<\/div>\n<h2 id=\"setting-kubectl-alias-for-contexts\">Setting Kubectl Alias for Contexts<\/h2>\n<p>If you regularly switch between contexts to work on different clusters, you can create an alias to list and set the context.<\/p>\n<p>Here is the alias to list the contexts.<\/p>\n<pre><code class=\"language-bash\">alias klist='kubectl config get-contexts -o=name'<\/code><\/pre>\n<p>You can add the following line to your <code>.bashrc<\/code> or <code>.zshrc<\/code> file, depending on the shell you use:<\/p>\n<p>Create the following alias to set the context with a custom namespace.<\/p>\n<pre><code class=\"language-bash\">kset() {\n    local context=\"$1\"\n    local namespace=\"$2\"\n    kubectl config use-context \"$context\" --namespace \"$namespace\"\n}<\/code><\/pre>\n<p>Here is how the aliases look in your <strong><code>bashrc<\/code><\/strong> or <strong><code>zshrc<\/code><\/strong> file.<\/p>\n<pre><code class=\"language-bash\">alias k=kubectl\n\nalias klist='kubectl config get-contexts -o=name'\n\nkset() {\n    local context=\"$1\"\n    local namespace=\"$2\"\n    kubectl config use-context \"$context\" --namespace \"$namespace\"\n}\n\nkdel() {\n    kubectl config delete-context \"$1\"\n}<\/code><\/pre>\n<p>Here is how you can use these aliases<\/p>\n<ol>\n<li>List the contexts with <strong>klist<\/strong> command.<\/li>\n<li>Set the context and namespace using the <strong>kset<\/strong> command<\/li>\n<\/ol>\n<p>Here is the example.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-186-3.png\" class=\"kg-image\" alt=\"Kubectl Set Context Aliases\" loading=\"lazy\" width=\"1200\" height=\"752\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-186-3.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-186-3.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-186-3.png 1200w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"set-context-faqs\">Set Context FAQ&#8217;s<\/h2>\n<h3 id=\"how-to-set-current-context-in-kubectl\">How to set current context in kubectl?<\/h3>\n<p>You can use the <strong>kubectl config use-context<\/strong> command with the context name to set the current context.<\/p>\n<p>For example, <strong><code>kubectl config use-context dev-cluster<\/code><\/strong><\/p>\n<p>Where <strong>dev-cluster<\/strong> is the context name.<\/p>\n<h3 id=\"how-do-i-set-context-in-kubernetes-for-specific-namespace\">How do I set context in Kubernetes for specific namespace?<\/h3>\n<p>You can use the <strong><code>--namespace<\/code><\/strong> flag with the kubectl config use-context command to set a current namespace in context.<\/p>\n<h3 id=\"what-is-the-difference-between-kubectl-set-and-kubectl-use-context\">What is the difference between kubectl set and kubectl use context?<\/h3>\n<p>The set-context parameters modify the cluster context or create a new one if not present. The use context parameter is used to switch to a different context.<\/p>\n<h2 id=\"kubectl-alternatives-to-set-contexts\">kubectl Alternatives to Set Contexts<\/h2>\n<p>If you dont want to use the native commands to set kubectl contexs, you can make use the following alternative open source utilites.<\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/sbstp\/kubie?ref=devopscube.com\" rel=\"noreferrer noopener\">Kubie<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/derailed\/k9s?ref=devopscube.com\">K9s<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ahmetb\/kubectx?ref=devopscube.com\" rel=\"noreferrer noopener\">Kubectx<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/turkenh\/KubeContext?ref=devopscube.com\" rel=\"noreferrer noopener\">KubeContext<\/a> (GUI)<\/li>\n<\/ol>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/kubectl-set-context\/\" target=\"_blank\" rel=\"noopener noreferrer\">kubectl set context Tutorial (Practical Examples) \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/kubectl-set-context\/<\/p>\n","protected":false},"author":1,"featured_media":569,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=568"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/568\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/569"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}