<p>i have an application that takes a long time because it operates on multiple clusters.</p>
<p>for example:</p>
<pre><code>#!/bin/bash
for kube_context in a b c d e f; do
    kubectl config use-context ${kube_context}
    kubectl get pods
done
</code></pre>
<p>normally to speed this up i would just use multiple processes via something like:</p>
<pre><code>#!/bin/bash
pids=()
for kube_context in a b c d e f; do
    (
    kubectl config use-context ${kube_context}
    kubectl get pods
    ) &amp;
    pid=$!
    pids+=( $pid )
done
for pid in ${pids[@]}; do
    if wait ${pid}; then
        echo &quot;DEBUG: pid=$pid: success&quot;
    else
        exitcode=$?
        echo &quot;WARNING: pid=${pid}: failure (exitcode=${exitcode})&quot; &gt;&amp;2
    fi
done
</code></pre>
<p>but i can not use multiple processes because they are all using the same Kubernetes config file.</p>
<h1>how do you execute multiple <code>kubectl</code> commands for multiple clusters in parallel?</h1>
<p>p.s. Does <code>kubectl</code> have a <code>--context</code> similar to <code>--namespace</code>, yes/no? If no, what is the best current practice?</p>
<p>p.p.s. i think this question may be similar to what is seen here: <a href="https://github.com/ahmetb/kubectx/issues/12" rel="nofollow noreferrer">https://github.com/ahmetb/kubectx/issues/12</a></p>

Trevor Boyd Smith
<p><code>kubectl</code> does not support running multiple commands in parallel. You would have to do this on the shell level.</p>
<p>There is a <code>--context</code> flag which you can use. There is no need to modify the <code>KUBECONFIG</code> environment variable or to run <code>kubectl config use-context</code>.</p>
<p>I modified your script a bit, but haven't tested it:</p>
<pre class="lang-bash prettyprint-override"><code>#!/bin/bash
pids=()
for kube_context in a b c d e f; do
    (
    kubectl --context &quot;${kube_context}&quot; get pods
    ) &amp;
    pid=$!
    pids+=( $pid )
done
for pid in ${pids[@]}; do
    if wait ${pid}; then
        echo &quot;DEBUG: pid=$pid: success&quot;
    else
        exitcode=$?
        echo &quot;WARNING: pid=${pid}: failure (exitcode=${exitcode})&quot; &gt;&amp;2
    fi
done
</code></pre>
