Cannot connect to Kubernetes NodePort Service
Asked Answered
W

2

5

I have a running pod that was created with the following pod-definition.yaml:

apiVersion: v1
kind: Pod
metadata:
    name: microservice-one-pod-name
    labels:
        app: microservice-one-app-label
        type: front-end
spec:
    containers:
    - name: microservice-one
      image: vismarkjuarez1994/microserviceone
      ports:
      - containerPort: 2019

I then created a Service using the following service-definition.yaml:

kind: Service
apiVersion: v1
metadata:
  name: microserviceone-service
spec:
  ports:
    - port: 30008
      targetPort: 2019
      protocol: TCP
  selector:
    app: microservice-one-app-label
  type: NodePort

I then ran kubectl describe node minikube to find the Node IP I should be connecting to -- which yielded:

Addresses:
  InternalIP:  192.168.49.2
  Hostname:    minikube

But I get no response when I run the following curl command:

curl 192.168.49.2:30008

The request also times out when I try to access 192.168.49.2:30008 from a browser.

The pod logs show that the container is up and running. Why can't I access my Service?

Wigfall answered 27/3, 2022 at 6:30 Comment(0)
D
7

The problem is that you are trying to access your service at the port parameter which is the internal port at which the service will be exposed, even when using NodePort type.

The parameter you were searching is called nodePort, which can optionally be specified together with port and targetPort. Quoting the documentation:

By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)

Since you didn't specify the nodePort, one in the range was automatically picked up. You can check which one by:

kubectl get svc -owide

And then access your service externally at that port.

As an alternative, you can change your service definition to be something like:

kind: Service
apiVersion: v1
metadata:
  name: microserviceone-service
spec:
  ports:
    - port: 30008
      targetPort: 2019
      nodePort: 30008
      protocol: TCP
  selector:
    app: microservice-one-app-label
  type: NodePort

But take in mind that you may need to delete your service and create it again in order to change the nodePort allocated.

Dioptrics answered 27/3, 2022 at 8:18 Comment(3)
Thanks for the suggestion -- I've changed my service definition to match your suggestion, but I still get no response when running curl 192.168.49.2:30008Wigfall
Do you still have problems? As I see you accepted the answer as correctDioptrics
Your solution was correct -- I simply had to tunnel into Minikube in order to access. ThanksWigfall
O
3

I think you missed the Port in your service:

apiVersion: v1
kind: Pod
metadata:
    name: microservice-one-pod-name
    labels:
        app: microservice-one-app-label
        type: front-end
spec:
    containers:
    - name: microservice-one
      image: vismarkjuarez1994/microserviceone
      ports:
      - containerPort: 2019

and your service should be like this:

kind: Service
apiVersion: v1
metadata:
  name: microserviceone-service
spec:
  ports:
    - port: 2019
      targetPort: 2019
      nodePort: 30008
      protocol: TCP
  selector:
    app: microservice-one-app-label
  type: NodePort

You can access to your app after enabling the Minikube ingress if you want trying Ingress with Minikube.

minikube addons enable ingress
Octoroon answered 27/3, 2022 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.