'Unable to connect to kubernetes nodeport service

I have a react app that I've dockerized and am trying to deploy on kubernetes through minikube with docker driver. The app connects to a backend, but I've removed that for simplicity in this question. I've also removed everything from the app except 'hello' inside App.js.

App Dockerfile:

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install
# RUN apk add --no-cache python2 g++ make
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Output from docker build:

olivermctammany@olivers-mbp map-tile-test % docker build -t olivermctammany/demomap client
[+] Building 25.6s (16/16) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                       0.0s
 => => transferring dockerfile: 220B                                                                                                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                            0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                                                                                                                                                                                 1.0s
 => [auth] docker/dockerfile:pull token for registry-1.docker.io                                                                                                                                                                                                           0.0s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:42399d4635eddd7a9b8a24be879d2f9a930d0ed040a61324cfdf59ef1357b3b2                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                          0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/node:12-alpine                                                                                                                                                                                                          0.6s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                                                                                                                                                0.0s
 => [1/5] FROM docker.io/library/node:12-alpine@sha256:dfa564312367b1a8fca8db7ae4bae102b28e68b39ebcb7b17022c938f105846b                                                                                                                                                    0.0s
 => [internal] load build context                                                                                                                                                                                                                                          2.9s
 => => transferring context: 2.92MB                                                                                                                                                                                                                                        2.5s
 => CACHED [2/5] RUN mkdir /app                                                                                                                                                                                                                                            0.0s
 => CACHED [3/5] WORKDIR /app                                                                                                                                                                                                                                              0.0s
 => CACHED [4/5] RUN npm install                                                                                                                                                                                                                                           0.0s
 => [5/5] COPY . .                                                                                                                                                                                                                                                         8.5s
 => exporting to image                                                                                                                                                                                                                                                    12.2s
 => => exporting layers                                                                                                                                                                                                                                                   12.0s
 => => writing image sha256:b32907debc011be0352b3a9d89461fd3177a3e1686b85d343420b2a520c20530                                                                                                                                                                               0.0s
 => => naming to docker.io/olivermctammany/demomap                                                                                                                                                                                                                         0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
olivermctammany@olivers-mbp map-tile-test % docker push olivermctammany/demomap
Using default tag: latest
The push refers to repository [docker.io/olivermctammany/demomap]
41c363924305: Pushed
db3c7f043bba: Layer already exists
5f70bf18a086: Layer already exists
ff268da50c61: Layer already exists
c5088e2ef878: Layer already exists
12587d8b4618: Layer already exists
738f7039e20b: Layer already exists
8d3ac3489996: Layer already exists
latest: digest: sha256:0377755c300765c7c4d4c8032b68121380f0a02961ba25b59e5256c05aa397f4 size: 1990
olivermctammany@olivers-mbp map-tile-test %

Minikube yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mapclient
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mapclient
  template:
    metadata:
      labels:
        app: mapclient
    spec:
      containers:
        - name: mapclient
          image: olivermctammany/demomap
          ports:
          - containerPort: 80
            hostPort: 3000


---
apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  type: NodePort
  selector:
    app: mapclient
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31000

Minikube deploy output:

olivermctammany@olivers-mbp map-tile-test % kubectl apply -f demomap.yaml
deployment.apps/mapclient created
service/frontend created
olivermctammany@olivers-mbp map-tile-test % kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
mapclient-6f4cc7bddd-x6mq5   1/1     Running   0          3m20s   172.17.0.5   minikube   <none>           <none>
olivermctammany@olivers-mbp map-tile-test % kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
frontend     NodePort    10.99.165.147   <none>        80:31000/TCP   2m16s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        3d7h
olivermctammany@olivers-mbp map-tile-test % kubectl get endpoints
NAME         ENDPOINTS           AGE
frontend     172.17.0.5:80       5m53s
kubernetes   192.168.49.2:8443   3d7h
olivermctammany@olivers-mbp map-tile-test % kubectl get node -o wide
NAME       STATUS   ROLES                  AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
minikube   Ready    control-plane,master   3d7h   v1.23.3   192.168.49.2   <none>        Ubuntu 20.04.2 LTS   5.10.47-linuxkit   docker://20.10.12
olivermctammany@olivers-mbp map-tile-test % kubectl describe svc frontend
Name:                     frontend
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=mapclient
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.99.165.147
IPs:                      10.99.165.147
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31000/TCP
Endpoints:                172.17.0.5:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Going to 192.168.49.2:31000, 172.17.0.5:80, 172.17.0.5:31000, and 10.99.165.147:31000 all just fail to connect. Not sure what my next steps should be. Any help is appreciated. Thanks



Solution 1:[1]

These three port numbers all need to match, and they also need to match the port the process inside the container is listening on:

# Dockerfile
EXPOSE 3000
# deployment.yaml
containers:
  ports:
    - containerPort: 3000
# service.yaml
targetPort: 3000

I'd avoid trying to specify a hostPort: for the pod; it can lead to unexpected conflicts scheduling the pods and may not be allowed in some environments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze