K8S安装rabbitmq-operator并配置RabbitMQ集群

K8S安装rabbitmq-operator并配置RabbitMQ集群

1.cluster-operator安装

1.1 下载脚本并安装

  1. 下载并部署Operator,默认部署在rabbitmq-system命名空间,如果需要修改命名空间可以先下载yaml文件,修改命名空间后再执行部署命令。文件已下载到本目录cluster-operator.yaml
  2. 执行安装命令kubectl apply -f cluster-operator.yaml

1.2 确认服务可用性

在将您的应用程序配置为使用 RabbitMQ Cluster Kubernetes Operator 之前,请确保 RabbitmqCluster 自定义资源已部署到您的 Kubernetes 集群并且可用。

  1. 确认服务可用性,运行kubectl get customresourcedefinitions.apiextensions.k8s.io

  2. 然后验证rabbitmqclusters.rabbitmq.com是否在列表中,如下例所示:

    root# kubectl get customresourcedefinitions.apiextensions.k8s.io
    NAME                                                     CREATED AT
    catalogsources.operators.coreos.com                      2021-07-31T02:46:43Z
    clusterserviceversions.operators.coreos.com              2021-07-31T02:46:43Z
    horizontalpodcronscalers.autoscaling.cloud.tencent.com   2021-07-31T02:46:43Z
    imagepullsecrets.tcr.tencentcloudcr.com                  2021-07-31T02:50:42Z
    installplans.operators.coreos.com                        2021-07-31T02:46:43Z
    logconfigs.cls.cloud.tencent.com                         2021-07-31T02:46:42Z
    nginxingresses.cloud.tencent.com                         2021-07-31T03:44:21Z
    operatorgroups.operators.coreos.com                      2021-07-31T02:46:43Z
    operators.operators.coreos.com                           2021-07-31T02:46:43Z
    rabbitmqclusters.rabbitmq.com                            2021-08-12T04:52:34Z
    subscriptions.operators.coreos.com                       2021-07-31T02:46:43Z
    tkeserviceconfigs.cloud.tencent.com                      2021-07-31T02:46:03Z
    volumesnapshotclasses.snapshot.storage.k8s.io            2021-07-31T02:46:43Z
    volumesnapshotcontents.snapshot.storage.k8s.io           2021-07-31T02:46:43Z
    volumesnapshots.snapshot.storage.k8s.io                  2021-07-31T02:46:43Z
  3. 如果不在,说明安装失败,请参考上面安装步骤重新安装。

2.常用命令

2.1 获取已安装的实例

kubectl get rabbitmqcluster -A

2.2 获取已安装实例的配置

kubectl get rabbitmqcluster [实例名称] -n [命名空间] -o yaml

2.3 编辑实例配置

kubectl edit rabbitmqcluster [实例名称]

2.4 删除RabbitMQ实例

kubectl delete rabbitmqcluster [实例名称]

3.创建RabbitMQ环境实例

3.1 实例配置配置文件definition.yaml

apiVersion: rabbitmq.com/v1beta1
kind: RabbitmqCluster
metadata:
  # RabbitMQ实例名称
  name: demo-rabbitmq-cluster
  # 应用所在命名空间
  namespace: middleware
