<p>by using <code>kubectl exec -ti POD_NAME bash</code> I am able to access the terminal inside the container and execute the command.</p>

<p>I can understand the usability and convenient of the above command. As K8s Operator I use <strong>exec</strong> regularly. </p>

<p>However, What is the use case of <code>kubectl attach POD_NAME</code>? </p>

<p>How can it be <strong>utilised</strong>?
What is the real <strong>purpose</strong> of it?
In what situation or circumstance it can be used?         </p>

<p>The use cases for kubectl attach are discussed in <a href="https://github.com/kubernetes/kubernetes/issues/23335" rel="noreferrer">kubernetes/issue 23335</a>.</p>

<p>It can attach to the main process run by the container, which is not always bash.<br>
As opposed to exec, which allows you to execute <em>any</em> process within the container (often: bash)</p>

<pre><code># Get output from running pod 123456-7890, using the first container by default
kubectl attach 123456-7890

# Get output from ruby-container from pod 123456-7890
kubectl attach 123456-7890 -c ruby-container
</code></pre>

<p><a href="https://kubernetes.io/blog/2015/10/some-things-you-didnt-know-about-kubectl_28#attach-to-existing-containers" rel="noreferrer">This article</a> proposes:</p>

<blockquote>
  <p>In addition to interactive execution of commands, you can now also attach to any running process. Like <code>kubectl logs</code>, you’ll get stderr and stdout data, but <strong>with attach, you’ll also be able to send stdin from your terminal to the program.<br>
  Awesome for interactive debugging, or even just sending <kbd>ctrl</kbd>-<kbd>c</kbd> to a misbehaving application.</strong></p>
</blockquote>

<pre><code>  $&gt; kubectl attach redis -i
</code></pre>

<hr>

<p>Again, the main difference is in the process you interact with in the container:</p>

<ul>
<li>exec: any one you want to create</li>
<li>attach: the one currently running (no choice)</li>
</ul>
