kubernetes搭建RabbitMQ集群(基础版)


版本说明

k8s:v1.19.2
RabbitMQ 3.8.9 on Erlang 23.1.5


一、RabbitMQ简介

RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。AMQP:Advanced Message Queue,高级消息队列协议。它是应用层协议的一个开放标准,为面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产品、开发语言灯条件的限制。AMQP具有如下的特性:

  • 可靠性(Reliablity): 使用了一些机制来保证可靠性,比如持久化、传输确认、发布确认。

  • 灵活的路由(Flexible Routing): 在消息进入队列之前,通过Exchange来路由消息。对于典型的路由功能,Rabbit已经提供了一些内置的Exchange来实现。针对更复杂的路由功能,可以将多个Exchange绑定在一起,也通过插件机制实现自己的Exchange。

  • 消息集群(Clustering): 多个RabbitMQ服务器可以组成一个集群,形成一个逻辑Broker。

  • 高可用(Highly Avaliable Queues): 队列可以在集群中的机器上进行镜像,使得在部分节点出问题的情况下队列仍然可用。

  • 多种协议(Multi-protocol): 支持多种消息队列协议,如STOMP、MQTT等。

  • 多种语言客户端(Many Clients): 几乎支持所有常用语言,比如Java、.NET、Ruby等。

  • 管理界面(Management UI): 提供了易用的用户界面,使得用户可以监控和管理消息Broker的许多方面。

  • 跟踪机制(Tracing): 如果消息异常,RabbitMQ提供了消息的跟踪机制,使用者可以找出发生了什么。

  • 插件机制(Plugin System): 提供了许多插件,来从多方面进行扩展,也可以编辑自己的插件。

二、RabbitMQ集群部署

提示:下面的例示代码,要测试的话,要稍作修改

apiVersion: v1
kind: ConfigMap
metadata:
  name: rabbitmq-config
  namespace: public   ##可选,自身添加
data:
  enabled_plugins: |
      [rabbitmq_management,rabbitmq_peer_discovery_k8s].
  rabbitmq.conf: |
      ## Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to learn more.
      cluster_formation.peer_discovery_backend  = rabbit_peer_discovery_k8s
      cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
      ## Should RabbitMQ node name be computed from the pod's hostname or IP address?
      ## IP addresses are not stable, so using [stable] hostnames is recommended when possible.
      ## Set to "hostname" to use pod hostnames.
      ## When this value is changed, so should the variable used to set the RABBITMQ_NODENAME
      ## environment variable.
      log.file.level = debug
      log.console = true
      log.console.level = debug
      cluster_formation.k8s.address_type = hostname
      ## How often should node cleanup checks run?
      cluster_formation.node_cleanup.interval = 30
      ## Set to false if automatic removal of unknown/absent nodes
      ## is desired. This can be dangerous, see
      ##  * https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup
      ##  * https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ
      cluster_formation.node_cleanup.only_log_warning = true
      cluster_partition_handling = autoheal
      ## See https://www.rabbitmq.com/ha.html#master-migration-data-locality
      queue_master_locator=min-masters
      ## This is just an example.
      ## This enables remote access for the default user with well known credentials.
      ## Consider deleting the default user and creating a separate user with a set of generated
      ## credentials instead.
      ## Learn more at https://www.rabbitmq.com/access-control.html#loopback-users
      loopback_users.guest = false
---
apiVersion: apps/v1
# See the Prerequisites section of https://www.rabbitmq.com/cluster-formation.html#peer-discovery-k8s.
kind: StatefulSet
metadata:
  name: rabbitmq
  namespace: public   ##可选,自身添加
