分布式消息队列(Cloud of Mosquitto)本文采用 Kubernetes + Mosquitto,利用 Kubernetes 平台快速创建 Mosquitto MQTT Broker 的 Bridge 集合。本部分利用之前建立 Kubernetes 系统创建pods和services创建多个 Mosquitto Broker 服务进行共享多个 Topic(即 Bridge)。本文参考:https://github.com/kairen/cloud-of-mosquitto,进行下面的步骤前请先下载:https://github.com/kairen/cloud-of-mosquitto.git
主要使用的配置问题如下:
1,cloud-of-mosquitto/k8s-service-bridge/mosquitto-bridge-svc.json
2,cloud-of-mosquitto/k8s-service-bridge/mosquitto-bridge-pods.json
使用命令创建pods:
//创建pods
kubectl apply -f cloud-of-mosquitto/k8s-service-bridge/mosquitto-bridge-pods.json
pod/mosquitto-1 created
pod/mosquitto-2 created
//创建service
kubectl apply -f project/cloud-of-mosquitto/k8s-service-bridge/mosquitto-bridge-svc.json
Error from server (Invalid): Service "mosquitto-1" is invalid: spec.ports[0].nodePort: Invalid value: 1883: provided port is not in the valid range. The range of valid ports is 30000-32767
Error from server (Invalid): Service "mosquitto-2" is invalid: spec.ports[0].nodePort: Invalid value: 1884: provided port is not in the valid range. The range of valid ports is 30000-32767
//此处报错和原因在于nodePort的定义不在valid ports的范围内,需要修改mosquitto-bridge-svc.json,将nodePort改成31883和31884
kubectl apply -f project/cloud-of-mosquitto/k8s-service-bridge/mosquitto-bridge-svc.json
service/mosquitto-1 created
service/mosquitto-2 created
//运行结果查看
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 14d
mosquitto-1 NodePort 10.1.254.249 <none> 1883:31883/TCP 3m27s
mosquitto-2 NodePort 10.1.198.71 <none> 1883:31884/TCP 3m26s
nginx-service ClusterIP 10.1.169.124 <none> 80/TCP 7d2h
kubectl get pods
NAME READY STATUS RESTARTS AGE
mosquitto-1 1/1 Running 0 4m20s
mosquitto-2 1/1 Running 0 4m20s
nginx 1/1 Running 0 7d2h
nginx-deployment-7fb7fd49b4-b6tcw 1/1 Running 0 7d2h
nginx-deployment-7fb7fd49b4-jxdx2 1/1 Running 0 7d2h
nginx-deployment-7fb7fd49b4-spxgr 1/1 Running 0 7d2h
使用mosquito_clients测试运行结果:
//监听topic消息
mosquitto_sub -h 10.10.30.76 -p 31883 -t mqtt//发送topic消息
mosquitto_pub -h 10.10.30.76 -p 31883 -t mqtt -m haha//监听topic消息
mosquitto_sub -h 10.10.30.76 -p 31883 -t mqtt//发送topic消息
mosquitto_pub -h 10.10.30.76 -p 31883 -t mqtt -m haha
网友评论