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

11/1/2018

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,

-- Akaedintov
kubernetes

3 Answers

11/3/2018

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.

-- Akaedintov
Source: StackOverflow

11/1/2018

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.

-- Emruz Hossain
Source: StackOverflow

11/1/2018

Dont need the hostPort:

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


---
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

now the nodePort 30036 can be used to access the service on any worker node.

If the cluster node is somewhere else and you want to make the port available on you local client , then just do kubectl port forward

kubectl port-forward svc/redis-service 6379:6379

https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Notes:

  • On-prem installs of k8s dont support service type of load balancer
  • ClusterIP is the IP on the pod network
  • Node IP is the IP of some machine that is running the k8s cluster
-- Ijaz Ahmad Khan
Source: StackOverflow