K8S部署RocketMQ

Published on with 17 views

K8S 部署 RocketMQ

制作镜像

基于官方镜像制作的改造版本

部署 Broker

创建 StorageClass

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: rocketmq-broker-server
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

创建 2 个 PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: rocketmq-broker-server-0
  namespace: rocketmq
  labels:
    name: rocketmq-broker-server-0
spec:
  capacity:
      storage: 2Gi
  accessModes:
      - ReadWriteOnce
  storageClassName: "rocketmq-broker-server"
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /mnt/rocketmq-broker-pv0
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: rocketmq-broker-server-1
  namespace: rocketmq
  labels:
    name: rocketmq-broker-server-1
spec:
  capacity:
      storage: 2Gi
  accessModes:
      - ReadWriteOnce
  storageClassName: "rocketmq-broker-server"
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /mnt/rocketmq-broker-pv1
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

创建 2 个 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rocketmq-broker-server-rocketmq-broker-server-0
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: rocketmq-broker-server
  selector:
    matchLabels:
      name: rocketmq-broker-server-0
  resources:
    requests:
      storage: 2Gi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rocketmq-broker-server-rocketmq-broker-server-1
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: rocketmq-broker-server
  selector:
    matchLabels:
      name: rocketmq-broker-server-1
  resources:
    requests:
      storage: 2Gi

创建 ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: rocketmq-broker-configmap
  namespace: rocketmq
  labels:
    name: rocketmq-broker-configmap
