<p>I wanted to execute <code>kubectl exec</code> command and connect to a container, so that I can execute some commands on it.
I have set the proxy with the following command:</p>

<pre><code>kubectl proxy -p=8080 --kubeconfig=/directory_path/remote-kubeconfig &amp;
</code></pre>

<p>proxy started running. I tried executing <code>kubectl exec</code> command:</p>

<pre><code>kubectl exec -it &lt;pod&gt; --namespace=&lt;namespace&gt; -c &lt;container&gt; -- ls -l
</code></pre>

<p>I got the following error:</p>

<pre><code>error: unable to upgrade connection: &lt;h3&gt;Unauthorized&lt;/h3&gt;
</code></pre>

<p>Did I missed anything while executing <code>kubectl exec</code> command ?? Please help.</p>

<p>By default, <code>kubectl proxy</code> rejects API commands matching <code>^/api/./pods/./exec</code>.</p>

<p>All you need to do in order to "bypass" this is start <code>kubectl proxy</code> with the appropriate flags:</p>

<p><code>kubectl proxy --port 8080 --reject-paths "^/api/./pods/./attach"</code></p>

<p>Of course, this comes with the security risk that anyone with access to your proxy can now exec into any of the pods on the cluster.</p>

<p>If you actually need to do this in a production environment, I highly recommend restricting what the proxy has access to using RBAC authorization - run the proxy as a Deployment with a ServiceAccount.</p>

<p>For more information, see:</p>

<p><a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#proxy" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#proxy</a>
<a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/access-authn-authz/rbac/</a></p>
