TanguyB
TanguyB

Reputation: 2006

Can't access kubernetes nodeport service

I don't think I miss anything, but my angular app doesn't seem to be able to contact the service I exposed trough Kubernetes.

Whenever I try to call the exposed nodeport on my localhost, I get a connection refused.

The deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: society-api-gateway-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: society-api-gateway-deployment
  template:
    metadata:
      labels:
        app: society-api-gateway-deployment
    spec:
      containers:
      - name: society-api-gateway-deployment
        image: tbusschaert/society-api-gateway:latest
        ports:
        - containerPort: 80

The service file

apiVersion: v1
kind: Service
metadata:
  name: society-api-gateway-service
spec:
  type: NodePort
  selector:
    app: society-api-gateway-deployment
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30001

I double checked, the call doesn't reach my pod, and this is the error I get when going the call:

enter image description here

I'm using minikube and kubectl on my local machine.

I'm out of options, tried everything I though it could be, thanks in advance.

EDIT 1:

So after following the suggestions, i used the node IP to call the service:

enter image description here

I changed the IP in my angular project, now I get a connection timeout:

enter image description here

As for the port forward, I get a permission error:

enter image description here

Upvotes: 0

Views: 3136

Answers (2)

TanguyB
TanguyB

Reputation: 2006

So, as I thought, the problem was related to minikube not opening up to my localhost.

First of all I didn't need a NodePort, but the LoadBalancer also fit my need, so my API gateway became a LoadBalancer.

Second, when using minikube, to achieve what I wanted ( running kubernetes on my local machine and my angular client also being on my local machine ), you have to create a minikube tunnel, exactly how they explain it here: https://minikube.sigs.k8s.io/docs/handbook/accessing/#run-tunnel-in-a-separate-terminal

Upvotes: 3

Emon46
Emon46

Reputation: 1626

From doc, you see that the templeate will be like this <NodeIP>:<NodePort>.

NodePort: Exposes the Service on each Node's IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You'll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.

So first, From kubectl get node -o wide comamnd take the NodeIP. then try with the <NodeIP>:<NodePort>. For example, If the NodeIP is 172.19.0.2 then try 172.19.0.2:30001 with your sub url.

Or, Another way is with port-forwarding. In a terminal first try port-forwarding with kubectl port-forward svc/society-api-gateway-service 80:80. Then use the url you have tried with localhost.

Upvotes: 1

Related Questions