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

你可能感兴趣的:(k8s,rabbitmq群集)