data:
  broker.conf: >
    brokerClusterName=rocketmq-cluster

    deleteWhen=04

    fileReservedTime=48

    flushDiskType=ASYNC_FLUSH

    namesrvAddr=rocketmq-namesrv-server-0.rocketmq-namesrv-server.rocketmq.svc.cluster.local:9876

    storePathRootDir=/home/rocketmq/store/data

    storePathCommitLog=/home/rocketmq/store/data/commitlog

    storePathConsumeQueue=/home/rocketmq/store/data/consumequeue

    storePathIndex=/home/rocketmq/store/data/index

    storeCheckpoint=/home/rocketmq/store/data/checkpoint

    abortFile=/home/rocketmq/store/data/abort

    slaveReadEnable=true
  rewrite-broker-config.sh: >
    #!/bin/sh

    cp /etc/rocketmq-4.7.0/broker.conf /home/rocketmq/rocketmq-4.7.0/conf

    BROKER_CONFIG="/home/rocketmq/rocketmq-4.7.0/conf/broker.conf"

    if [ -f ${BROKER_CONFIG} ]; then
      index=${HOSTNAME//rocketmq-broker-server-/}
      flag=$((index%2))
      if [ ${flag} == 0 ]; then
        if [ `grep -c "brokerName=broker-" /home/rocketmq/rocketmq-4.7.0/conf/broker.conf` -eq '0' ]; then
          echo brokerName=broker-${index} >> ${BROKER_CONFIG}
        fi
        if [ `grep -c "brokerId=" /home/rocketmq/rocketmq-4.7.0/conf/broker.conf` -eq '0' ]; then
          echo brokerId=${flag} >> ${BROKER_CONFIG}
        fi
        if [ `grep -c "brokerRole=ASYNC_MASTER" /home/rocketmq/rocketmq-4.7.0/conf/broker.conf` -eq '0' ]; then
          echo brokerRole=ASYNC_MASTER >> ${BROKER_CONFIG}
        fi
      else
        if [ `grep -c "brokerName=broker-" /home/rocketmq/rocketmq-4.7.0/conf/broker.conf` -eq '0' ]; then
          echo brokerName=broker-${index} >> ${BROKER_CONFIG}
        fi
        if [ `grep -c "brokerId=" /etc/rocketmq-4.7.0/broker.conf` -eq '0' ]; then
          echo brokerId=${flag} >> ${BROKER_CONFIG}
        fi
        if [ `grep -c "brokerRole=SLAVE" /etc/rocketmq-4.7.0/broker.conf` -eq '0' ]; then
          echo brokerRole=SLAVE >> ${BROKER_CONFIG}
        fi
      fi
    fi

    sh /home/rocketmq/rocketmq-4.7.0/bin/mqbroker -c /home/rocketmq/rocketmq-4.7.0/conf/broker.conf

创建 Broker 的 StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rocketmq-broker-server
  namespace: rocketmq
  labels:
    app: rocketmq-broker-server
spec:
  selector:
    matchLabels:
      app: rocketmq-broker-server
  serviceName: rocketmq-broker-server
  replicas: 2
  template:
    metadata:
      labels:
        app: rocketmq-broker-server
    spec:
      volumes:
        - name: rocketmq-broker-conf
          configMap:
            name: rocketmq-broker-configmap
            defaultMode: 511
            items:
              - key: broker.conf
                path: broker.conf
        - name: rocketmq-broker-rewrite-config
          configMap:
            name: rocketmq-broker-configmap
            defaultMode: 511
            items:
              - key: rewrite-broker-config.sh
                path: rewrite-broker-config.sh
      containers:
        - name: rocketmq-broker-server
          image: 'registry.cn-beijing.aliyuncs.com/jenphyjohn/rocketmq:latest'
          command:
            - sh
            - /etc/rocketmq-4.7.0/bin/rewrite-broker-config.sh
          imagePullPolicy: Always
          ports:
            - containerPort: 10909
            - containerPort: 10911
            - containerPort: 10912
          volumeMounts:
            - name: rocketmq-broker-server
              mountPath: /home/rocketmq/store
              subPath: store
            - name: rocketmq-broker-server
              mountPath: /home/rocketmq/logs
              subPath: logs
            - name: rocketmq-broker-conf
              mountPath: /etc/rocketmq-4.7.0/broker.conf
              subPath: broker.conf
              readOnly: false
            - name: rocketmq-broker-rewrite-config
              mountPath: /etc/rocketmq-4.7.0/bin/rewrite-broker-config.sh
              subPath: rewrite-broker-config.sh
              readOnly: false
          resources:
            limits:
              cpu: '1'
              memory: 1Gi
            requests:
              cpu: '1'
              memory: 1Gi
          env:
            - name: JAVA_OPT_EXT
              value: "-server -Xms512M -Xmx512M -Xmn256m"
            - name: rocketmq-broker-configmap
              value: rocketmq-broker-configmap
          envFrom:
            - configMapRef:
                name: rocketmq-broker-configmap
  volumeClaimTemplates:
    - metadata:
        name: rocketmq-broker-server
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: rocketmq-broker-server
        resources:
          requests:
            storage: 2Gi
  updateStrategy:
    type: RollingUpdate

部署 NameServer

创建 StorageClass

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: rocketmq-namesrv-server
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

创建 PersistentVolume 和 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolume
metadata:
  name: rocketmq-namesrv-server-0
  namespace: rocketmq
  labels:
    name: rocketmq-namesrv-server-0
spec:
  capacity:
      storage: 2Gi
  accessModes:
      - ReadWriteOnce
  storageClassName: "rocketmq-namesrv-server"
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /mnt/rocketmq-namesrv-pv0
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rocketmq-namesrv-server-rocketmq-namesrv-server-0
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: rocketmq-namesrv-server
  selector:
    matchLabels:
      name: rocketmq-namesrv-server-0
  resources:
    requests:
      storage: 2Gi

创建 Service

apiVersion: v1
kind: Service
metadata:
  name: rocketmq-namesrv-server
  namespace: rocketmq
  labels:
    app: rocketmq-namesrv-server
spec:
  selector:
    app: rocketmq-namesrv-server
  ports:
    - protocol: TCP
      port: 9876
      targetPort: 9876
      name: rocketmq-namesrv-server-9876
  type: ClusterIP
  clusterIP: None
  sessionAffinity: ClientIP

创建 StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rocketmq-namesrv-server
  namespace: rocketmq
  labels:
    app: rocketmq-namesrv-server
spec:
  serviceName: rocketmq-namesrv-server
  replicas: 1
  selector:
    matchLabels:
      app: rocketmq-namesrv-server
  template:
    metadata:
      labels:
        app: rocketmq-namesrv-server
    spec:
      containers:
        - name: rocketmq-nameserver
          image: 'registry.cn-beijing.aliyuncs.com/jenphyjohn/rocketmq:latest'
          command:
            - sh
            - mqnamesrv
          imagePullPolicy: Always
          ports:
            - containerPort: 9876
          volumeMounts:
            - mountPath: /home/rocketmq/logs
              name: rocketmq-namesrv-server
          resources:
            limits:
              cpu: '1'
              memory: 512Mi
            requests:
              cpu: '1'
              memory: 512Mi
  volumeClaimTemplates:
    - metadata:
        name: rocketmq-namesrv-server
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: rocketmq-namesrv-server
        resources:
          requests:
            storage: 2Gi
  updateStrategy:
    type: RollingUpdate

部署 Console

创建 StorageClass

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: rocketmq-console
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

创建 PersistentVolume 和 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolume
metadata:
  name: rocketmq-console-0
  namespace: rocketmq
  labels:
    name: rocketmq-console-0
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  storageClassName: "rocketmq-console"
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /mnt/rocketmq-console-pv0
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rocketmq-console-rocketmq-console-0
spec:
  accessModes:
  - ReadWriteMany
  storageClassName: rocketmq-console
  selector:
    matchLabels:
      name: rocketmq-console-0
  resources:
    requests:
      storage: 2Gi

创建 HeadlessService

apiVersion: v1
kind: Service
metadata:
  name: rocketmq-console
  namespace: rocketmq
  labels:
    app: rocketmq-console
spec:
  selector:
    app: rocketmq-console
  ports:
    - port: 8080
      targetPort: 8080
      name: rocketmq-console-8080
  type: ClusterIP
  clusterIP: None
  sessionAffinity: ClientIP

创建 StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rocketmq-console
  namespace: rocketmq
  labels:
    app: rocketmq-console
spec:
  serviceName: rocketmq-console
  replicas: 1
  selector:
    matchLabels:
      app: rocketmq-console
  template:
    metadata:
      labels:
        app: rocketmq-console
    spec:
      containers:
        - name: rocketmq-console
          image: 'styletang/rocketmq-console-ng:1.0.0'
          imagePullPolicy: Always
          resources:
            #生产用配置
            limits:
              cpu: '1'
              memory: 512Mi
            requests:
              cpu: '0.1'
              memory: 512Mi
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: JAVA_OPTS
              value: >-
                -Drocketmq.namesrv.addr=rocketmq-namesrv-server-0.rocketmq-namesrv-server.rocketmq.svc.cluster.local:9876
                -Dcom.rocketmq.sendMessageWithVIPChannel=false
                -Duser.home=/root  -Xms512m -Xmx512m
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: rocketmq-console
              mountPath: /root/logs
  volumeClaimTemplates:
  - metadata:
      name: rocketmq-console
    spec:
      accessModes: [ "ReadWriteMany" ]
      storageClassName: "rocketmq-console"
      resources:
        requests:
          storage: 2Gi
  updateStrategy:
    type: RollingUpdate

创建 Service,暴露服务

apiVersion: v1
kind: Service
metadata:
  name: rocketmq-console-nodeport
  namespace: rocketmq
  labels:
    app: rocketmq-console
spec:
  type: NodePort
  selector:
    app: rocketmq-console
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30903

标题:K8S部署RocketMQ
作者:jenphyjohn
地址:http://blog.join-e.tech/articles/2020/04/20/1587379451251.html

Responses