无法在公共域上连接Mosquitto Broker

时间:2020-08-18 04:45:13

标签: kubernetes mosquitto

我的Kubernetes上有一个Mosquitto经纪人。我可以连接到专用网络中的Mosquitto Broker。它运作良好。 但是,当我们使用公共域(我们使用Sophos UTM 9)时,客户端无法连接到Mosquitto Broker。

我是Kubernetes的新手。这是mosquitto.yaml文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mosquitto
spec:
  selector:
    matchLabels:
      app: mosquitto
  replicas: 1
  template:
    metadata:
      labels:
        app: mosquitto
    spec:
      containers:
      - name: mosquitto
        image: eclipse-mosquitto:v1.16.10
        resources:
          limits:
            cpu: "1"
            memory: 2Gi
          requests:
            cpu: "1"
            memory: 2Gi
        imagePullPolicy: Always
        ports:
        - containerPort: 1883
---

apiVersion: v1
kind: Service
metadata:
  name: mosquitto
spec:
  externalIPs:
  - xxx.xxx.xxx.xxx
  type: ClusterIP
  ports:
    - name: mqtt
      port: 1883
      targetPort: 1883
      protocol: TCP
  selector:
    app: mosquitto

我使用NodeJS来连接公共领域。此NodeJS代码为:

var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://mydomain.com:1883');

client.on('connect', function () {
    client.subscribe(topic)
    console.log("Subscribed topic " + topic);
})

我想知道问题是kubernetes还是Sophos UTM9。我错过了什么吗?

我必须为Kubernetes上的Mosquitto使用公共域做什么?

我非常感谢。

1 个答案:

答案 0 :(得分:0)

在测试了yaml文件后,我得出的结论是您的配置几乎正确,这是因为:

  • 您使用的eclipse-mosquitto:v1.16.10图片不存在。您可以检查所有可用的标签here

因此,最可能的问题是您的pod可能未运行。您可以通过运行以下命令并检查列STATUS来对其进行检查。

$ kubectl get pods -l=app=mosquitto
NAME                        READY   STATUS    RESTARTS   AGE
mosquitto-c9dc57d59-98l8r   1/1     Running   0          5m53s

这对我有用。 注意:出于测试目的,我已从服务和部署中删除了externalIPresource limits,并为eclipse-mosquitto:1.6.10替换了图像:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mosquitto
spec:
  selector:
    matchLabels:
      app: mosquitto
  replicas: 1
  template:
    metadata:
      labels:
        app: mosquitto
    spec:
      containers:
      - name: mosquitto
        image: eclipse-mosquitto:1.6.10
        imagePullPolicy: Always
        ports:
        - containerPort: 1883
---

apiVersion: v1
kind: Service
metadata:
  name: mosquitto
spec:
  type: ClusterIP
  ports:
    - name: mqtt
      port: 1883
      targetPort: 1883
      protocol: TCP
  selector:
    app: mosquitto

部署后,我已经使用dnsutil容器进行了测试(您可以找到规格here):

kubectl exec dnsutils -- sh -c 'apk update && apk add mosquitto-clients'
kubectl exec dnsutils -- mosquitto_pub -h mosquitto -t 'test/topic' -m 'upvoteIt'

检查蚊子荚中的日志:

kubectl logs mosquitto-xxxxx 

1597829622: New client connected from 172.17.0.4 as mosqpub|88-dnsutils (p1, c1, k60).
1597829622: Client mosqpub|88-dnsutils disconnected.

如果要在测试前查看消息,请打开第二个终端并运行以下命令以查看mosquitto服务器正在接收的消息:

$ kubectl exec mosquitto-xxxxx -- mosquitto_sub -v -t 'test/topic'
test/topic upvoteIt

其中mosquitto-xxxxx是您的广告连播的名称。