<p>I successfully deployed my Kubernetes app using <code>kubectl apply -f deployment.yaml</code>.
When I try to hit the URL endpoint, I'm getting an <code>nginx 404 Not Found</code> error page.</p>
<p>My next step is to open a <code>bash</code> shell on the docker instance that is running my app.  How do I do this in Kubernetes?</p>
<p><strong>How do I ssh into the docker container running my app, or docker exec bash to an app I've deployed to Kubernetes?</strong></p>
<p>If I were running in docker I would run <code>docker ps</code> to find the container ID and then run <code>docker exec -t ### bash</code> to open a shell on my container to look around to troubleshoot why something isn't working.</p>
<p><strong>What is the equivalent way to do this in Kubernetes?</strong></p>
<h2>Searching for a solution</h2>
<p>I searched and found <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="nofollow noreferrer">this URL</a>, which says how to get a shell on your app.
The summary of that URL is:</p>
<pre><code>kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
kubectl get pod shell-demo
kubectl exec --stdin --tty shell-demo -- /bin/bash
</code></pre>
<p>But when I tried the equivalent commands, I got an error see below:</p>
<pre><code>kubectl get pods --namespace my-app-namespace
NAME                               READY   STATUS    RESTARTS   AGE
dpl-my-app-787bc5b7d-4ftkb         1/1     Running   0           2h
</code></pre>
<p>Then I tried:</p>
<pre><code>kubectl exec --stdin --tty my-app-namespace -- /bin/bash
Error from server (NotFound): pods &quot;my-app-namespace&quot; not found
exit status 1
</code></pre>
<p>I figured this happened because I was trying to exec into the namespace not the pod, so I also tried with the <code>dpl-my-app-...</code> (see below)  but got the same error.</p>
<pre><code>kubectl exec --stdin --tty dpl-my-app-787bc5b7d-4ftkb -- /bin/bash
Error from server (NotFound): pods &quot;dpl-my-app-787bc5b7d-4ftkb&quot; not found
exit status 1
</code></pre>
<p>What is the command I need to get the pod instance so that <code>kubectl exec</code> will work?</p>

PatS
<p>As correctly stated by @David Maze:</p>
<blockquote>
<p>Your <code>kubectl get pods</code> command has a <code>--namespace</code> option; you need to repeat this in the <code>kubectl exec</code> command. – David Maze 12 hours ago</p>
</blockquote>
<p>If you've created your <code>Deployment</code>: <code>dpl-my-app</code> in a namespace: <code>my-app-namespace</code> you should also specify the <code>--namespace</code>/<code>-n</code> parameter in all of your commands.</p>
<blockquote>
<p>A side note!</p>
<p>There is a tool to change namespaces, called: <a href="https://github.com/ahmetb/kubectx/blob/master/kubens" rel="nofollow noreferrer">kubens</a></p>
</blockquote>
<hr />
<p>With a following command:</p>
<ul>
<li><code>kubectl exec --stdin --tty my-app-namespace -- /bin/bash</code></li>
</ul>
<p>You've correctly identified the issue that you are trying to exec into a <code>namespace</code> but not into a <code>Pod</code></p>
<p>With a following command:</p>
<ul>
<li><code>kubectl exec --stdin --tty dpl-my-app-787bc5b7d-4ftkb -- /bin/bash</code></li>
</ul>
<p>You've tried to exec into a <strong><code>Pod</code></strong> named <code>dpl-my-app-787bc5b7d-4ftkb</code> but in a <code>default</code> namespace. Not in a <code>namespace</code> your <code>Pod</code> is residing.</p>
<p>To exec into your <code>Pod</code> in a specific <code>namespace</code> you should use following command:</p>
<ul>
<li><code>kubectl exec --stdin --tty --namespace my-app-namespace dpl-my-app-787bc5b7d-4ftkb -- /bin/bash</code></li>
</ul>
<p>Please notice the <code>--namespace</code> is before <code>--</code> where the commands to the <code>Pod</code> should be placed (like <code>-- /bin/bash</code>).</p>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/" rel="nofollow noreferrer">Kubernetes.io: Docs: Concepts: Overview: Working with objects: Namespaces</a></em></li>
<li><em><a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="nofollow noreferrer">Kubernetes.io: Docs: Tasks: Debug application cluster: Get shell running container</a></em></li>
</ul>