spec:
  serviceName: rabbitmq
  # Three nodes is the recommended minimum. Some features may require a majority of nodes
  # to be available.
  replicas: 3
  volumeClaimTemplates: []
  selector:
    matchLabels:
      app: rabbitmq
  template:
    metadata:
      labels:
        app: rabbitmq  #源码是没有这个,但会报错
    spec:
    #  serviceAccountName: rabbitmq
      terminationGracePeriodSeconds: 10
      nodeSelector:
        # Use Linux nodes in a mixed OS kubernetes cluster.
        # Learn more at https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-os
        kubernetes.io/os: linux
      containers:
      - name: rabbitmq-k8s
        image: rabbitmq:3.8
        volumeMounts:
          - name: config-volume
            mountPath: /etc/rabbitmq
        # Learn more about what ports various protocols use
        # at https://www.rabbitmq.com/networking.html#ports
        ports:
          - name: http
            protocol: TCP
            containerPort: 15672
          - name: amqp
            protocol: TCP
            containerPort: 5672
        livenessProbe:
          exec:
            # This is just an example. There is no "one true health check" but rather
            # several rabbitmq-diagnostics commands that can be combined to form increasingly comprehensive
            # and intrusive health checks.
            # Learn more at https://www.rabbitmq.com/monitoring.html#health-checks.
            #
            # Stage 2 check:
            command: ["rabbitmq-diagnostics", "status"]
          initialDelaySeconds: 60
          # See https://www.rabbitmq.com/monitoring.html for monitoring frequency recommendations.
          periodSeconds: 60
          timeoutSeconds: 15
        readinessProbe:
          exec:
            # This is just an example. There is no "one true health check" but rather
            # several rabbitmq-diagnostics commands that can be combined to form increasingly comprehensive
            # and intrusive health checks.
            # Learn more at https://www.rabbitmq.com/monitoring.html#health-checks.
            #
            # Stage 1 check:
            command: ["rabbitmq-diagnostics", "ping"]
          initialDelaySeconds: 20
          periodSeconds: 60
          timeoutSeconds: 10
		  # IfNotPresent,Always
        imagePullPolicy: IfNotPresent #默认Always
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          # See a note on cluster_formation.k8s.address_type in the config file section
          - name: K8S_SERVICE_NAME
            value: rabbitmq
          - name: RABBITMQ_NODENAME
            value: rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
          - name: K8S_HOSTNAME_SUFFIX
            value: .$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
          - name: RABBITMQ_ERLANG_COOKIE
            value: "mycookie"  #生成一个erlang.cookie的文件:echo $(openssl rand -base64 32) > erlang.cookie,复制内容覆盖"mycookie"
      volumes:
        - name: config-volume
          configMap:
            name: rabbitmq-config
            items:
            - key: rabbitmq.conf
              path: rabbitmq.conf
            - key: enabled_plugins
              path: enabled_plugins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rabbitmq-peer-discovery-rbac
  namespace: public   ##可选,自身添加
rules:
- apiGroups: 
    - ""
  resources: 
    - endpoints
  verbs: 
    - get
    - list
    - watch
- apiGroups:
    - ""
  resources:
    - events
  verbs:
    - create
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rabbitmq-peer-discovery-rbac
  namespace: public   ##可选,自身添加
subjects:
- kind: ServiceAccount
  name: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: rabbitmq-peer-discovery-rbac
---
kind: Service
apiVersion: v1
metadata:
  name: rabbitmq
  namespace: public   ##可选,自身添加
spec:
  type: NodePort
  ports:
   - name: http
     protocol: TCP
     port: 15672
     targetPort: 15672
     nodePort: 31672
   - name: amqp
     protocol: TCP
     port: 5672
     targetPort: 5672
     nodePort: 30672
  selector:
    app: rabbitmq

三、部署验证

在浏览器中输入:http://IP:31672/,访问部署好的RabbitMQ。在登录页面输入用户名和密码(此处初始guest/guest),系统将会进入RabbitMQ的主页。
在这里插入图片描述

四、官方源码

源码地址

#configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: rabbitmq-config
data:
  enabled_plugins: |
      [rabbitmq_management,rabbitmq_peer_discovery_k8s].
  rabbitmq.conf: |
      ## Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to learn more.
      cluster_formation.peer_discovery_backend  = rabbit_peer_discovery_k8s
      cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
      ## Should RabbitMQ node name be computed from the pod's hostname or IP address?
      ## IP addresses are not stable, so using [stable] hostnames is recommended when possible.
      ## Set to "hostname" to use pod hostnames.
      ## When this value is changed, so should the variable used to set the RABBITMQ_NODENAME
      ## environment variable.
      log.file.level = debug
      log.console = true
      log.console.level = debug
      cluster_formation.k8s.address_type = hostname
      ## How often should node cleanup checks run?
      cluster_formation.node_cleanup.interval = 30
      ## Set to false if automatic removal of unknown/absent nodes
      ## is desired. This can be dangerous, see
      ##  * https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup
      ##  * https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ
      cluster_formation.node_cleanup.only_log_warning = true
      cluster_partition_handling = autoheal
      ## See https://www.rabbitmq.com/ha.html#master-migration-data-locality
      queue_master_locator=min-masters
      ## This is just an example.
      ## This enables remote access for the default user with well known credentials.
      ## Consider deleting the default user and creating a separate user with a set of generated
      ## credentials instead.
      ## Learn more at https://www.rabbitmq.com/access-control.html#loopback-users
      loopback_users.guest = false
