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

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.


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.


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

Tags:

Kubernetes