<ol>
<li>I built my Docker image and uploaded it to Amazon ECS (image repository). </li>
<li>I've written a <code>deployment.yaml</code> file and ran <code>kubectl apply -f deployment.yaml</code>. </li>
</ol>

<p>Worth noting I've used kops to deploy the K8s cluster to AWS EC2</p>

<p>I can see the containers are running in Kubernetes pods using the Kubernetes Dashboard. Also <code>kubectl get pods -o wide</code> also shows me the pods.</p>

<p>The image I deployed is a simple API that exposes one route. My problem is that I have no idea how to query the container I just deployed.</p>

<p>Dockerfile of deployed image:</p>

<pre><code>FROM node:lts
COPY package*.json tsconfig.json ./
RUN npm ci
COPY . .
RUN npm run build

EXPOSE 3000
CMD ["node", "dist/index.js"]
</code></pre>

<p>deployment.yaml (<code>kubectl apply -f deployment.yaml</code>):  </p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: vuekcal
spec:
  selector:
    matchLabels:
      app: vuekcal
  replicas: 2
  template:
    metadata:
      labels:
        app: vuekcal
    spec:
      containers:
        - name: search
          image: [my-repo-id].dkr.ecr.eu-central-1.amazonaws.com/k8s-search
          ports:
            - containerPort: 3000
</code></pre>

<p>What I tried:</p>

<ol>
<li>Run <code>kubectl get pods -o wide</code></li>
</ol>

<pre><code>NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE                                            NOMINATED NODE   READINESS GATES
vuekcal-6956589484-7f2kx   1/1     Running   0          16m   100.96.2.6   ip-172-20-54-21.eu-central-1.compute.internal   &lt;none&gt;           &lt;none&gt;
vuekcal-6956589484-f4pqf   1/1     Running   0          16m   100.96.1.7   ip-172-20-59-29.eu-central-1.compute.internal   &lt;none&gt;           &lt;none&gt;
</code></pre>

<p>If get and IP address from the <code>IP</code> column and try to curl it, nothing happens:</p>

<p><a href="https://i.stack.imgur.com/Q21S1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q21S1.png" alt="curl not working for ports 80 and 3000 on an example copied ip"></a></p>

<p>I assume this is because those IPs are local.</p>

<ol start="2">
<li>Finding the K8s node that is running my K8s pod with my container and trying to curl that node's public ip address</li>
</ol>

<p><a href="https://i.stack.imgur.com/tO2Pj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tO2Pj.png" alt="external ip of aws ec2 node"></a></p>

<p>And same thing: no response.
<a href="https://i.stack.imgur.com/ycqlC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ycqlC.png" alt="enter image description here"></a></p>

<p>Everything is fine if I run the container locally <code>docker run k8s-search</code>.</p>

<p>I have no idea what to do here. How do I query the image that deployment.yaml sets up inside a Kubernetes node?</p>

<p>To access the pod from outside the cluster you need to create either <a href="https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/" rel="nofollow noreferrer">Nodeport</a> or <a href="https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/" rel="nofollow noreferrer">LoadBalancer</a> type service.</p>

<pre><code>kubectl expose deployment vuekcal --type=NodePort --name=example-service
</code></pre>

<p>Then access it via <code>curl http://&lt;public-node-ip&gt;:&lt;node-port&gt;</code></p>

<hr>

<p><strong>!Make sure you ran the <code>kubectl expose</code> command above!</strong></p>

<h3>Public node IP</h3>

<p>To get the public node IP, run the following command:</p>

<pre><code>kubectl get nodes -o wide
</code></pre>

<p>and look at the "EXTERNAL-IP" column. This is the public ip of the node that is running your container. This is where you should try to connect. For example, the extrenal IP of your node could be <code>133.71.33.7</code>. Remember this IP.</p>

<h2>NodePort</h2>

<p>It's different than the containerPort in your deployment.yaml.<br>
To find the NodePort, run this command:</p>

<pre><code>kubectl describe service example-service
</code></pre>

<p>Replace <code>example-service</code> with whatever you wrote in <code>--name=</code> when running <code>kubectl expose deployment ...</code> (first command in this post)</p>

<p>After you run the command, you'll see something like this:
<a href="https://i.stack.imgur.com/Jblnf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jblnf.png" alt="enter image description here"></a></p>

<p>This is the port you should use when connecting.</p>

<h3>Putting it together</h3>

<p>133.73.133.7:31110</p>
