k8s搭建rabbitmq群集

k8s搭建rabbitmq群集

本章rabbitmq数据使用nfs存储类,请先创建nfs-storage,然后在依次执行yaml文件。

创建唯一erlang.cookie

echo $(openssl rand -base64 32) > erlang.cookie
kubectl create secret generic erlang.cookie --from-file=erlang.cookie -n rabbitmq

rabbitmq-data.yaml

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    k8s.kuboard.cn/pvcType: Dynamic
  name: rabbitmq-data
  namespace: rabbitmq
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-storage

rabbitmq.yaml

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: rabbitmq
  name: rabbitmq
  annotations:
    k8s.kuboard.cn/workload: rabbitmq
    k8s.eip.work/ingress: 'false'
    k8s.eip.work/service: NodePort
    k8s.eip.work/workload: rabbitmq
    k8s.kuboard.cn/service: NodePort
    k8s.kuboard.cn/ingress: 'false'
  labels:
    k8s.eip.work/layer: ''
    k8s.eip.work/name: rabbitmq
spec:
  selector:
    matchLabels:
      k8s.eip.work/layer: ''
      k8s.eip.work/name: rabbitmq
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        k8s.eip.work/layer: ''
        k8s.eip.work/name: rabbitmq
    spec:
      securityContext:
        seLinuxOptions: {}
      imagePullSecrets: []
      restartPolicy: Always
      initContainers: []
      containers:
        - image: '镜像仓库地址/rabbitmq:3.8.3-management-delayed'
          imagePullPolicy: IfNotPresent
          name: rabbitmq
          volumeMounts:
            - name: rabbitmq-data
              mountPath: /var/lib/rabbitmq/mnesia
              subPath: rabbitmq
            - name: data-config
              readOnly: true
              mountPath: /etc/localtime
          resources:
            limits:
              cpu: 600m
              memory: 900Mi
            requests:
              cpu: 400m
              memory: 800Mi
          env:
            - name: RABBITMQ_DEFAULT_USER
              value: admin
            - name: RABBITMQ_DEFAULT_PASS
              value: jnThdLQ3MQ0D9Vvn
            - name: RABBITMQ_ERLANG_COOKIE
              valueFrom:
                secretKeyRef:
                  name: erlang.cookie
                  key: erlang.cookie
            - name: HOSTNAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: SERVICE_INTERNAL_NAME
              value: rabbitmq
            - name: RABBITMQ_USE_LONGNAME
              value: 'true'
            - name: RABBITMQ_NODENAME
              value: >-
                rabbit@$(HOSTNAME).$(SERVICE_INTERNAL_NAME).$(NAMESPACE).svc.cluster.local
          lifecycle: {}
          ports:
            - name: amqp
              containerPort: 5672
              protocol: TCP
            - name: http
              containerPort: 15672
              protocol: TCP
      volumes:
        - name: rabbitmq-data
          persistentVolumeClaim:
            claimName: rabbitmq-data
        - name: data-config
          hostPath:
            path: /etc/localtime
            type: ''
      dnsPolicy: ClusterFirst
      dnsConfig: {}
      terminationGracePeriodSeconds: 30
  replicas: 3
  volumeClaimTemplates: []
  serviceName: rabbitmq

---
apiVersion: v1
kind: Service
metadata:
  namespace: rabbitmq
  name: rabbitmq
  annotations:
    k8s.kuboard.cn/workload: rabbitmq
    k8s.eip.work/workload: rabbitmq
  labels:
    k8s.eip.work/layer: ''
    k8s.eip.work/name: rabbitmq
spec:
  selector:
    k8s.eip.work/layer: ''
    k8s.eip.work/name: rabbitmq
  type: NodePort
  ports:
    - port: 15672
      targetPort: 15672
      protocol: TCP
      name: ewpqda
      nodePort: 31672
    - port: 5672
      targetPort: 5672
      protocol: TCP
      name: ktfftz
      nodePort: 30672
  sessionAffinity: None
kubectl create -f ./rabbitmq.yaml

kubectl exec -it -n r rabbitmq-1 /bin/bash
rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) stop_app
rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) join_cluster rabbit@rabbitmq-0
rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) start_app
#rabbitmq-2同上

kubectl exec -it -n r rabbitmq-0 /bin/bash
rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) cluster_status----Running Nodes----rabbit@rabbitmq-0.rabbitmq.r.svc.cluster.local----rabbit@rabbitmq-1.rabbitmq.r.svc.cluster.local----rabbit@rabbitmq-2.rabbitmq.r.svc.cluster.local
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值