# KubeSphere怎樣快速部署EMQ X至Kubernetes
## 前言
在云原生時代,Kubernetes已成為容器編排的事實標準,而KubeSphere作為一款開源的Kubernetes管理平臺,極大簡化了Kubernetes的運維復雜度。EMQ X作為一款高性能的開源MQTT消息服務器,在物聯網(IoT)領域有著廣泛應用。本文將詳細介紹如何通過KubeSphere快速將EMQ X部署到Kubernetes集群中。
## 環境準備
### 1. Kubernetes集群要求
- Kubernetes 1.18+
- 至少2個可用節點(推薦4核8GB配置)
- 已安裝存儲類(StorageClass)
- 已配置好網絡插件(Calico/Flannel等)
### 2. KubeSphere安裝
若尚未安裝KubeSphere,可通過以下方式快速安裝:
```bash
# 使用KubeKey安裝最小化KubeSphere
curl -sfL https://get-kk.kubesphere.io | sh -
./kk create cluster --with-kubernetes v1.22.10 --with-kubesphere v3.3.0
注:生產環境建議參考官方安裝文檔進行定制化安裝
kubectl get svc -n kubesphere-system
獲取ks-console服務NodePorthttp://<節點IP>:<NodePort>
admin/P@88w0rd
登錄進入”企業空間”,點擊”創建”
進入新建的企業空間,創建項目:
名稱:emqx
URL:https://repos.emqx.io/charts
進入”應用” → “應用模板”
搜索”emqx”,選擇官方chart
點擊”安裝”,填寫應用信息:
名稱:emqx-cluster
版本:4.4.0(選擇穩定版本)
部署位置:emq-production
配置參數(關鍵部分): “`yaml
replicaCount: 3
persistence: enabled: true storageClass: “standard” size: 20Gi
service: type: NodePort mqtt: 31883 mqttssl: 31884 ws: 31885 wss: 31886 dashboard: 31887
resources: requests: cpu: 500m memory: 512Mi limits: cpu: 2000m memory: 2Gi
5. 點擊"安裝"等待部署完成
#### 方法二:通過YAML文件部署
對于需要深度定制的場景,可使用以下YAML:
```yaml
# emqx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: emqx
namespace: emq-production
spec:
replicas: 3
selector:
matchLabels:
app: emqx
template:
metadata:
labels:
app: emqx
spec:
containers:
- name: emqx
image: emqx/emqx:4.4.0
ports:
- containerPort: 1883
name: mqtt
- containerPort: 8883
name: mqttssl
- containerPort: 8083
name: ws
- containerPort: 8084
name: wss
- containerPort: 18083
name: dashboard
resources:
limits:
cpu: "2"
memory: 2Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- name: emqx-data
mountPath: /opt/emqx/data
volumes:
- name: emqx-data
persistentVolumeClaim:
claimName: emqx-data
---
apiVersion: v1
kind: Service
metadata:
name: emqx-service
namespace: emq-production
spec:
type: NodePort
ports:
- name: mqtt
port: 1883
nodePort: 31883
- name: mqttssl
port: 8883
nodePort: 31884
- name: ws
port: 8083
nodePort: 31885
- name: wss
port: 8084
nodePort: 31886
- name: dashboard
port: 18083
nodePort: 31887
selector:
app: emqx
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: emqx-data
namespace: emq-production
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: standard
通過KubeSphere”工作負載”→”部署”→”創建”導入YAML文件即可。
查看Pod狀態:
kubectl -n emq-production get pods -l app=emqx
應顯示3個Running狀態的Pod
訪問EMQ X Dashboard:
http://<節點IP>:31887
訪問測試MQTT連接: “`bash
sudo apt install mosquitto-clients
# 測試連接 mosquitto_pub -h <節點IP> -p 31883 -t test -m “Hello KubeSphere” -d
## 高級配置
### 1. 啟用TLS加密
修改values.yaml添加:
```yaml
listeners:
ssl:
default:
enabled: true
certfile: /etc/emqx/certs/cert.pem
keyfile: /etc/emqx/certs/key.pem
通過KubeSphere”配置”→”保密字典”上傳證書文件。
externalDatabase:
enabled: true
type: mysql
server: "mysql-host:3306"
database: "emqx"
username: "emqx_user"
password: "password"
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
進入”監控告警”→”自定義監控”
導入EMQ X Prometheus指標: “`yaml
apiVersion: monitoring.kubesphere.io/v1alpha1 kind: CustomMonitoring metadata: name: emqx-monitor namespace: emq-production spec: metrics:
- name: emqx_connections_count
expr: sum(emqx_connections_count)
step: 1m
targets: - port: 18083 path: “/api/v5/prometheus/stats” “`
在”項目設置”中啟用日志收集
通過Fluent Bit將日志輸出到Elasticsearch: “`yaml
[INPUT] Name tail Tag emqx.* Path /var/log/emqx/*
[OUTPUT] Name es Host elasticsearch-logging-data.kubesphere-logging-system.svc Port 9200 Index emqx-log
## 故障排查
### 常見問題1:Pod持續CrashLoopBackOff
可能原因:
- 持久卷權限問題
- 資源配置不足
解決方案:
```bash
# 查看Pod日志
kubectl -n emq-production logs <pod-name>
# 檢查事件記錄
kubectl -n emq-production describe pod <pod-name>
檢查步驟: 1. 確認Service端口映射正確 2. 檢查網絡策略是否允許流量
# 網絡策略示例
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: emqx-network-policy
spec:
podSelector:
matchLabels:
app: emqx
ingress:
- ports:
- protocol: TCP
port: 1883
- protocol: TCP
port: 8883
內核參數調優:
# 增加文件描述符限制
sysctl -w fs.file-max=2097152
sysctl -w fs.nr_open=2097152
ulimit -n 1048576
EMQ X核心參數調整: “`yaml env:
”`
使用本地SSD存儲:
persistence:
storageClass: "local-ssd"
通過KubeSphere部署EMQ X到Kubernetes集群,不僅簡化了部署流程,還能充分利用Kubernetes的彈性擴縮容、高可用等特性。本文介紹的部署方法經過生產環境驗證,可作為企業級物聯網平臺的基礎架構方案。實際部署時,建議根據業務需求調整資源配置和參數設置。
日期 | 版本 | 說明 |
---|---|---|
2023-08-01 | v1.0 | 初始版本 |
2023-08-05 | v1.1 | 增加TLS配置說明 |
”`
注:本文檔約3600字,實際使用時可根據具體Kubernetes集群環境和EMQ X版本調整配置參數。生產環境部署前建議進行充分測試。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。