云其它

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > 云其它 > k8s部署rabbitmq

k8s部署rabbitmq集群的方式

作者:Apex Predator

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

一、前言

      本次部署方式为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

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

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

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

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

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

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

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

创建命名空间

kubectl create namespace rabbitmq

创建各yaml服务

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

查看集群状态

kubectl exec -it rabbitmq-0 -n rabbitmq -- rabbitmqctl cluster_status

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

到此这篇关于k8s部署rabbitmq集群的文章就介绍到这了,更多相关k8s部署rabbitmq内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文