<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Klystiq blog</title>
    <link>https://klystiq.com/blog/</link>
    <atom:link href="https://klystiq.com/blog.xml" rel="self" type="application/rss+xml"/>
    <description>Practical Kubernetes writing from the maker of Klystiq.</description>
    <language>en-us</language>
    <lastBuildDate>Sun, 22 Nov 2026 00:00:00 GMT</lastBuildDate>
    <item>
      <title>Stop running kubectl against the wrong cluster</title>
      <link>https://klystiq.com/blog/wrong-kubernetes-context/</link>
      <guid isPermaLink="true">https://klystiq.com/blog/wrong-kubernetes-context/</guid>
      <pubDate>Sun, 22 Nov 2026 00:00:00 GMT</pubDate>
      <description><![CDATA[<p>Why kubectl ends up pointed at production when you meant staging, and the habits that stop it: shell prompts, per-terminal kubeconfigs and read-only roles.</p>
<p>Everyone who works with more than one cluster has a version of this story. You meant to delete a Deployment in staging. kubectl was pointed at production. The command did exactly what you asked, quickly and without complaint, because that&#39;s its job.</p>
<p>It&#39;s rarely carelessness. It&#39;s how kubectl stores state.</p>
<h2 id="why-it-happens"><a class="anchor" href="#why-it-happens" aria-hidden="true" tabindex="-1">#</a>Why it happens</h2>
<p><code>kubectl config use-context</code> doesn&#39;t change anything about your terminal. It rewrites <code>current-context</code> in your kubeconfig file, usually <code>~/.kube/config</code>. Every terminal, script and tool that reads that file now points at the new cluster, including the terminal you switched an hour ago and forgot about.</p>
<p>So the context you see in one window isn&#39;t necessarily the context you&#39;re using in another. Add <code>kubectx</code> muscle memory, a few tabs and a pager going off, and you have the classic setup.</p>
<h2 id="1-make-the-context-impossible-to-miss"><a class="anchor" href="#1-make-the-context-impossible-to-miss" aria-hidden="true" tabindex="-1">#</a>1. Make the context impossible to miss</h2>
<p>Put the current context in your shell prompt. <a href="https://github.com/jonmosco/kube-ps1">kube-ps1</a> does it for bash and zsh, and <a href="https://starship.rs/config/#kubernetes">Starship</a> has a <code>kubernetes</code> module (off by default) that can color the prompt by context name:</p>
<pre><code class="language-toml">[kubernetes]
disabled = false
contexts = [
  { context_pattern = &quot;.*prod.*&quot;, style = &quot;bold red&quot; },
]
</code></pre>
<p>A red prompt won&#39;t stop a determined finger, but it does make &quot;wait, which cluster?&quot; a reflex.</p>
<h2 id="2-give-each-terminal-its-own-context"><a class="anchor" href="#2-give-each-terminal-its-own-context" aria-hidden="true" tabindex="-1">#</a>2. Give each terminal its own context</h2>
<p>The real fix for shared state is to stop sharing it. <code>KUBECONFIG</code> can point at a file per shell:</p>
<pre><code class="language-sh"># a private copy for this terminal; use-context here changes nothing elsewhere
cp ~/.kube/config ~/.kube/config.$$
export KUBECONFIG=~/.kube/config.$$
</code></pre>
<p>The copies contain your credentials, so clean them up (<code>rm ~/.kube/config.*</code> once those shells are closed), or skip the copying: <a href="https://github.com/sbstp/kubie">kubie</a> does this for you: <code>kubie ctx staging</code> opens a shell whose context is staging and stays staging, whatever happens in the next tab.</p>
<h2 id="3-name-contexts-so-production-looks-like-production"><a class="anchor" href="#3-name-contexts-so-production-looks-like-production" aria-hidden="true" tabindex="-1">#</a>3. Name contexts so production looks like production</h2>
<p>Cloud CLIs generate names like <code>arn:aws:eks:us-east-1:123456789012:cluster/main</code> and <code>gke_acme-prod_us-central1_main</code>. They&#39;re unique, and unreadable at 2 a.m. Rename them:</p>
<pre><code class="language-sh">kubectl config rename-context gke_acme-prod_us-central1_main prod-us-central
</code></pre>
<p>Then &quot;prod&quot; is in every prompt, every error and every <code>kubectl config get-contexts</code>.</p>
<h2 id="4-be-explicit-in-anything-youll-run-twice"><a class="anchor" href="#4-be-explicit-in-anything-youll-run-twice" aria-hidden="true" tabindex="-1">#</a>4. Be explicit in anything you&#39;ll run twice</h2>
<p>Scripts, runbooks and aliases should never rely on <code>current-context</code>:</p>
<pre><code class="language-sh">kubectl --context prod-us-central -n shop rollout restart deploy/checkout
</code></pre>
<p>It&#39;s longer. It&#39;s also the only version of that command that does the same thing every time you run it. An alias such as <code>alias kprod=&#39;kubectl --context prod-us-central&#39;</code> keeps it short.</p>
<h2 id="5-have-no-default-at-all"><a class="anchor" href="#5-have-no-default-at-all" aria-hidden="true" tabindex="-1">#</a>5. Have no default at all</h2>
<p>If you&#39;d rather kubectl refuse than guess, remove the default:</p>
<pre><code class="language-sh">kubectl config unset current-context
</code></pre>
<p>Now a bare <code>kubectl get pods</code> fails (with a confusing message about <code>localhost:8080</code>, but fails) until you pass <code>--context</code>. It&#39;s annoying for about a day, and then it&#39;s just how you type.</p>
<h2 id="6-use-read-only-credentials-for-production"><a class="anchor" href="#6-use-read-only-credentials-for-production" aria-hidden="true" tabindex="-1">#</a>6. Use read-only credentials for production</h2>
<p>Prompts and habits reduce mistakes. Permissions prevent them. If your day-to-day production access is a user or role bound to the built-in <code>view</code> ClusterRole, a wrong-context <code>delete</code> is a <code>Forbidden</code> error instead of an incident. Keep write access in a separate context you switch to on purpose:</p>
<pre><code class="language-sh">kubectl --context prod-us-central auth can-i delete deployments -n shop
# no
</code></pre>
<p>This is the one item on the list that also protects you from scripts, tools and coding agents, not just tired humans.</p>
<h2 id="7-look-before-you-write"><a class="anchor" href="#7-look-before-you-write" aria-hidden="true" tabindex="-1">#</a>7. Look before you write</h2>
<p><code>kubectl diff</code> shows what an apply would change, computed by the API server, before anything is written:</p>
<pre><code class="language-sh">kubectl --context staging diff -f checkout.yaml
</code></pre>
<p>If the diff is enormous, or names objects you don&#39;t recognize, you&#39;re probably not where you think you are.</p>
<h2 id="where-klystiq-fits"><a class="anchor" href="#where-klystiq-fits" aria-hidden="true" tabindex="-1">#</a>Where Klystiq fits</h2>
<p>I built Klystiq partly because of this problem, so it&#39;s opinionated about it. Every row in every list shows which context it came from, with a color per cluster. Writes go to the cluster the object came from, not to whatever is &quot;current&quot;. You can mark a context <a href="/docs/getting-started/#make-production-read-only">read-only</a>, and Klystiq then refuses every write to it in its backend, not just by hiding buttons. And every change gets a server-side dry run and a diff first. It doesn&#39;t touch your kubeconfig, so everything above still works alongside it.</p>
<p>But do the read-only credentials either way. They&#39;re the fix that works for every tool, including the ones you haven&#39;t installed yet.</p>
]]></description>
    </item>
  </channel>
</rss>
