Can't access my node in k8s via browser (with service & deployment)

8/3/2020

There is the deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-task-tracker-deployment
spec:
  selector:
    matchLabels:
      app: my-task-tracker
  replicas: 5
  template:
    metadata:
      labels:
        app: my-task-tracker
    spec:
      containers:
        - name: hello-world
          image: shaikezam/task-tracker:1.0
          ports:
            - containerPort: 8080
              protocol: TCP

This is the service (NodePort):

apiVersion: v1
kind: Service
metadata:
  name: my-task-tracker-service
  labels:
    app: my-task-tracker
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8085
      nodePort: 30001
      protocol: TCP
  selector:
    app: my-task-tracker

Now, I try to access localhost:8085 or localhost:30001, and nothing happened.

I'm running using K8S in docker desktop.

Any suggestion what I'm doing wrong?

-- Shay Zambrovski
deployment
docker
kubernetes

1 Answer

8/3/2020

Target port should be 8080 in service yaml if that is what your container runs on as per your deployment yaml file.

apiVersion: v1
kind: Service
metadata:
  name: my-task-tracker-service
  labels:
    app: my-task-tracker
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30001
      protocol: TCP
  selector:
    app: my-task-tracker

=======

port exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port.

TargetPort is the port on which the service will send requests to, that your pod will be listening on. Your application in the container will need to be listening on this port also.

NodePort exposes a service externally to the cluster by means of the target nodes IP address and the NodePort. NodePort is the default setting if the port field is not specified. You should be able to use your application on Nodeport as well.

In your case target port should be 8080 that is what is important for app to run ,you can listen to your app on port 8085 within your cluster by changing the port field in the yaml and externally by changing the Nodeport.

-- Tarun Khosla
Source: StackOverflow