k8s部署rabbitmq集群

一、前言

本次部署方式为setafulset的方式部署rabbitmq集群,使用svc的无头服务,本次涉及使用到的服务有setafulset、secret、configmap、service、pv、pvc、sa,在rabbitmq的官网中使用的是operator的方式

operator方式参考:RabbitMQ Cluster Kubernetes Operator Quickstart | RabbitMQ

二、部署

创建rabbitmq yaml文件存放目录

mkdir /opt/rabbitmq && cd /opt/rabbitmq

编辑secret配置

vi secret.yaml

bash 复制代码
kind: Secret
apiVersion: v1
metadata:
  name: rabbitmq
  namespace: rabbitmq
type: Opaque
stringData:
  rabbitDefaulUser: admin       #设置rabbitmq集群的用户名
  rabbitDefaultPass: admin      #设置rabbitmq集群的密码
  erlang.cookie: ERLANG_COOKIE    #集群需要用到的cookie值,每个节点都需要保持一致

编辑configmap配置

vi configmap.yaml

bash 复制代码
kind: ConfigMap
apiVersion: v1
metadata:
  name: rabbitmq-config
  namespace: rabbitmq
data:
   enabled_plugins: |
      [rabbitmq_management,rabbitmq_peer_discovery_k8s].
   rabbitmq.conf: |
      cluster_formation.peer_discovery_backend  = rabbit_peer_discovery_k8s
      cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
      cluster_formation.k8s.address_type = hostname
      cluster_formation.k8s.hostname_suffix = .rabbitmq-headless.rabbitmq.svc.cluster.local
      cluster_formation.node_cleanup.interval = 30
      cluster_formation.node_cleanup.only_log_warning = true
      cluster_partition_handling = autoheal
      queue_master_locator=min-masters
      cluster_formation.randomized_startup_delay_range.max = 2
      vm_memory_high_watermark.absolute = 1GB
      disk_free_limit.absolute = 2GB
      loopback_users.guest = false

编辑rbac配置

vi rbac.yaml

bash 复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: rabbitmq
  namespace: rabbitmq
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: endpoint-reader
  namespace: rabbitmq