spec:
  persistence:
    # 数据持久化storageClass
    storageClassName: hssd
    # 数据卷大小
    storage: 100Gi
  # 实例pod数量
  replicas: 3
  override:
    statefulSet:
      spec:
        template:
          spec:
            containers:
              - name: rabbitmq
                volumeMounts:
                  - mountPath: /opt/rabbitmq/community-plugins
                    name: community-plugins
            volumes:
              - name: community-plugins
                emptyDir: {}
            initContainers:
              - command:
                  - sh
                  - -c
                  - curl -L -v https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/3.8.17/rabbitmq_delayed_message_exchange-3.8.17.8f537ac.ez --output /community-plugins/rabbitmq_delayed_message_exchange-3.8.17.8f537ac.ez
                image: curlimages/curl
                imagePullPolicy: IfNotPresent
                name: copy-community-plugins
                resources:
                  limits:
                    cpu: 100m
                    memory: 500Mi
                  requests:
                    cpu: 100m
                    memory: 500Mi
                terminationMessagePolicy: FallbackToLogsOnError
                volumeMounts:
                  - mountPath: /community-plugins/
                    name: community-plugins
  service:
    # 服务类型,可选参数:ClusterIP,NodePortLoadBalancer
    type: ClusterIP
  # 资源限制
  resources:
    requests:
      cpu: 2000m
      memory: 4Gi
    limits:
      cpu: 2000m
      memory: 4Gi
  rabbitmq:
    # 额外增加的插件
    additionalPlugins:
      - rabbitmq_federation
      - rabbitmq_federation_management
      - rabbitmq_peer_discovery_k8s
      - rabbitmq_prometheus
      - rabbitmq_management
      - rabbitmq_tracing
      - rabbitmq_delayed_message_exchange
    envConfig: |
      PLUGINS_DIR=/opt/rabbitmq/plugins:/opt/rabbitmq/community-plugins

3.2 安装

kubectl apply -f definition.yaml

3.3 获取默认用户

查看pod容器内文件/etc/rabbitmq/conf.d/11-default_user.conf

rabbitmq@prod-rabbitmq-cluster-server-0:/$ cat /etc/rabbitmq/conf.d/11-default_user.conf
default_user = nSM8kXCRuUzKIwWcgtgtedyc0qfGGl0e-
default_pass = KJEVJhwaP2WDFsrgSNEannzlH-3NdjN
rabbitmq@prod-rabbitmq-cluster-server-0:/$

4.创建RabbitMQ环境实例(安装社区插件)

  • 示例配置,以延迟消息插件rabbitmq_delayed_message_exchange为例

    apiVersion: rabbitmq.com/v1beta1
    kind: RabbitmqCluster
    metadata:
      # RabbitMQ实例名称
      name: demo-rabbitmq-cluster
      # 应用所在命名空间
      namespace: middleware
    spec:
      override:
        statefulSet:
          spec:
            template:
              spec:
                containers:
                  - name: rabbitmq
                    volumeMounts:
                      - mountPath: /opt/rabbitmq/community-plugins
                        name: community-plugins
                volumes:
                  - name: community-plugins
                    emptyDir: {}
                initContainers:
                  - command:
                      - sh
                      - -c
                      - curl -L -v https://devops-1301682764.cos.ap-guangzhou.myqcloud.com/RabbitMQ/plugins/rabbitmq_delayed_message_exchange-3.8.17.8f537ac.ez --output /community-plugins/rrabbitmq_delayed_message_exchange-3.8.17.ez
                    image: curlimages/curl
                    imagePullPolicy: IfNotPresent
                    name: copy-community-plugins
                    resources:
                      limits:
                        cpu: 100m
                        memory: 500Mi
                      requests:
                        cpu: 100m
                        memory: 500Mi
                    terminationMessagePolicy: FallbackToLogsOnError
                    volumeMounts:
                      - mountPath: /community-plugins/
                        name: community-plugins
      persistence:
        # 数据持久化storageClass
        storageClassName: hssd
        # 数据卷大小
        storage: 100Gi
      # 实例pod数量
      replicas: 3
      service:
        # 服务类型,可选参数:ClusterIP,NodePortLoadBalancer
        type: ClusterIP
      # 资源限制
      resources:
        requests:
          cpu: 2000m
          memory: 4Gi
        limits:
          cpu: 2000m
          memory: 4Gi
      rabbitmq:
        # 额外增加的插件
        additionalPlugins:
          - rabbitmq_federation
          - rabbitmq_federation_management
          - rabbitmq_peer_discovery_k8s
          - rabbitmq_prometheus
          - rabbitmq_management
          - rabbitmq_tracing
          - rabbitmq_delayed_message_exchange
        envConfig: |
          PLUGINS_DIR=/opt/rabbitmq/plugins:/opt/rabbitmq/community-plugins

K8S安装rabbitmq-operator并配置RabbitMQ集群
https://www.gmtgo.com/86.html
作者
大帅
发布于
2021年8月12日
许可协议