Kubernetes NodePort service time out

504 views Asked by At

I am trying to deploy a pod in kubernetes and link a service to it so I can access it via web browser.

The pod specification is the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: visualizer-nautic
  labels:
    app: visualizer
spec:
  replicas: 1
  selector:
    matchLabels:
      app: visualizer
  template:
    metadata:
      annotations:
        k8s.v1.cni.cncf.io/networks: zenoh-network
      labels:
        app: visualizer
    spec:
      containers:
      - name: api
        image: hielito/visualizer:latest
        ports:
          - containerPort: 8100

It deploys a web app, its frontend is exposed at port 8100, so then I add a NodePort service to make it visible in my network:

apiVersion: v1
kind: Service
metadata:
  name: visualizer-service
spec:
  type: NodePort
  selector:
    app: visualizer
  ports:
    - port: 8100
      targetPort: 8100
      nodePort: 30012

Once I have this service deployed, I try to access localhost:30012, but the web browser just gets stuck at loading and eventually throw a timeout error.

If anyone is wondering, the app deployed is working fine, if I execute a port-forward (microk8s kubectl port-forward pods/visualizer-nautic-77957b94c9-mww6p 8100:8100 ) I can then access the app working perfectly at localhost:8100.

EDIT: I forgot to add that I am using multus CNI add-on and have a MACVLAN network interface added to the pod. When I remove the additional CNI it works fine again, so the MACVLAN network might be interfering with the service.

EDIT2: As requested, I leave specs and versions here:
OS - Ubuntu 20.04 LTS
Kubernetes - Microk8s 1.23

1

There are 1 answers

8
Ralle Mc Black On

The nodeport is on your kubernetes node. Accessing from your local machine wont work. Use http://masternodeipaddress:nodeport.

You can add a hostentry in your local hosts file like:

masternodeipaddress mytestdomain.local

then access http://mytestdomain.local:nodeport.

Update

With service type Loadbalancer.
Get the IP from your masternode.
I tried microk8s enable host-access, but had issues so i put the nodes ip address in externalIPs, as below. From the browser http://nmasternodeipaddress.

deployment.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: app
spec:
  ports:
    - name: tcp
      port: 80
      protocol: TCP
      targetPort: 80
  externalIPs: 
    - 10.165.39.165 # master node ip
  selector:
    app: app
  type: LoadBalancer


---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
        - image: nginx
          name: app
          ports:
            - containerPort: 80
              protocol: TCP
        
      restartPolicy: Always

with nodePort

apiVersion: v1
kind: Service
metadata:
  name: app
spec:
  ports:
    - name: tcp
      port: 80
      protocol: TCP
      targetPort: 80
      nodePort: 31000 # optional, use kubectl get svc -o wide if you let it blank
  externalIPs: 
    - 10.165.39.165 # master node ip
  selector:
    app: app
  type: NodePort

To set a domain local:

sudo vi /etc/hosts

add a line with masternode ip like:

10.165.39.165 mydomain.local

Now you can access in your browser with

http://mydomain.local

Note: I have tested this with: Ubuntu 22.04 multipass 3 nodes with microk8s 1.24 in microk8s dns ingress enabled.