Can't connect to targetPort of NodePort service

12/10/2021

I am having a rough time understanding networking on the local machine for my cluster. I have built a simple cluster just to run an Nginx web server for static files using minikube.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: Nginx
spec:
  selector:
    matchLabels:
      app: Nginx
  template:
    metadata:
      labels:
        app: Nginx
    spec:
      containers:
      - name: nginx
        image: pattersonbl2/webapp
        ports:
        - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: Nginx
spec:
  selector:
    app: Nginx
  ports:
  - port: 80
    targetPort: 3088
    name: https
  type: NodePort 

This will show up on my localhost IP on port 52723. I thought it would be on 3088. How do you understand what port your content will be served on ?.

-- Brandon Patterson
kubernetes
nginx

1 Answer

12/10/2021

This will show up on my localhost IP on port 52723. I thought it would be on 3088. How do you understand what port your content will be served on ?.

You have not set the nodePort in your file, this means any random node port would be set between 30000-32767. You can set it to some value at the time of creation to be certain of the node port. See description below.

apiVersion: v1
kind: Service
metadata:
  name: Nginx
spec:
  selector:
    app: Nginx
  ports:
  - port: 80              #<---This is internal port for the service which would be forwarded to targetPort
    targetPort: 3088      #<----Application is listning on this, although this seems off, typically nginx listen on 80
    nodePort: <some-port> #<----External traffic would come here, If not set random port would be set between   30000-32767. 
    name: https
  type: NodePort 
-- P....
Source: StackOverflow