Search code examples
dockerkuberneteskubectlkops

How to curl container deployed on Kubernetes?


  1. I built my Docker image and uploaded it to Amazon ECS (image repository).
  2. I've written a deployment.yaml file and ran kubectl apply -f deployment.yaml.

Worth noting I've used kops to deploy the K8s cluster to AWS EC2

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

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.

Dockerfile of deployed image:

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

EXPOSE 3000
CMD ["node", "dist/index.js"]

deployment.yaml (kubectl apply -f deployment.yaml):

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

What I tried:

  1. Run kubectl get pods -o wide
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   <none>           <none>
vuekcal-6956589484-f4pqf   1/1     Running   0          16m   100.96.1.7   ip-172-20-59-29.eu-central-1.compute.internal   <none>           <none>

If get and IP address from the IP column and try to curl it, nothing happens:

curl not working for ports 80 and 3000 on an example copied ip

I assume this is because those IPs are local.

  1. Finding the K8s node that is running my K8s pod with my container and trying to curl that node's public ip address

external ip of aws ec2 node

And same thing: no response. enter image description here

Everything is fine if I run the container locally docker run k8s-search.

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


Solution

  • To access the pod from outside the cluster you need to create either Nodeport or LoadBalancer type service.

    kubectl expose deployment vuekcal --type=NodePort --name=example-service
    

    Then access it via curl http://<public-node-ip>:<node-port>


    !Make sure you ran the kubectl expose command above!

    Public node IP

    To get the public node IP, run the following command:

    kubectl get nodes -o wide
    

    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 133.71.33.7. Remember this IP.

    NodePort

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

    kubectl describe service example-service
    

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

    After you run the command, you'll see something like this: enter image description here

    This is the port you should use when connecting.

    Putting it together

    133.73.133.7:31110