嘿伙计们,我遇到了一些困难。我能够成功运行我的 yaml 文件并让容器运行 MQTT。问题是当我尝试连接它时,它不起作用。这是我的 YAML 文件
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.22.0 (955b78124)
creationTimestamp: null
labels:
io.kompose.service: mqtt
name: mqtt
namespace: mqtt
spec:
replicas: 2
selector:
matchLabels:
io.kompose.service: mqtt
strategy:
type: Recreate
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.22.0 (955b78124)
creationTimestamp: null
labels:
io.kompose.service: mqtt
spec:
containers:
- image: eclipse-mosquitto
name: mqtt
ports:
- containerPort: 1883
- containerPort: 9001
resources: {}
volumeMounts:
- name: mosquitto-config-file
mountPath: /mosquitto/config
- name: mosquitto-pwfile
mountPath: /mosquitto/config/pwfile
- name: mqtt-data-pvc
mountPath: /mosquitto/data
- name: mqtt-log-pvc
mountPath: /mosquitto/log
restartPolicy: Always
volumes:
- name: mosquitto-config-file
configMap:
name: mosquitto-config
- name: mosquitto-pwfile
secret:
secretName: mosquitto-pwfile
- name: mqtt-data-pvc
persistentVolumeClaim:
claimName: mqtt-data-pvc
- name: mqtt-log-pvc
persistentVolumeClaim:
claimName: mqtt-log-pvc
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: mqtt
namespace: mqtt
spec:
ports:
- name: "1883"
port: 1883
targetPort: 1883
- name: "9001"
port: 9001
targetPort: 9001
selector:
io.kompose.service: mqtt
status:
loadBalancer: {}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mosquitto-config
namespace: mqtt
data:
mosquitto.conf: |
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
listener 1883
protocol mqtt
listener 9001
protocol websockets
allow_anonymous false
password_file /mosquitto/config/pwfile/pwfile
---
apiVersion: v1
kind: Secret
metadata:
name: mosquitto-pwfile
namespace: mqtt
type: Opaque
data:
pwfile: |
<BASE64 USERNAME:PW>
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mosquitto-data-pv
namespace: mqtt
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-path
local:
path: /home/pi/Software/mqtt/storage/mosquitto/data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- masternode
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mqtt-data-pvc
namespace: mqtt
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mosquitto-log-pv
namespace: mqtt
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-path
local:
path: /home/pi/Software/mqtt/storage/mosquitto/log
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- masternode
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mqtt-log-pvc
namespace: mqtt
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mqtt-ingress
namespace: mqtt
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
spec:
rules:
- host: mqtt.local
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: mqtt
port:
number: 1883
当我检查我的容器是否正在运行时,它们运行良好。当我检查日志时,它显示以下内容
1618785757: mosquitto version 2.0.10 starting
1618785757: Config loaded from /mosquitto/config/mosquitto.conf.
1618785757: Opening ipv4 listen socket on port 1883.
1618785757: Opening ipv6 listen socket on port 1883.
1618785757: Opening websockets listen socket on port 9001.
1618785757: mosquitto version 2.0.10 running
我尝试在端口 1883 上连接 mqtt.local(根据入口),但日志没有更新,它只是说使用我的 iPad 应用程序进行连接
如果我在浏览器上访问 mqtt.local,日志会更新(我相信他们正在使用 websockets) 它显示以下内容
1618786368: Client <unknown> disconnected due to protocol error.
1618786368: New connection from 10.42.1.3:48912 on port 1883.
1618786368: Client <unknown> disconnected due to protocol error.
1618786368: New connection from 10.42.1.3:57298 on port 1883.
1618786368: Client <unknown> disconnected due to protocol error.
我会说实话。我很困惑。用户名和密码确认了十几次。如果有人有想法可以分享,请虚心接受。预先感谢
您的 Ingress 声明指向端口 1883,该端口正在 TCP 上运行本机 MQTT。
Ingress 设置一个指向服务的 HTTP 反向代理,因为 MQTT 不是 HTTP,所以这是行不通的。
正如评论中指出的,一种选择是将 Ingress 更改为指向端口 9001,这是代理上的 WebSocket 侦听器
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mqtt-ingress
namespace: mqtt
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
spec:
rules:
- host: mqtt.local
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: mqtt
port:
number: 9001
然后,您将能够通过 Websockets 连接任何支持 MQTT 的 MQTT 客户端,因为该协议是通过 HTTP 引导的。
还值得指出的是,Ingress 反向代理将在端口 80 上公开此路由。
唯一的其他连接方式是直接连接到运行 MQTT 代理的集群节点。但这意味着将 ServiceType 更改为 NodePort 或 LoadBalancer 并映射外部 IP 地址。
我刚刚检查了上面的帖子,我猜问题是“应用”的顺序 - 如果使用上面的 yaml 文件进行 kubectl apply,那么首先加载部署,然后加载 configMap。必须首先加载 configMap,以便部署组件可以使用它们。
当我在我的服务中使用“UDP”作为协议时,我遇到了同样的错误 - 1883 支持 TCP 和 UDP,但我猜 UDP 需要额外的配置。 OP 没有指定协议,因此默认使用 TCP。