#deployment.yaml
apiVersion: apps/v1
# See the Prerequisites section of https://www.rabbitmq.com/cluster-formation.html#peer-discovery-k8s.
kind: StatefulSet
metadata:
  name: rabbitmq
spec:
  serviceName: rabbitmq
  # Three nodes is the recommended minimum. Some features may require a majority of nodes
  # to be available.
  replicas: 3
  volumeClaimTemplates: []
  selector:
    matchLabels:
      app: rabbitmq
  template:
    spec:
    #  serviceAccountName: rabbitmq
      terminationGracePeriodSeconds: 10
      nodeSelector:
        # Use Linux nodes in a mixed OS kubernetes cluster.
        # Learn more at https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-os
        kubernetes.io/os: linux
      containers:
      - name: rabbitmq-k8s
        image: rabbitmq:3.8
        volumeMounts:
          - name: config-volume
            mountPath: /etc/rabbitmq
        # Learn more about what ports various protocols use
        # at https://www.rabbitmq.com/networking.html#ports
        ports:
          - name: http
            protocol: TCP
            containerPort: 15672
          - name: amqp
            protocol: TCP
            containerPort: 5672
        livenessProbe:
          exec:
            # This is just an example. There is no "one true health check" but rather
            # several rabbitmq-diagnostics commands that can be combined to form increasingly comprehensive
            # and intrusive health checks.
            # Learn more at https://www.rabbitmq.com/monitoring.html#health-checks.
            #
            # Stage 2 check:
            command: ["rabbitmq-diagnostics", "status"]
          initialDelaySeconds: 60
          # See https://www.rabbitmq.com/monitoring.html for monitoring frequency recommendations.
          periodSeconds: 60
          timeoutSeconds: 15
        readinessProbe:
          exec:
            # This is just an example. There is no "one true health check" but rather
            # several rabbitmq-diagnostics commands that can be combined to form increasingly comprehensive
            # and intrusive health checks.
            # Learn more at https://www.rabbitmq.com/monitoring.html#health-checks.
            #
            # Stage 1 check:
            command: ["rabbitmq-diagnostics", "ping"]
          initialDelaySeconds: 20
          periodSeconds: 60
          timeoutSeconds: 10
        imagePullPolicy: Always
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          # See a note on cluster_formation.k8s.address_type in the config file section
          - name: K8S_SERVICE_NAME
            value: rabbitmq
          - name: RABBITMQ_NODENAME
            value: rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
          - name: K8S_HOSTNAME_SUFFIX
            value: .$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
          - name: RABBITMQ_ERLANG_COOKIE
            value: "mycookie"
      volumes:
        - name: config-volume
          configMap:
            name: rabbitmq-config
            items:
            - key: rabbitmq.conf
              path: rabbitmq.conf
            - key: enabled_plugins
              path: enabled_plugins
#deployment_rbac.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rabbitmq-peer-discovery-rbac
 
rules:
- apiGroups: 
    - ""
  resources: 
    - endpoints
  verbs: 
    - get
    - list
    - watch
- apiGroups:
    - ""
  resources:
    - events
  verbs:
    - create
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rabbitmq-peer-discovery-rbac
subjects:
- kind: ServiceAccount
  name: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: rabbitmq-peer-discovery-rbac
#service.yaml
kind: Service
apiVersion: v1
metadata:
  name: rabbitmq
spec:
  type: NodePort
  ports:
   - name: http
     protocol: TCP
     port: 15672
     targetPort: 15672
     nodePort: 31672
   - name: amqp
     protocol: TCP
     port: 5672
     targetPort: 5672
     nodePort: 30672
  selector:
    app: rabbitmq
#kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: rabbitmq

