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集群部署完成

相关推荐
是芽芽哩!3 小时前
【Kubernetes 指南】基础入门——Kubernetes 基本概念(二)
云原生·容器·kubernetes
怡雪~4 小时前
k8s使用ceph
ceph·容器·kubernetes
运维小文5 小时前
K8S中的服务质量QOS
云原生·容器·kubernetes
华为云开发者联盟5 小时前
Karmada v1.12 版本发布!单集群应用迁移可维护性增强
云原生·kubernetes·开源·容器编排·karmada
Hadoop_Liang6 小时前
Kubernetes Secret的创建与使用
云原生·容器·kubernetes
元气满满的热码式6 小时前
K8S集群部署实战(超详细)
云原生·容器·kubernetes
上海运维Q先生7 小时前
面试题整理13----deployment和statefulset区别
运维·面试·kubernetes
Allen Bright13 小时前
RabbitMQ中的普通Confirm模式:深入解析与最佳实践
分布式·rabbitmq
S-X-S13 小时前
集成RabbitMQ+MQ常用操作
rabbitmq
dzend13 小时前
Kafka、RocketMQ、RabbitMQ 对比
kafka·rabbitmq·rocketmq