标题 k8s 搭建 emqx mqtt

EMQ X 是基于 Erlang/OTP 平台开发的开源物联网 MQTT 消息服务器,重点是专业支持MQTT协议,(MQTT 是轻量的 (Lightweight)、发布订阅模式 (PubSub) 的物联网消息协议),在物联网领域非常适合。

  1. 创建emqx-comfigmap.yaml

kubectl create -f emqx-comfigmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: emqx-config
  namespace: db
data:
  EMQX_CLUSTER__K8S__ADDRESS_TYPE: hostname
  EMQX_CLUSTER__K8S__APISERVER: https://kubernetes.default.svc:443
  EMQX_CLUSTER__K8S__SUFFIX: svc.cluster.local

  1. 创建emqx-rbac.yaml

kubectl create -f emqx-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: db
  name: emqx
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  namespace: db
  name: emqx
rules:
- apiGroups:
  - ""
  resources:
  - endpoints
  verbs:
  - get
  - watch
  - list
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  namespace: db
  name: emqx
subjects:
  - kind: ServiceAccount
    name: emqx
    namespace: db
roleRef:
  kind: Role
  name: emqx
  apiGroup: rbac.authorization.k8s.io
  1. 创建svc - headless.yaml

kubectl apply -f headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: emqx-headless
  namespace: db
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app: emqx
  ports:
  - name: mqtt
    port: 1883
    protocol: TCP
    targetPort: 1883
  - name: mqttssl
    port: 8883
    protocol: TCP
    targetPort: 8883
  - name: mgmt
    port: 8081
    protocol: TCP
    targetPort: 8081
  - name: websocket
    port: 8083
    protocol: TCP
    targetPort: 8083
  - name: wss
    port: 8084
    protocol: TCP
    targetPort: 8084
  - name: dashboard
    port: 18083
    protocol: TCP
    targetPort: 18083

  1. 创建emqx-statefulset.yam
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: emqx-statefulset
  namespace: db
  labels:
    app: emqx
spec:
  replicas: 3
  serviceName: emqx-headless
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: emqx
  template:
    metadata:
      labels:
        app: emqx
    spec:
      serviceAccountName: emqx
      containers:
      - name: emqx
        image: harbor.trtjk.com/devops/emqx:v4.1.5
        ports:
        - name: mqtt
          containerPort: 1883
        - name: mqttssl
          containerPort: 8883
        - name: mgmt
          containerPort: 8081
        - name: ws
          containerPort: 8083
        - name: wss
          containerPort: 8084
        - name: dashboard
          containerPort: 18083
        envFrom:
          - configMapRef:
              name: emqx-config
        env:
        - name: EMQX_CLUSTER__K8S__NAMESPACE
          value: db
        - name: EMQX_CLUSTER__K8S__SERVICE_NAME
          value: emqx-headless
        - name: EMQX_CLUSTER__K8S__APP_NAME
          value: emqx
        - name: EMQX_NAME
          value: emqx
        - name: EMQX_CLUSTER__DISCOVERY
          value: k8s
        volumeMounts:
        - name: emqx-data
          mountPath: "/opt/emqx/data/mnesia"
  volumeClaimTemplates:
  - metadata:
      name: emqx-data
    spec:
      storageClassName: rook-ceph-block
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi

#查看单个节点情况
kubectl exec -it emqx-statefulset-0 -ndb -- emqx_ctl status
#查看集群情况
[root@master2 emqx-mqtt]# kubectl exec -it emqx-statefulset-0 -ndb -- emqx_ctl cluster status
Cluster status: #{running_nodes =>
                      ['emqx@emqx-statefulset-0.emqx-headless.db.svc.cluster.local',
                       'emqx@emqx-statefulset-1.emqx-headless.db.svc.cluster.local',
                       'emqx@emqx-statefulset-2.emqx-headless.db.svc.cluster.local'],
                  stopped_nodes => []}
[root@master2 emqx-mqtt]#

其中镜像可以到docker官方镜像仓库去下载,搜索emqx 就能找到,这边存储用的是ceph的rbd

  1. 后面为了提供外部访问于是再创建了一个NodePort 的headless-net.yaml

kubectl apply -f headless-net.yaml

apiVersion: v1
kind: Service
metadata:
    name: emqx-headless-net
    namespace: db
spec:
    type: NodePort
    selector:
      app: emqx
    ports:
    - name: mqtt
      port: 1883
      protocol: TCP
      targetPort: 1883
    - name: mqttssl
      port: 8883
      protocol: TCP
      targetPort: 8883
    - name: mgmt
      port: 8081
      protocol: TCP
      targetPort: 8081
    - name: websocket
      port: 8083
      protocol: TCP
      targetPort: 8083
    - name: wss
      port: 8084
      protocol: TCP
      targetPort: 8084
    - name: dashboard
      port: 18083
      protocol: TCP
      targetPort: 18083

  1. 查看是否创建完成
    emqx

  2. 登陆界面 18083对应的 NodePort 30694
    http://10.8.154.206:30694/

默认用户/密码:admin/public
用户/密码:admin/public

登陆后界面

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