Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access service in my local kubernetes cluster using NodePort

Tags:

I have a manifest as the following

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-redis
spec:
  selector:
    matchLabels:
      app: my-redis
  replicas: 1
  template:
    metadata:
      labels:
        app: my-redis
    spec:
      containers:
       - name: my-redis
         image: redis
         ports:
         - name: redisport1
           containerPort: 6379
           hostPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  labels:
    app: my-redis
spec:
  type: NodePort
  selector:
    name: my-redis
  ports:
  - name: redisport1
    port: 6379
    targetPort: 6379
    nodePort: 30036
    protocol: TCP

This is a sample that reproduces my problem. My intention here is to create a simple cluster that has a pod with a redis container in it, and it should be exposed to my localhost. Still, get services gives me the following output:

redis-service   NodePort    10.107.233.66   <none>        6379:30036/TCP   10s

If I swap NodePort with LoadBalancer, I get an external-ip but still port doesn't work.

Can you help me identify why I'm failing to map the 6379 port to my localhost, please?

Thanks,

like image 947
Akaedintov Avatar asked Nov 01 '18 16:11

Akaedintov


People also ask

How do I access NodePort services from outside cluster?

Exposing services as NodePort : Declaring a Service as NodePort exposes it on each Node's IP at a static port (referred to as the NodePort ). You can then access the Service from outside the cluster by requesting <NodeIp>:<NodePort> . This can also be used for production, albeit with some limitations.

How do I access NodePort services?

Form the URL with one of the worker node IP addresses and the NodePort. Example: http://192.0.2.23:30872 . For VPC clusters, you must be connected to the private network, such as through a VPN connection, to access the worker node private IP address and NodePort.

How do you expose a service in Kubernetes cluster?

From the Service type drop-down list, select Cluster IP. Click Expose. When your Service is ready, the Service details page opens, and you can see details about your Service. Under Cluster IP, make a note of the IP address that Kubernetes assigned to your Service.


2 Answers

In order to access your app through node port, you have to use this url http://{node ip}:{node port}.

If you are using minikube, your minikube ip is the node ip. You can retrieve it using minikube ip command.

You can also use minikube service redis-service --url command to get the url to access your application through node port.

like image 151
Emruz Hossain Avatar answered Sep 20 '22 15:09

Emruz Hossain


For anybody who's interested in the question, I found the problem. After Ijaz's fix, I also needed to change the selector to match the label in the pod, it was a typo on my end!

pod has "app=my-redis" tag, but Service selector had "name=my-redis". Matching them fixed the access problem.

like image 44
Akaedintov Avatar answered Sep 19 '22 15:09

Akaedintov