resources:
- configMap.yaml
- deployment.yaml
- deployment_rbac.yaml
- service.yaml
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用 Docker 搭建 RabbitMQ 集群的步骤: 1. 首先,确保已经安装了 Docker 和 Docker Compose。 2. 在本地创建一个新的文件夹,用于存储 RabbitMQ 的配置文件和数据。 3. 在该文件夹中创建一个名为 docker-compose.yml 的文件,并将以下内容复制到该文件中: ``` version: '3' services: rabbitmq-node1: image: rabbitmq:3.8-management-alpine hostname: rabbitmq-node1 environment: RABBITMQ_ERLANG_COOKIE: "secret_cookie" RABBITMQ_DEFAULT_USER: "admin" RABBITMQ_DEFAULT_PASS: "admin" ports: - "5672:5672" - "15672:15672" volumes: - ./data/node1:/var/lib/rabbitmq - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf networks: rabbitmq-network: aliases: - rabbitmq-node1 rabbitmq-node2: image: rabbitmq:3.8-management-alpine hostname: rabbitmq-node2 environment: RABBITMQ_ERLANG_COOKIE: "secret_cookie" RABBITMQ_DEFAULT_USER: "admin" RABBITMQ_DEFAULT_PASS: "admin" volumes: - ./data/node2:/var/lib/rabbitmq - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf networks: rabbitmq-network: aliases: - rabbitmq-node2 rabbitmq-node3: image: rabbitmq:3.8-management-alpine hostname: rabbitmq-node3 environment: RABBITMQ_ERLANG_COOKIE: "secret_cookie" RABBITMQ_DEFAULT_USER: "admin" RABBITMQ_DEFAULT_PASS: "admin" volumes: - ./data/node3:/var/lib/rabbitmq - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf networks: rabbitmq-network: aliases: - rabbitmq-node3 networks: rabbitmq-network: driver: bridge ``` 该文件定义了一个 RabbitMQ 集群,由三个节点组成。每个节点都有一个名称、一个主机名、一个环境变量(Erlang cookie、默认用户名和密码)、端口映射、数据卷和网络别名。该文件还定义了一个网络,用于连接三个节点。 4. 在该文件夹中创建一个名为 config 的文件夹,并在其中创建一个名为 rabbitmq.conf 的文件。将以下内容复制到该文件中: ``` cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config cluster_formation.classic_config.nodes.1 = rabbit@rabbitmq-node1 cluster_formation.classic_config.nodes.2 = rabbit@rabbitmq-node2 cluster_formation.classic_config.nodes.3 = rabbit@rabbitmq-node3 ``` 该文件定义了 RabbitMQ 集群的配置,包括节点发现方式和节点列表。 5. 在该文件夹中创建一个名为 data 的文件夹,用于存储 RabbitMQ 节点的数据。 6. 在终端中导航到该文件夹,并运行以下命令: ``` 回复:搭建rabbitmq集群需要使用Docker,可以参考Docker官方文档:https://docs.docker.com/engine/examples/rabbitmq/。好的,以下是如何使用Docker搭建RabbitMQ集群的步骤: 1. 首先,确保您已经在本地安装了Docker,并且已经在命令行中登录到了Docker。 2. 在命令行中输入以下命令,以在Docker中下载和运行RabbitMQ镜像: ``` docker run -d --name rabbitmq-node1 -p 5672:5672 -p 15672:15672 rabbitmq:3.8-management ``` 这将下载最新本的RabbitMQ,并将其运行在Docker容器中。这里我们指定了两个端口,一个用于AMQP通信(5672),另一个用于Web管理界面(15672)。 3. 等待RabbitMQ容器启动后,您可以使用以下命令在另一个容器中运行第二个RabbitMQ节点: ``` docker run -d --name rabbitmq-node2 -p 5673:5672 -p 15673:15672 --link rabbitmq-node1:rabbit1 rabbitmq:3.8-management ``` 这将启动第二个RabbitMQ容器,并将其链接到第一个容器,以便它们可以互相通信。请注意,我们使用了不同的端口(5673和15673),以便避免与第一个节点的端口冲突。 4. 接下来,您可以使用以下命令启动第三个节点: ``` docker run -d --name rabbitmq-node3 -p 5674:5672 -p 15674:15672 --link rabbitmq-node1:rabbit1 --link rabbitmq-node2:rabbit2 rabbitmq:3.8-management ``` 同样,我们指定了不同的端口,并将此容器链接到前两个容器,以创建一个RabbitMQ集群。 5. 等待第三个节点启动后,您可以使用以下命令检查集群状态: ``` docker exec -it rabbitmq-node1 rabbitmqctl cluster_status ``` 这将显示集群中所有节点的状态。 恭喜!您已经成功地在Docker中部署了一个RabbitMQ集群

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值