文章目录
基础知识
## EMQX 相关
1、EMQX 5.x系列,版本号< 5.9的开源版,支持3节点集群(我们使用 5.8.6版本),5.9及其之后的版本,只能单节点部署。
2、EMQX 5.8.6 开源版只支持 Core 节点集群,企业版所有节点都默认为 Core 节点。
3、EMQX 核心节点之间 要求 在10ms 以下,超过100ms 将不可用。
4、EMQX 核心节点要求 较大内存,在不承接连接的情况下 CPU 消耗较低。
5、emqx 容器内程序运行在emqx 这个用户下,若挂载主机目录,要考虑777 或者 创建一个emqx 账户并赋予所有者,所属组。
6、容器内emqx 数据是存储在 emqx@hostname 下的,所以要固化 主机名,否则容器再创建时 可能丢数据。
7、EMQX 集群节点直接 cookie 必须一致。
8、在线生成 emqx 的docker-compose.yml ,可以用该网站生成 https://docker.emqx.dev
## EMQX 集群端口说明
端口 协议 描述
1883 TCP MQTT over TCP 监听器端口,主要用于未加密的 MQTT 连接。
8883 TCP MQTT over SSL/TLS 监听器端口,用于加密的 MQTT 连接。
8083 TCP MQTT over WebSocket 监听器端口,使 MQTT 能通过 WebSocket 进行通信。
8084 TCP MQTT over WSS (WebSocket over SSL) 监听器端口,提供加密的 WebSocket 连接。
18083 HTTP EMQX Dashboard 和 REST API 端口,用于管理控制台和 API 接口。
4370 TCP Erlang 分布式传输端口,根据节点名称不同实际端口可能是 BasePort (4370) + Offset。
5370 TCP 集群 RPC 端口(在 Docker 环境下为 5369),根据节点名称不同实际端口可能是 BasePort (5370) + Offset。
## 官方文档
# emqx 开启 mqtts
https://docs.emqx.com/zh/emqx/latest/network/emqx-mqtt-tls.html
# emqx 集群负载均衡
https://docs.emqx.com/zh/emqx/latest/deploy/cluster/lb.html
# 用 HAProxy 负载均衡 EMQX 集群
https://docs.emqx.com/zh/emqx/latest/deploy/cluster/lb-haproxy.html
2台虚拟机,分别部署一个EMQX(非k8s 且非同一台云主机,故docker 我使用host网络模式)
配置 emqx
## 宿主机上创建对应目录,并授予权限(两台emqx云主机都要操作)
mkdir /opt/devops/emqx -p
mkdir /opt/devops/emqx/plugins -p
useradd -m -s /sbin/nologin emqx
mkdir /data/emqx_data -p
mkdir /var/log/emqx -p
chown emqx:emqx /opt/devops/emqx/plugins
chown emqx:emqx /data/emqx_data
chown emqx:emqx /var/log/emqx
# 自定义hosts文件
cd /opt/devops/emqx
cat > /opt/devops/emqx/hosts <<EOF
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.60.82 node1.emqx.atstm.cc
192.168.60.83 node2.emqx.atstm.cc
EOF
配置 emqx.conf(两节点配置可以一样)
============ 同时支持v4 和v6的 1883(MQTT over TCP)并开启代理协议(仅适用于 前端有LB的场景)====
cat /opt/devops/emqx/emqx.conf
## Place read-only configurations in this file.
## To define configurations that can later be overridden through UI/API/CLI, add them to `etc/base.hocon`.
##
## Config precedence order:(这是emqx的配置优先级)
## etc/base.hocon < cluster.hocon < emqx.conf < environment variables
##
## See https://www.emqx.io/docs/en/latest/configuration/configuration.html for more information.
## Configuration full example can be found in etc/examples
node {
name = "emqx@127.0.0.1"
cookie = "xxxxxxxxxxxxxxxxxdsdsdsdxxx"
data_dir = "data"
}
# 对tcp listener 监听所有 IPv6 地址的 1883,同时也会监听ipv4的1883,因为大多数linux, 默认是双栈模式 (双栈模式net.ipv6.bindv6only=0,RockyLinux9 默认就是 0)
listeners.tcp.default {
bind = ":::1883"
# 开启协议代理,仅适用于前段有LB的情况,(也可以在dashboard web面板手动开启)
proxy_protocol = true
}
cluster {
name = emqxcl
discovery_strategy = static
static {
seeds = ["emqx@node1.emqx.atstm.cc", "emqx@node2.emqx.atstm.cc"]
}
}
dashboard {
listeners {
http.bind = 18083
# https.bind = 18084
https {
ssl_options {
certfile = "${EMQX_ETC_DIR}/certs/cert.pem"
keyfile = "${EMQX_ETC_DIR}/certs/key.pem"
}
}
}
}
配置 docker-compose.yml (两节点 非k8s且不在同一主机,故用host模式,请自行修改文件中hostname字段)
cat /opt/devops/emqx/docker-compose.yml
services:
emqx:
image: emqx:5.8.6
container_name: emqx
hostname: node1.emqx.atstm.cc
#ports:
# - "18083:18083"
# - "1883:1883"
# - "8083:8083"
# - "8883:8883"
# - "8084:8084"
volumes:
- /data/emqx_data:/opt/emqx/data:rw
- /var/log/emqx/:/opt/emqx/log:rw
- ./plugins:/opt/emqx/plugins:rw
- ./emqx.conf:/opt/emqx/etc/emqx.conf:ro
- ./hosts:/etc/hosts:ro
environment:
- EMQX_HOST=node1.emqx.atstm.cc
- EMQX_NODE__DATA_DIR="data"
- EMQX_LOG__FILE__DEFAULT__ENABLE=true
- EMQX_LOG__CONSOLE__ENABLE=false
- EMQX_DASHBOARD__LISTENERS__HTTP__BIND=18083
- TZ=Asia/Shanghai
privileged: true
restart: unless-stopped
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 60s
timeout: 15s
retries: 3
start_period: 90s
# host 网络模式下,不能写这些优化参数,需放置在宿主机上优化
#sysctls:
# net.core.somaxconn: 32768
# net.ipv4.tcp_syncookies: 0
# net.ipv4.tcp_max_syn_backlog: 16384
# net.ipv4.tcp_max_tw_buckets: 1048576
# net.ipv4.tcp_fin_timeout: 15
# net.ipv4.ip_local_port_range: "1024 65535"
#ulimits:
# nproc: 1048576
# nofile:
# soft: 1048576
# hard: 1048576
network_mode: host
#networks:
# - emqx_bridge
#networks:
# emqx_bridge:
# driver: bridge
cd /opt/devops/emqx/
docker compose up -d
EMQX 用pull方式与 Prometheus 集成
## 官方文档
https://docs.emqx.com/zh/emqx/v5.8/observability/prometheus.html#%E9%9B%86%E6%88%90-prometheus
## 指标手册
https://docs.emqx.com/zh/emqx/v5.0/observability/metrics-and-stats.html#metrics-stats
## grafana 图表ID:17446
https://grafana.com/grafana/dashboards/17446-emqx/
## EMQX 提供以下 REST API 供 Prometheus 采集系统指标
/api/v5/prometheus/stats:EMQX 的基础指标及计数器
/api/v5/prometheus/auth:包含访问控制中认证和鉴权的关键指标及计数器
/api/v5/prometheus/data_integration:包含规则引擎,连接器,动作,Sink/Source,编解码相关指标及计数器。
## 使用 URL 查询参数 mode 来获取不同模式的指标数据。不同参数的含义如下
mode=node 默认模式,会返回当前请求节点的指标
mode=all_nodes_aggregated 聚合集群指标,返回的是集群中所有运行节点指标的算术和或者逻辑和
mode=all_nodes_unaggregated 集群指标非聚合模式,返回的是集群中所有运行节点的各自指标
## prometheus.yml 集成配置(请先到emqx 创建一个用于监控的账户,并把账户密码填入下方)
- job_name: 'emqx_stats'
scrape_interval: 15s
static_configs:
- targets: ['127.0.0.1:18083']
metrics_path: '/api/v5/prometheus/stats'
scheme: 'http'
basic_auth:
username: ''
password: ''
- job_name: 'emqx_auth'
scrape_interval: 15s
static_configs:
- targets: ['127.0.0.1:18083']
metrics_path: '/api/v5/prometheus/auth'
scheme: 'http'
basic_auth:
username: ''
password: ''
- job_name: 'emqx_data_integration'
scrape_interval: 15s
static_configs:
- targets: ['127.0.0.1:18083']
metrics_path: '/api/v5/prometheus/data_integration'
scheme: 'http'
basic_auth:
username: ''
password: ''
如果文章对你有帮助,欢迎点击上方按钮打赏作者
暂无评论