rules:
- apiGroups: [""]
  resources: ["endpoints"]
  verbs: ["get"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: endpoint-reader
  namespace: rabbitmq
subjects:
- kind: ServiceAccount
  name: rabbitmq
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: endpoint-reader

编辑service配置

vi service.yaml

bash 复制代码
kind: Service
apiVersion: v1
metadata:
  name: rabbitmq-headless       #该service服务为提供无头服务
  namespace: rabbitmq
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  ports:
   - name: amqp
     port: 5672
   - name: http
     port: 15672
  selector:
    app: rabbitmq

vi service-nodeport.yaml

bash 复制代码
kind: Service
apiVersion: v1
metadata:
  namespace: rabbitmq
  name: rabbitmq-service
spec:
  ports:
  - name: http
    protocol: TCP
    port: 15672
    nodePort: 30015  #管理web界面
  - name: amqp
    protocol: TCP
    port: 5672
    targetPort: 5672
    nodePort: 30016
  selector:
    app: rabbitmq
  type: NodePort

编辑pv配置(使用该nfs路径时一定要配置一定的写入权限,不然启动rabbitmq服务时会出现没有写入权限而报错)

vi pv.yaml

bash 复制代码
apiVersion: v1
kind: PersistentVolume
metadata:
  name: rabbitmq-pv01
spec:
  storageClassName: rabbitmq-pv    #定义了存储类型
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /volume2/k8s-data/rabbitmq/rabbitmq-pv01
    server: 10.1.13.99
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: rabbitmq-pv02
spec:
  storageClassName: rabbitmq-pv    #定义了存储类型
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /volume2/k8s-data/rabbitmq/rabbitmq-pv02
    server: 10.1.13.99
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: rabbitmq-pv03
spec:
  storageClassName: rabbitmq-pv    #定义了存储类型
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /volume2/k8s-data/rabbitmq/rabbitmq-pv03
    server: 10.1.13.99

编辑setafulset配置

vi setafulset.yaml

bash 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rabbitmq
  namespace: rabbitmq
spec:
  podManagementPolicy: Parallel
  serviceName: rabbitmq-headless
  selector:
    matchLabels:
      app: rabbitmq  
  replicas: 3  #副本数3
  template:
    metadata:
      labels:
        app: rabbitmq
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:             #配置反亲和性,使pod不能部署再同一个node上
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - rabbitmq
              topologyKey: "kubernetes.io/hostname"
      tolerations:       #该配置是容忍污点,使pod可以部署再master上,可以不要该配置
      - key: "node-role.kubernetes.io/control-plane"
        operator: "Exists"
        effect: NoSchedule
      serviceAccountName: rabbitmq
      terminationGracePeriodSeconds: 10
      containers:
      - name: rabbitmq
        image: rabbitmq:3.10-management
        securityContext:
          runAsUser: 1000
        resources:
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 1
            memory: 1Gi
        volumeMounts:
          - name: config-volume
            mountPath: /etc/rabbitmq
          - name: rabbitmq-data      #使用pvc挂载数据目录,rabbitmq启动时会往该目录写入数据,所以挂载的该目录要具有写入权限
            mountPath: /var/lib/rabbitmq/mnesia
        ports:
          - name: http
            protocol: TCP
            containerPort: 15672
          - name: amqp
            protocol: TCP
            containerPort: 5672
        livenessProbe:
          exec:
            command: ["rabbitmq-diagnostics", "status"]
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command: ["rabbitmq-diagnostics", "status"]
          initialDelaySeconds: 20
          periodSeconds: 60
          timeoutSeconds: 5
        imagePullPolicy: Always
        env:
        - name: RABBITMQ_DEFAULT_USER
          valueFrom:
            secretKeyRef:
              key: rabbitDefaulUser
              name: rabbitmq              
        - name: RABBITMQ_DEFAULT_PASS
          valueFrom:
            secretKeyRef:
              key: rabbitDefaultPass
              name: rabbitmq
        - name: RABBITMQ_ERLANG_COOKIE
          valueFrom:
            secretKeyRef:
              name: rabbitmq
              key: erlang.cookie
        - name: HOSTNAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: RABBITMQ_USE_LONGNAME
          value: "true"
        - name: SERVICE_NAME
          value: "rabbitmq-headless"
        - name: RABBITMQ_NODENAME
          value: rabbit@$(HOSTNAME).$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local
        - name: K8S_HOSTNAME_SUFFIX
          value: .$(SERVICE_NAME).$(NAMESPACE).svc.cluster.local
      volumes:
      - name: config-volume
        configMap:
          name: rabbitmq-config
          items:
          - key: rabbitmq.conf
            path: rabbitmq.conf
          - key: enabled_plugins
            path: enabled_plugins
  volumeClaimTemplates:
  - metadata:
      name: rabbitmq-data
    spec:
      storageClassName: "rabbitmq-pv"
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 30Gi

创建命名空间

bash 复制代码
kubectl create namespace rabbitmq

创建各yaml服务

bash 复制代码
kubectl create -f secret.yaml
kubectl create -f configmap.yaml
kubectl create -f pv.yaml
kubectl create -f service.yaml
kubectl create -f service-nodeport.yaml
kubectl create -f setafulset.yaml

查看集群状态

bash 复制代码
kubectl exec -it rabbitmq-0 -n rabbitmq -- rabbitmqctl cluster_status

可以看到rabbitmq集群正常运行,至此rabbitmq集群部署完成

相关推荐
petaexpress32 分钟前
常用的k8s容器网络模式有哪些?
网络·容器·kubernetes
小宋10213 小时前
玩转RabbitMQ声明队列交换机、消息转换器
服务器·分布式·rabbitmq
有你的晚安._12 小时前
pod基本概念
kubernetes
Richardlygo19 小时前
(k8s)Kubernetes部署Promehteus
云原生·容器·kubernetes
xcx00319 小时前
快充协议方案,Type-C接口受电端Sink取电快充协议芯片
zookeeper·rabbitmq·flume·memcached·storm
杰信步迈入C++之路20 小时前
【RabbitMQ】快速上手
分布式·rabbitmq·ruby
c1tenj220 小时前
docker创建rabbitmq容器
java·rabbitmq·java-rabbitmq
炸裂狸花猫20 小时前
Kubernetes从零到精通(12-Ingress、Gateway API)
容器·kubernetes·gateway
sdg_advance1 天前
RabbitMQ消费者确认和重复机制
rabbitmq·java-rabbitmq
青云交1 天前
大数据新视界 --大数据大厂之Kubernetes与大数据:容器化部署的最佳实践
数据库·kubernetes·容器编排·资源管理·大数据处理·扩展性、故障恢复·存储持久化·监控、日志管理、性能提升