Kubernetes Service does not map the right port

4/3/2019

I'd like to expose default port (1883) and WS port (9001) of MQTT server on an Azure Kubernetes Cluster.

Anyway here is the deployement I currently wrote :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mqtt-server
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: mqtt-server
  template: 
    metadata: 
      labels: 
        app: mqtt-server
        type: backend 
    spec: 
      containers: 
        - name: mqtt-server
          image: eclipse-mosquitto:1.5.4
          resources: 
            requests:
              cpu: 250m
              memory: 256Mi
          ports:
            - name: mqtt-dflt-port
              containerPort: 1883
            - name: mqtt-ws-port
              containerPort: 9001
---
apiVersion: v1
kind: Service
metadata:
  name: mqtt-server-service
spec:
  selector:
    app: mqtt-server
  type: LoadBalancer
  ports:
  - name: mqtt-dflt-port
    protocol: TCP
    port: 1883
    targetPort: 1883
  - name: mqtt-ws-port
    protocol: TCP
    port: 1884
    targetPort: 9001

And when I deploy it, everything is fine but the MQTT broker is unreachable and my service is described like that :

mqtt-server-service   LoadBalancer   10.0.163.167   51.143.170.64   1883:32384/TCP,1884:31326/TCP   21m

Why is the 1883/9001 port aren't forwarded like it should be ?

-- NeitoFR
azure
kubernetes
kubernetes-service
port

2 Answers

4/3/2019

First, make sure you’re connecting to the service’s cluster IP from within the cluster, not from the outside. Don’t bother pinging the service IP to figure out if the service is accessible (remember, the service’s cluster IP is a virtual IP and pinging it will never work). If you’ve defined a readiness probe, make sure it’s succeeding; otherwise the

pod won’t be part of the service. To confirm that a pod is part of the service, examine the corresponding End- points object with kubectl get endpoints . If you’re trying to access the service through its FQDN or a part of it (for exam- ple, myservice.mynamespace.svc.cluster.local or myservice.mynamespace) and it doesn’t work, see if you can access it using its cluster IP instead of the FQDN. Check whether you’re connecting to the port exposed by the service and not the target port. Try connecting to the pod IP directly to confirm your pod is accepting connec- tions on the correct port. If you can’t even access your app through the pod’s IP, make sure your app isn’t only binding to localhost.

-- yasin lachini
Source: StackOverflow

4/3/2019

I dont see anything wrong, ports you requested are being forwarded to. And service created temporary ports on nodes for traffic to flow (it always does that). Service got endpoints, everything is okay.

Just to give more context, it always does that, because it needs to route traffic to some port, but it cannot depend on this exact port, because it might be occupied, so its using random port from 30.000 range (by default).

https://kubernetes.io/docs/concepts/services-networking/service/#nodeport

If you must specify a known and static port assignment, you can add nodePort: some-number to your ports definition in your service. By default, nodeports are assigned in 30000-32767.

-- 4c74356b41
Source: StackOverflow