Home > other >  NodePort service is not exposed outside kubernetes cluster
NodePort service is not exposed outside kubernetes cluster

Time:10-31

I have following configuration of a service:

apiVersion: v1
kind: Service
metadata:
  name: academy-backend-service
spec:
  selector:
    app: academy-backend-app
  type: NodePort
  ports:
    - port: 8081
      targetPort: 8081
      nodePort: 30081

Behind this service there is a deployment that runs a docker image of a spring boot application that expose port 8081. When I try to reach the application from browser on http://localhost:30081 I don't get anything (not reachable). However if I connect inside minikube cluster, the application is available on http:{servcieip}:8081. Any clues what is not configured properly? I thought that nodePort is enough.

NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
academy-backend-service   NodePort    10.97.44.87      <none>        8081:30081/TCP   34m

CodePudding user response:

Use NodePorts to expose the service nodePort on all nodes in the cluster.

From https://kubernetes.io/docs/concepts/services-networking/service/

NodePort: Exposes the Service on each Node's IP at a static port (the NodePort). To make the node port available, Kubernetes sets up a cluster IP address, the same as if you had requested a Service of type: ClusterIP.

If you want to expose your service outside the cluster, use LoadBalancer type:

LoadBalancer: Exposes the Service externally using a cloud provider's load balancer.

or use ingress-controller which is a reverse-proxy that routs traffics from outside to your cluster: https://github.com/kubernetes/ingress-nginx

CodePudding user response:

Declaring a service as NodePort exposes the Service on each Node's IP at the NodePort (a fixed port for that Service , in the default range of 30000-32767). You can then access the Service from outside the cluster by requesting :

Below command can work for you:

curl <node-ip>:<node-port>         # curl <node-ip>:30081

Refer to this similar node port and stack question for more information.

  • Related