1、RabbitMQ介紹
kind: ConfigMap apiVersion: v1 metadata: name: rabbitmq-cluster-config namespace: default labels: addonmanager.kubernetes.io/mode: Reconcile data: enabled_plugins: | [rabbitmq_management,rabbitmq_peer_discovery_k8s]. rabbitmq.conf: | default_user = admin default_pass = 123!@# ## Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to learn more. cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s cluster_formation.k8s.host = kubernetes.default.svc.cluster.local ## Should RabbitMQ node name be computed from the pod's hostname or IP address? ## IP addresses are not stable, so using [stable] hostnames is recommended when possible. ## Set to "hostname" to use pod hostnames. ## When this value is changed, so should the variable used to set the RABBITMQ_NODENAME ## environment variable. cluster_formation.k8s.address_type = hostname ## How often should node cleanup checks run? cluster_formation.node_cleanup.interval = 30 ## Set to false if automatic removal of unknown/absent nodes ## is desired. This can be dangerous, see ## * https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup ## * https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ cluster_formation.node_cleanup.only_log_warning = true cluster_partition_handling = autoheal ## See https://www.rabbitmq.com/ha.html#master-migration-data-locality queue_master_locator=min-masters ## See https://www.rabbitmq.com/access-control.html#loopback-users loopback_users.guest = false cluster_formation.randomized_startup_delay_range.min = 0 cluster_formation.randomized_startup_delay_range.max = 2 # default is rabbitmq-cluster's namespace # hostname_suffix cluster_formation.k8s.hostname_suffix = .rabbitmq-cluster.default.svc.cluster.local # memory vm_memory_high_watermark.absolute = 1GB # disk disk_free_limit.absolute = 2GB
kind: Service apiVersion: v1 metadata: labels: app: rabbitmq-cluster name: rabbitmq-cluster namespace: default spec: clusterIP: None ports: - name: rmqport port: 5672 targetPort: 5672 selector: app: rabbitmq-cluster --- kind: Service apiVersion: v1 metadata: labels: app: rabbitmq-cluster name: rabbitmq-cluster-manage namespace: default spec: ports: - name: http port: 15672 protocol: TCP targetPort: 15672 selector: app: rabbitmq-cluster type: NodePort
apiVersion: v1 kind: ServiceAccount metadata: name: rabbitmq-cluster namespace: default --- kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: rabbitmq-cluster namespace: default rules: - apiGroups: [""] resources: ["endpoints"] verbs: ["get"] --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: rabbitmq-cluster namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: rabbitmq-cluster subjects: - kind: ServiceAccount name: rabbitmq-cluster namespace: default
kind: StatefulSet apiVersion: apps/v1 metadata: labels: app: rabbitmq-cluster name: rabbitmq-cluster namespace: default spec: replicas: 3 selector: matchLabels: app: rabbitmq-cluster serviceName: rabbitmq-cluster template: metadata: labels: app: rabbitmq-cluster spec: containers: - args: - -c - cp -v /etc/rabbitmq/rabbitmq.conf ${RABBITMQ_CONFIG_FILE}; exec docker-entrypoint.sh rabbitmq-server command: - sh env: - name: TZ value: 'Asia/Shanghai' - name: RABBITMQ_ERLANG_COOKIE value: 'SWvCP0Hrqv43NG7GybHC95ntCJKoW8UyNFWnBEWG8TY=' - name: K8S_SERVICE_NAME value: rabbitmq-cluster - name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: RABBITMQ_USE_LONGNAME value: "true" - name: RABBITMQ_NODENAME value: rabbit@$(POD_NAME).$(K8S_SERVICE_NAME).$(POD_NAMESPACE).svc.cluster.local - name: RABBITMQ_CONFIG_FILE value: /var/lib/rabbitmq/rabbitmq.conf image: rabbitmq:3.8.3-management imagePullPolicy: IfNotPresent livenessProbe: exec: command: - rabbitmq-diagnostics - status # See https://www.rabbitmq.com/monitoring.html for monitoring frequency recommendations. initialDelaySeconds: 60 periodSeconds: 60 timeoutSeconds: 15 name: rabbitmq ports: - containerPort: 15672 name: http protocol: TCP - containerPort: 5672 name: amqp protocol: TCP readinessProbe: exec: command: - rabbitmq-diagnostics - status initialDelaySeconds: 20 periodSeconds: 60 timeoutSeconds: 10 volumeMounts: - mountPath: /etc/rabbitmq name: config-volume readOnly: false - mountPath: /var/lib/rabbitmq name: rabbitmq-storage readOnly: false - name: timezone mountPath: /etc/localtime readOnly: true serviceAccountName: rabbitmq-cluster terminationGracePeriodSeconds: 30 volumes: - name: config-volume configMap: items: - key: rabbitmq.conf path: rabbitmq.conf - key: enabled_plugins path: enabled_plugins name: rabbitmq-cluster-config - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai volumeClaimTemplates: - metadata: name: rabbitmq-storage spec: accessModes: - ReadWriteMany storageClassName: "managed-nfs-storage" resources: requests: storage: 2Gi
3.6 部署檢查
➜ rabbitmq-cluster pwd
/Users/ssgeek/Documents/k8s-manifests/rabbitmq-cluster
➜ rabbitmq-cluster ls
01-rabbitmq-configmap.yaml 02-rabbitmq-service.yaml 03-rabbitmq-rbac.yaml 04-rabbitmq-cluster-sts.yaml
➜ rabbitmq-cluster kubectl apply -f .
configmap/rabbitmq-cluster-config created
service/rabbitmq-cluster created
service/rabbitmq-cluster-manage created
serviceaccount/rabbitmq-cluster created
role.rbac.authorization.k8s.io/rabbitmq-cluster created
rolebinding.rbac.authorization.k8s.io/rabbitmq-cluster created
statefulset.apps/rabbitmq-cluster created
等待一段時間,查看創建的相關資源
➜ rabbitmq-cluster kubectl get po,sts -l app=rabbitmq-cluster
NAME READY STATUS RESTARTS AGE
pod/rabbitmq-cluster-0 1/1 Running 0 3m22s
pod/rabbitmq-cluster-1 1/1 Running 0 2m18s
pod/rabbitmq-cluster-2 1/1 Running 0 2m23s
NAME READY AGE
statefulset.apps/rabbitmq-cluster 3/3 3m25s
查看日誌,從日誌的最後部分觀察集群建立的狀態
➜ rabbitmq-cluster kubectl logs -f rabbitmq-cluster-0 '/etc/rabbitmq/rabbitmq.conf' -> '/var/lib/rabbitmq/rabbitmq.conf' 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: list of feature flags found: 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: [ ] drop_unroutable_metric 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: [ ] empty_basic_get_metric 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: [ ] implicit_default_bindings 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: [ ] quorum_queue 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: [ ] virtual_host_metadata 2021-02-17 03:30:39.445 [info] <0.9.0> Feature flags: feature flag states written to disk: yes 2021-02-17 03:30:39.544 [info] <0.269.0> ra: meta data store initialised. 0 record(s) recovered 2021-02-17 03:30:39.547 [info] <0.274.0> WAL: recovering [] 2021-02-17 03:31:10.676 [info] <0.313.0> Starting RabbitMQ 3.8.3 on Erlang 22.3.4.1 Copyright (c) 2007-2020 Pivotal Software, Inc. Licensed under the MPL 1.1. Website: https://rabbitmq.com ## ## RabbitMQ 3.8.3 ## ## ########## Copyright (c) 2007-2020 Pivotal Software, Inc. ###### ## ########## Licensed under the MPL 1.1. Website: https://rabbitmq.com Doc guides: https://rabbitmq.com/documentation.html Support: https://rabbitmq.com/contact.html Tutorials: https://rabbitmq.com/getstarted.html Monitoring: https://rabbitmq.com/monitoring.html Logs: <stdout> Config file(s): /var/lib/rabbitmq/rabbitmq.conf Starting broker...2021-02-17 03:31:10.678 [info] <0.313.0> node : rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local home dir : /var/lib/rabbitmq config file(s) : /var/lib/rabbitmq/rabbitmq.conf cookie hash : H+IQL2spD4MDV4jPi7mMAg== log(s) : <stdout> database dir : /var/lib/rabbitmq/mnesia/rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local 2021-02-17 03:31:10.695 [info] <0.313.0> Running boot step pre_boot defined by app rabbit ...省略中間內容 2021-02-17 03:31:13.273 [info] <0.824.0> Statistics database started. 2021-02-17 03:31:13.273 [info] <0.823.0> Starting worker pool 'management_worker_pool' with 3 processes in it 2021-02-17 03:31:13.467 [info] <0.9.0> Server startup complete; 5 plugins started. * rabbitmq_peer_discovery_k8s * rabbitmq_management * rabbitmq_web_dispatch * rabbitmq_management_agent * rabbitmq_peer_discovery_common completed with 5 plugins. 2021-02-17 03:32:30.060 [info] <0.566.0> node 'rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local' up 2021-02-17 03:32:31.264 [info] <0.566.0> rabbit on node 'rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local' up 2021-02-17 03:33:31.280 [info] <0.566.0> node 'rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local' up 2021-02-17 03:33:32.627 [info] <0.566.0> rabbit on node 'rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local' up
進入到pod中通過用戶端查看集群狀態
➜ rabbitmq-cluster kubectl exec -it rabbitmq-cluster-0 bash root@rabbitmq-cluster-0:/# rabbitmqctl cluster_status Cluster status of node rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local ... Basics Cluster name: rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local Disk Nodes rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local Running Nodes rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local Versions rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local: RabbitMQ 3.8.3 on Erlang 22.3.4.1 rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local: RabbitMQ 3.8.3 on Erlang 22.3.4.1 rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local: RabbitMQ 3.8.3 on Erlang 22.3.4.1 Alarms (none) Network Partitions (none) Listeners Node: rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Node: rabbit@rabbitmq-cluster-0.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 15672, protocol: http, purpose: HTTP API Node: rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Node: rabbit@rabbitmq-cluster-1.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 15672, protocol: http, purpose: HTTP API Node: rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication Node: rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0 Node: rabbit@rabbitmq-cluster-2.rabbitmq-cluster.default.svc.cluster.local, interface: [::], port: 15672, protocol: http, purpose: HTTP API Feature flags Flag: drop_unroutable_metric, state: enabled Flag: empty_basic_get_metric, state: enabled Flag: implicit_default_bindings, state: enabled Flag: quorum_queue, state: enabled Flag: virtual_host_metadata, state: enabled
確認pvc持久性檔案已建立
mq新增一個管理員指令
登入mq ui 可以看到集群已建立