0


Kubernetes 部署 RabbitMQ

文章目录

  • Kubernetes
  1. # Kubernetes
  2. https://iothub.org.cn/docs/kubernetes/
  3. https://iothub.org.cn/docs/kubernetes/middleware/rabbitmq/

一、概述

1.Operator

  1. # awesome-operators
  2. https://github.com/operator-framework/awesome-operators
  3. # 安装# RabbitMQ #1 (Official) rabbitmq/cluster-operator
  4. https://github.com/rabbitmq/cluster-operator

App NameGithubDescriptionRabbitMQ #1 (Official)rabbitmq/cluster-operatorRabbitMQ cluster operator for Kubernetes.RabbitMQ #2skylt/rabbitmq-operatorRabbitMQ operator for Kubernetes.

2.Helm

Bitnami

  1. https://bitnami.com/
  2. https://github.com/bitnami
  3. https://bitnami.com/stacks
  1. [root@k8s-master redis-cluster-operator]# helm search repo rabbitmq
  2. NAME CHART VERSION APP VERSION DESCRIPTION
  3. bitnami/rabbitmq 8.23.0 3.9.7 Open source message broker software that implem...
  4. bitnami/rabbitmq-cluster-operator 0.1.6 1.9.0 The RabbitMQ Cluster Kubernetes Operator automa...
  5. ......

二、基础

1.Operator

  1. # 安装# RabbitMQ #1 (Official) rabbitmq/cluster-operator
  2. https://github.com/rabbitmq/cluster-operator
  3. #RabbitMQ Cluster Kubernetes Operator Quickstart
  4. https://www.rabbitmq.com/kubernetes/operator/quickstart-operator.html
  5. # Using RabbitMQ Cluster Kubernetes Operator
  6. https://www.rabbitmq.com/kubernetes/operator/using-operator.html
  1. # 参考# Operator overview
  2. https://www.rabbitmq.com/kubernetes/operator/operator-overview.html
  3. # Deploying an operator
  4. https://www.rabbitmq.com/kubernetes/operator/install-operator.html
  5. # Deploying a RabbitMQ cluster
  6. https://www.rabbitmq.com/kubernetes/operator/using-operator.html
  7. # Monitoring the cluster
  8. https://www.rabbitmq.com/kubernetes/operator/operator-monitoring.html
  9. # Troubleshooting operator deployments
  10. https://www.rabbitmq.com/kubernetes/operator/troubleshooting-operator.html

下载rabbitmq/cluster-operator

  1. # git git clone https://github.com/rabbitmq/cluster-operator.git[root@k8s-master rabbitmq]# git clone https://github.com/rabbitmq/cluster-operator.git
  2. Cloning into 'cluster-operator'...
  3. remote: Enumerating objects: 11892, done.
  4. remote: Counting objects: 100% (2279/2279), done.
  5. remote: Compressing objects: 100% (976/976), done.
  6. remote: Total 11892(delta 1438), reused 1976(delta 1240), pack-reused 9613
  7. Receiving objects: 100% (11892/11892), 11.95 MiB |1.66 MiB/s, done.
  8. Resolving deltas: 100% (7740/7740), done.
1.1.Quickstart(单实例)
  1. #RabbitMQ Cluster Kubernetes Operator Quickstart
  2. https://www.rabbitmq.com/kubernetes/operator/quickstart-operator.html
  3. Quickstart Steps
  4. This guide goes through the following steps:
  5. Install the RabbitMQ Cluster Operator
  6. Deploy a RabbitMQ Cluster using the Operator
  7. View RabbitMQ Logs
  8. Access the RabbitMQ Management UI
  9. Attach a Workload to the Cluster
  10. Next Steps

1.创建Operator

  1. #kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml[root@k8s-master docs]# kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  2. namespace/rabbitmq-system created
  3. customresourcedefinition.apiextensions.k8s.io/rabbitmqclusters.rabbitmq.com created
  4. serviceaccount/rabbitmq-cluster-operator created
  5. role.rbac.authorization.k8s.io/rabbitmq-cluster-leader-election-role created
  6. clusterrole.rbac.authorization.k8s.io/rabbitmq-cluster-operator-role created
  7. rolebinding.rbac.authorization.k8s.io/rabbitmq-cluster-leader-election-rolebinding created
  8. clusterrolebinding.rbac.authorization.k8s.io/rabbitmq-cluster-operator-rolebinding created
  9. deployment.apps/rabbitmq-cluster-operator created
  10. # 查看# 会创建一个新的名称空间:rabbitmq-system[root@k8s-master docs]# kubectl get all -n rabbitmq-system
  11. NAME READY STATUS RESTARTS AGE
  12. pod/rabbitmq-cluster-operator-7cbf865f89-5c4wk 1/1 Running 0 81s
  13. NAME READY UP-TO-DATE AVAILABLE AGE
  14. deployment.apps/rabbitmq-cluster-operator 1/1 11 81s
  15. NAME DESIRED CURRENT READY AGE
  16. replicaset.apps/rabbitmq-cluster-operator-7cbf865f89 111 81s

2.使用cluster-operator创建RabbitMQ

简单的yaml文件下载地址:https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples/hello-world
yaml文件名:rabbitmq.yaml
这是最简单的RabbitmqCluster定义。唯一显式指定的属性是集群的名称。其他一切都将根据集群运营商的默认值进行配置。

examples文件夹还有许多其他引用,比如用TLS、mTLS创建RabbitMQ集群,用生产默认值设置集群,添加社区插件等等。下载地址:https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples/

  1. # rabbitmq.yamlapiVersion: rabbitmq.com/v1beta1
  2. kind: RabbitmqCluster
  3. metadata:name: hello-world
  1. # kubectl apply -f https://raw.githubusercontent.com/rabbitmq/cluster-operator/main/docs/examples/hello-world/rabbitmq.yaml[root@k8s-master docs]# kubectl apply -f https://raw.githubusercontent.com/rabbitmq/cluster-operator/main/docs/examples/hello-world/rabbitmq.yaml
  2. rabbitmqcluster.rabbitmq.com/hello-world created
  3. [root@k8s-master docs]# kubectl get all
  4. NAME READY STATUS RESTARTS AGE
  5. pod/hello-world-server-0 0/1 Pending 0 74s
  6. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  7. service/hello-world ClusterIP 10.100.123.79 <none>15672/TCP,15692/TCP,5672/TCP 74s
  8. service/hello-world-nodes ClusterIP None <none>4369/TCP,25672/TCP 74s
  9. NAME READY AGE
  10. statefulset.apps/hello-world-server 0/1 74s
  11. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  12. rabbitmqcluster.rabbitmq.com/hello-world False Unknown 74s
  13. [root@k8s-master rabbitmq]# kubectl get pvc
  14. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  15. persistence-hello-world-server-0 Pending 16m
  16. # 查找Pending愿意,没有存储[root@k8s-master docs]# kubectl describe pod hello-world-server-0
  17. Name: hello-world-server-0
  18. Namespace: default
  19. Priority: 0
  20. Node: <none>
  21. Labels: app.kubernetes.io/component=rabbitmq
  22. app.kubernetes.io/name=hello-world
  23. app.kubernetes.io/part-of=rabbitmq
  24. controller-revision-hash=hello-world-server-6669f4bc8f
  25. statefulset.kubernetes.io/pod-name=hello-world-server-0
  26. Annotations: prometheus.io/port: 15692
  27. prometheus.io/scrape: true
  28. Status: Pending
  29. IP:
  30. IPs: <none>
  31. Controlled By: StatefulSet/hello-world-server
  32. Init Containers:
  33. setup-container:
  34. Image: rabbitmq:3.8.21-management
  35. Port: <none>
  36. Host Port: <none>
  37. Command:
  38. sh-ccp /tmp/erlang-cookie-secret/.erlang.cookie /var/lib/rabbitmq/.erlang.cookie &&chmod600 /var/lib/rabbitmq/.erlang.cookie ;cp /tmp/rabbitmq-plugins/enabled_plugins /operator/enabled_plugins ;echo'[default]'> /var/lib/rabbitmq/.rabbitmqadmin.conf &&sed-e's/default_user/username/'-e's/default_pass/password/' /tmp/default_user.conf >> /var/lib/rabbitmq/.rabbitmqadmin.conf &&chmod600 /var/lib/rabbitmq/.rabbitmqadmin.conf
  39. Limits:
  40. cpu: 100m
  41. memory: 500Mi
  42. Requests:
  43. cpu: 100m
  44. memory: 500Mi
  45. Environment: <none>
  46. Mounts:
  47. /operator from rabbitmq-plugins (rw)
  48. /tmp/default_user.conf from rabbitmq-confd (rw,path="default_user.conf")
  49. /tmp/erlang-cookie-secret/ from erlang-cookie-secret (rw)
  50. /tmp/rabbitmq-plugins/ from plugins-conf (rw)
  51. /var/lib/rabbitmq/ from rabbitmq-erlang-cookie (rw)
  52. /var/lib/rabbitmq/mnesia/ from persistence (rw)
  53. /var/run/secrets/kubernetes.io/serviceaccount from hello-world-server-token-xvxcp (ro)
  54. Containers:
  55. rabbitmq:
  56. Image: rabbitmq:3.8.21-management
  57. Ports: 4369/TCP, 5672/TCP, 15672/TCP, 15692/TCP
  58. Host Ports: 0/TCP, 0/TCP, 0/TCP, 0/TCP
  59. Limits:
  60. cpu: 2
  61. memory: 2Gi
  62. Requests:
  63. cpu: 1
  64. memory: 2Gi
  65. Readiness: tcp-socket :amqp delay=10s timeout=5s period=10s #success=1 #failure=3
  66. Environment:
  67. MY_POD_NAME: hello-world-server-0 (v1:metadata.name)
  68. MY_POD_NAMESPACE: default (v1:metadata.namespace)
  69. K8S_SERVICE_NAME: hello-world-nodes
  70. RABBITMQ_ENABLED_PLUGINS_FILE: /operator/enabled_plugins
  71. RABBITMQ_USE_LONGNAME: true
  72. RABBITMQ_NODENAME: rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE)
  73. K8S_HOSTNAME_SUFFIX: .$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE)
  74. Mounts:
  75. /etc/pod-info/ from pod-info (rw)
  76. /etc/rabbitmq/conf.d/10-operatorDefaults.conf from rabbitmq-confd (rw,path="operatorDefaults.conf")
  77. /etc/rabbitmq/conf.d/11-default_user.conf from rabbitmq-confd (rw,path="default_user.conf")
  78. /etc/rabbitmq/conf.d/90-userDefinedConfiguration.conf from rabbitmq-confd (rw,path="userDefinedConfiguration.conf")
  79. /operator from rabbitmq-plugins (rw)
  80. /var/lib/rabbitmq/ from rabbitmq-erlang-cookie (rw)
  81. /var/lib/rabbitmq/mnesia/ from persistence (rw)
  82. /var/run/secrets/kubernetes.io/serviceaccount from hello-world-server-token-xvxcp (ro)
  83. Conditions:
  84. Type Status
  85. PodScheduled False
  86. Volumes:
  87. persistence:
  88. Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
  89. ClaimName: persistence-hello-world-server-0
  90. ReadOnly: false
  91. plugins-conf:
  92. Type: ConfigMap (a volume populated by a ConfigMap)
  93. Name: hello-world-plugins-conf
  94. Optional: false
  95. rabbitmq-confd:
  96. Type: Projected (a volume that contains injected data from multiple sources)
  97. ConfigMapName: hello-world-server-conf
  98. ConfigMapOptional: <nil>
  99. SecretName: hello-world-default-user
  100. SecretOptionalName: <nil>
  101. rabbitmq-erlang-cookie:
  102. Type: EmptyDir (a temporary directory that shares a pod's lifetime)
  103. Medium:
  104. SizeLimit: <unset>
  105. erlang-cookie-secret:
  106. Type: Secret (a volume populated by a Secret)
  107. SecretName: hello-world-erlang-cookie
  108. Optional: false
  109. rabbitmq-plugins:
  110. Type: EmptyDir (a temporary directory that shares a pod's lifetime)
  111. Medium:
  112. SizeLimit: <unset>
  113. pod-info:
  114. Type: DownwardAPI (a volume populated by information about the pod)
  115. Items:
  116. metadata.labels['skipPreStopChecks'] -> skipPreStopChecks
  117. hello-world-server-token-xvxcp:
  118. Type: Secret (a volume populated by a Secret)
  119. SecretName: hello-world-server-token-xvxcp
  120. Optional: false
  121. QoS Class: Burstable
  122. Node-Selectors: <none>
  123. Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
  124. node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
  125. Events:
  126. Type Reason Age From Message
  127. ---- ------ ---- ---- -------
  128. Warning FailedScheduling 8m21s default-scheduler 0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
  129. Warning FailedScheduling 8m21s default-scheduler 0/4 nodes are available: 4 pod has unbound immediate PersistentVolumeClaims.
  1. # 手动创建PVC# persistence-hello-world-server-0# persistence-hello-world-server-0.yamlapiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:name: persistence-hello-world-server-0namespace: default
  4. spec:accessModes:- ReadWriteOnce
  5. storageClassName: rook-ceph-block
  6. resources:requests:storage: 12Gi
  7. [root@k8s-master rabbitmq]# kubectl apply -f persistence-hello-world-server-0.yaml
  8. persistentvolumeclaim/persistence-hello-world-server-0 created
  9. [root@k8s-master rabbitmq]# kubectl get pvc
  10. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  11. persistence-hello-world-server-0 Bound pvc-91f82092-4b29-42ab-87b4-2cf3f9099089 12Gi RWO rook-ceph-block 3s
  1. # 服务正常[root@k8s-master rabbitmq]# kubectl get all
  2. NAME READY STATUS RESTARTS AGE
  3. pod/hello-world-server-0 1/1 Running 0 20m
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. service/hello-world ClusterIP 10.100.123.79 <none>15672/TCP,15692/TCP,5672/TCP 20m
  6. service/hello-world-nodes ClusterIP None <none>4369/TCP,25672/TCP 20m
  7. NAME READY AGE
  8. statefulset.apps/hello-world-server 1/1 20m
  9. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  10. rabbitmqcluster.rabbitmq.com/hello-world True True 20m
  11. [root@k8s-master rabbitmq]# kubectl get pvc
  12. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  13. persistence-hello-world-server-0 Bound pvc-91f82092-4b29-42ab-87b4-2cf3f9099089 12Gi RWO rook-ceph-block 2m27s
1.2.获取用户名和密码

获取用户名和密码

  1. # 获取获取用户名和密码username="$(kubectl get secret hello-world-default-user -ojsonpath='{.data.username}'| base64 --decode)"echo"username: $username"password="$(kubectl get secret hello-world-default-user -ojsonpath='{.data.password}'| base64 --decode)"echo"password: $password"[root@k8s-master rabbitmq]# username="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.username}' | base64 --decode)"[root@k8s-master rabbitmq]# echo "username: $username"
  2. username: default_user_HGTmPURsI5VhaVjmeEQ
  3. [root@k8s-master rabbitmq]# password="$(kubectl get secret hello-world-default-user -o jsonpath='{.data.password}' | base64 --decode)"[root@k8s-master rabbitmq]# echo "password: $password"
  4. password: TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX
  5. # 登录信息:
  6. username: default_user_HGTmPURsI5VhaVjmeEQ
  7. password: TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX
  8. kubectl port-forward "service/hello-world"15672
  1. # 系统创建service# hello-world[root@k8s-master rabbitmq]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. hello-world ClusterIP 10.100.123.79 <none>15672/TCP,15692/TCP,5672/TCP 33m
  4. hello-world-nodes ClusterIP None <none>4369/TCP,25672/TCP 33m
  5. [root@k8s-master rabbitmq]# kubectl port-forward "service/hello-world" 15672
  6. Forwarding from 127.0.0.1:15672 ->15672
  7. Forwarding from [::1]:15672 ->15672curl -u$username:$password localhost:15672/api/overview
  8. {"management_version":"3.8.9","rates_mode":"basic", ...}curl-udefault_user_HGTmPURsI5VhaVjmeEQ:TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX10.100.123.79:15672/api/overview
  9. # 访问[root@k8s-master rabbitmq]# curl -udefault_user_HGTmPURsI5VhaVjmeEQ:TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX 10.100.123.79:15672/api/overview{"management_version":"3.8.21","rates_mode":"basic","sample_retention_policies":{"global":[600,3600,28800,86400],"basic":[600,3600],"detailed":[600]},"exchange_types":[{"name":"direct","description":"AMQP direct exchange, as per the AMQP specification","enabled":true},{"name":"fanout","description":"AMQP fanout exchange, as per the AMQP specification","enabled":true},{"name":"headers","description":"AMQP headers exchange, as per the AMQP specification","enabled":true},{"name":"topic","description":"AMQP topic exchange, as per the AMQP specification","enabled":true}],"product_version":"3.8.21","product_name":"RabbitMQ","rabbitmq_version":"3.8.21","cluster_name":"hello-world","erlang_version":"24.0.5","erlang_full_version":"Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:4:2] [ds:4:2:10] [async-threads:1] [jit]","disable_stats":false,"enable_queue_totals":false,"message_stats":{},"churn_rates":{"channel_closed":0,"channel_closed_details":{"rate":0.0},"channel_created":0,"channel_created_details":{"rate":0.0},"connection_closed":312,"connection_closed_details":{"rate":0.2},"connection_created":0,"connection_created_details":{"rate":0.0},"queue_created":0,"queue_created_details":{"rate":0.0},"queue_declared":0,"queue_declared_details":{"rate":0.0},"queue_deleted":0,"queue_deleted_details":{"rate":0.0}},"queue_totals":{},"object_totals":{"channels":0,"connections":0,"consumers":0,"exchanges":7,"queues":0},"statistics_db_event_queue":0,"node":"rabbit@hello-world-server-0.hello-world-nodes.default","listeners":[{"node":"rabbit@hello-world-server-0.hello-world-nodes.default","protocol":"amqp","ip_address":"::","port":5672,"socket_opts":{"backlog":128,"nodelay":true,"linger":[true,0],"exit_on_close":false}},{"node":"rabbit@hello-world-server-0.hello-world-nodes.default","protocol":"clustering","ip_address":"::","port":25672,"socket_opts":[]},{"node":"rabbit@hello-world-server-0.hello-world-nodes.default","protocol":"http","ip_address":"::","port":15672,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15672}},{"node":"rabbit@hello-world-server-0.hello-world-nodes.default","protocol":"http/prometheus","ip_address":"::","port":15692,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15692,"protocol":"http/prometheus"}}],"contexts":[{"ssl_opts":[],"node":"rabbit@hello-world-server-0.hello-world-nodes.default","description":"RabbitMQ Management","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15672"},{"ssl_opts":[],"node":"rabbit@hello-world-server-0.hello-world-nodes.default","description":"RabbitMQ Prometheus","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15692","protocol":"'http/prometheus'"}]}
1.3.访问RabbitMQ管理UI

rabbitmq-k8s-svc.yaml

  1. # rabbitmq-k8s-svc.yamlapiVersion: v1
  2. kind: Service
  3. metadata:namespace: default
  4. name: rabbitmq-k8s-svc
  5. labels:statefulset.kubernetes.io/pod-name: hello-world-server-0spec:type: NodePort
  6. ports:-port:15672name: rabbitmq-http
  7. targetPort:15672nodePort:30672-port:5672name: rabbitmq-tcp
  8. targetPort:5672nodePort:30673selector:statefulset.kubernetes.io/pod-name: hello-world-server-0
  1. [root@k8s-master rabbitmq]# kubectl apply -f rabbitmq-svc.yaml
  2. service/rabbitmq-svc created
  3. [root@k8s-master rabbitmq]# kubectl get svc
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. hello-world ClusterIP 10.100.123.79 <none>15672/TCP,15692/TCP,5672/TCP 141m
  6. hello-world-nodes ClusterIP None <none>4369/TCP,25672/TCP 141m
  7. rabbitmq-k8s-svc NodePort 10.96.247.212 <none>15672:30672/TCP 6s
  8. curl-udefault_user_HGTmPURsI5VhaVjmeEQ:TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX10.96.247.212:15672/api/overview
  1. # 地址
  2. http://172.51.216.81:30672
  3. username: default_user_HGTmPURsI5VhaVjmeEQ
  4. password: TawohNwWLpQ0D7xmpElx9bUsL_PuIhhX
1.4.删除Rabbitmq
  1. # 删除Rabbitmq# 根据创建选择
  2. kubectl delete -f https://raw.githubusercontent.com/rabbitmq/cluster-operator/main/docs/examples/hello-world/rabbitmq.yaml
  3. # 删除operator
  4. kubectl delete -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  1. # 查看[root@k8s-master rabbitmq]# kubectl get all -n rabbitmq-system
  2. NAME READY STATUS RESTARTS AGE
  3. pod/rabbitmq-cluster-operator-7cbf865f89-5c4wk 1/1 Running 1 19h
  4. NAME READY UP-TO-DATE AVAILABLE AGE
  5. deployment.apps/rabbitmq-cluster-operator 1/1 11 19h
  6. NAME DESIRED CURRENT READY AGE
  7. replicaset.apps/rabbitmq-cluster-operator-7cbf865f89 111 19h
  8. [root@k8s-master rabbitmq]# kubectl get all
  9. NAME READY STATUS RESTARTS AGE
  10. pod/hello-world-server-0 1/1 Running 1 19h
  11. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  12. service/hello-world ClusterIP 10.100.123.79 <none>15672/TCP,15692/TCP,5672/TCP 19h
  13. service/hello-world-nodes ClusterIP None <none>4369/TCP,25672/TCP 19h
  14. service/rabbitmq-k8s-svc NodePort 10.106.168.227 <none>15672:30672/TCP,5672:30673/TCP 29m
  15. NAME READY AGE
  16. statefulset.apps/hello-world-server 1/1 19h
  17. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  18. rabbitmqcluster.rabbitmq.com/hello-world True True 19h
  19. # 删除# 删除svc[root@k8s-master rabbitmq]# kubectl delete -f rabbitmq-k8s-svc.yaml service"rabbitmq-k8s-svc" deleted
  20. # 删除rabbitmq[root@k8s-master rabbitmq]# kubectl delete -f https://raw.githubusercontent.com/rabbitmq/cluster-operator/main/docs/examples/hello-world/rabbitmq.yaml
  21. rabbitmqcluster.rabbitmq.com "hello-world" deleted
  22. [root@k8s-master rabbitmq]# kubectl get all
  23. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  24. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 105d
  25. # 删除operator[root@k8s-master rabbitmq]# kubectl delete -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  26. namespace "rabbitmq-system" deleted
  27. customresourcedefinition.apiextensions.k8s.io "rabbitmqclusters.rabbitmq.com" deleted
  28. serviceaccount "rabbitmq-cluster-operator" deleted
  29. role.rbac.authorization.k8s.io "rabbitmq-cluster-leader-election-role" deleted
  30. clusterrole.rbac.authorization.k8s.io "rabbitmq-cluster-operator-role" deleted
  31. rolebinding.rbac.authorization.k8s.io "rabbitmq-cluster-leader-election-rolebinding" deleted
  32. clusterrolebinding.rbac.authorization.k8s.io "rabbitmq-cluster-operator-rolebinding" deleted
  33. deployment.apps "rabbitmq-cluster-operator" deleted
  34. [root@k8s-master rabbitmq]# kubectl get all -n rabbitmq-system
  35. No resources found in rabbitmq-system namespace.
  36. # 删除PVC[root@k8s-master rabbitmq]# kubectl delete -f persistence-hello-world-server-0.yaml
  37. persistentvolumeclaim "persistence-hello-world-server-0" deleted
  38. [root@k8s-master rabbitmq]# kubectl get pvc
  39. No resources found in default namespace.
1.5.安装方式选择

单实例安装方式

yaml文件下载地址:https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples/hello-world
yaml文件名:rabbitmq.yaml

这是最简单的RabbitmqCluster定义。唯一显式指定的属性是集群的名称。其他一切都将根据集群运营商的默认值进行配置。

examples文件夹还有许多其他引用,比如用TLS、mTLS创建RabbitMQ集群,用生产默认值设置集群,添加社区插件等等。

下载地址:https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples/

多种安装方式

https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples

  1. [root@k8s-master examples]# pwd
  2. /k8s/middleware/rabbitmq/cluster-operator/docs/examples
  3. [root@k8s-master examples]# ll
  4. total 4
  5. drwxr-xr-x 2 root root 59 Nov 3011:52 additionalPorts
  6. drwxr-xr-x 2 root root 59 Nov 3011:52 community-plugins
  7. drwxr-xr-x 2 root root 60 Nov 3011:52 custom-configuration
  8. drwxr-xr-x 2 root root 59 Nov 3011:52 default-security-context
  9. drwxr-xr-x 2 root root 177 Nov 3011:52 federation-over-tls
  10. drwxr-xr-x 2 root root 60 Nov 3011:52 hello-world
  11. drwxr-xr-x 2 root root 99 Nov 3011:52 import-definitions
  12. drwxr-xr-x 2 root root 59 Nov 3011:52 json-log
  13. drwxr-xr-x 2 root root 60 Nov 3011:52 mtls
  14. drwxr-xr-x 2 root root 161 Nov 3011:52 mtls-inter-node
  15. drwxr-xr-x 2 root root 59 Nov 3011:52 multiple-disks
  16. drwxr-xr-x 2 root root 167 Nov 3011:52 network-policies
  17. drwxr-xr-x 2 root root 60 Nov 3011:52 plugins
  18. drwxr-xr-x 2 root root 60 Nov 3011:52 pod-anti-affinity
  19. drwxr-xr-x 2 root root 114 Nov 3011:52 production-ready
  20. -rw-r--r-- 1 root root 2372 Nov 3011:52 README.md
  21. drwxr-xr-x 2 root root 60 Nov 3011:52 resource-limits
  22. drwxr-xr-x 2 root root 59 Nov 3011:52 set-login-password-username
  23. drwxr-xr-x 2 root root 59 Nov 3011:52 sidecar
  24. drwxr-xr-x 2 root root 99 Nov 3011:52 tls
  25. drwxr-xr-x 2 root root 60 Nov 3011:52 tolerations
  26. drwxr-xr-x 2 root root 75 Nov 3011:52 vault-default-user
  27. drwxr-xr-x 2 root root 75 Nov 3011:52 vault-tls
  1. # 单实例部署
  2. drwxr-xr-x 2 root root 60 Nov 3011:52 hello-world
  3. # 集群部署
  4. drwxr-xr-x 2 root root 114 Nov 3011:52 production-ready

三、实践

1.部署说明

集群部署:

https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples

https://github.com/rabbitmq/cluster-operator/tree/main/docs/examples/production-ready

安装说明

https://github.com/rabbitmq/cluster-operator/blob/main/docs/examples/production-ready/README.md

  1. [root@k8s-master examples]# cd production-ready/[root@k8s-master production-ready]# ll
  2. total 16
  3. -rw-r--r-- 1 root root 199 Nov 3011:52 pod-disruption-budget.yaml
  4. -rw-r--r-- 1 root root 1184 Nov 3011:52 rabbitmq.yaml
  5. -rw-r--r-- 1 root root 3134 Nov 3011:52 README.md
  6. -rw-r--r-- 1 root root 409 Nov 3011:52 ssd-gke.yaml
  7. # 部署
  8. kubectl apply -f rabbitmq.yaml
  9. kubectl apply -f pod-disruption-budget.yaml

配置文件

  1. # rabbitmq.yaml[root@k8s-master production-ready]# vim rabbitmq.yaml apiVersion: rabbitmq.com/v1beta1
  2. kind: RabbitmqCluster
  3. metadata:name: production-ready
  4. spec:replicas:3resources:requests:cpu:4memory: 10Gi
  5. limits:cpu:4memory: 10Gi
  6. rabbitmq:additionalConfig:|
  7. cluster_partition_handling = pause_minority
  8. vm_memory_high_watermark_paging_ratio = 0.99
  9. disk_free_limit.relative = 1.0
  10. collect_statistics_interval = 10000persistence:storageClassName: ssd
  11. storage:"500Gi"affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:-labelSelector:matchExpressions:-key: app.kubernetes.io/name
  12. operator: In
  13. values:- production-ready
  14. topologyKey: kubernetes.io/hostname
  15. override:statefulSet:spec:template:spec:containers:[]topologySpreadConstraints:-maxSkew:1topologyKey:"topology.kubernetes.io/zone"whenUnsatisfiable: DoNotSchedule
  16. labelSelector:matchLabels:app.kubernetes.io/name: production-ready
  1. # pod-disruption-budget.yaml [root@k8s-master production-ready]# vim pod-disruption-budget.yaml apiVersion: policy/v1beta1
  2. kind: PodDisruptionBudget
  3. metadata:name: production-ready-rabbitmq
  4. spec:maxUnavailable:1selector:matchLabels:app.kubernetes.io/name: production-ready

2.部署RabbitMQ集群

1.下载rabbitmq/cluster-operator

  1. # git git clone https://github.com/rabbitmq/cluster-operator.git[root@k8s-master rabbitmq]# git clone https://github.com/rabbitmq/cluster-operator.git
  2. Cloning into 'cluster-operator'...
  3. remote: Enumerating objects: 11892, done.
  4. remote: Counting objects: 100% (2279/2279), done.
  5. remote: Compressing objects: 100% (976/976), done.
  6. remote: Total 11892(delta 1438), reused 1976(delta 1240), pack-reused 9613
  7. Receiving objects: 100% (11892/11892), 11.95 MiB |1.66 MiB/s, done.
  8. Resolving deltas: 100% (7740/7740), done.

2.创建Operator

  1. # kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml# 创建[root@k8s-master rabbitmq]# kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  2. namespace/rabbitmq-system created
  3. customresourcedefinition.apiextensions.k8s.io/rabbitmqclusters.rabbitmq.com created
  4. serviceaccount/rabbitmq-cluster-operator created
  5. role.rbac.authorization.k8s.io/rabbitmq-cluster-leader-election-role created
  6. clusterrole.rbac.authorization.k8s.io/rabbitmq-cluster-operator-role created
  7. rolebinding.rbac.authorization.k8s.io/rabbitmq-cluster-leader-election-rolebinding created
  8. clusterrolebinding.rbac.authorization.k8s.io/rabbitmq-cluster-operator-rolebinding created
  9. deployment.apps/rabbitmq-cluster-operator created
  10. [root@k8s-master rabbitmq]# kubectl get all -n rabbitmq-system
  11. NAME READY STATUS RESTARTS AGE
  12. pod/rabbitmq-cluster-operator-7cbf865f89-s95gq 1/1 Running 0 33s
  13. NAME READY UP-TO-DATE AVAILABLE AGE
  14. deployment.apps/rabbitmq-cluster-operator 1/1 11 33s
  15. NAME DESIRED CURRENT READY AGE
  16. replicaset.apps/rabbitmq-cluster-operator-7cbf865f89 111 33s

3.修改配置

  1. # rabbitmq.yaml# cpu、memory# storageClassName: rook-ceph-block[root@k8s-master production-ready]# vim rabbitmq.yaml apiVersion: rabbitmq.com/v1beta1
  2. kind: RabbitmqCluster
  3. metadata:name: production-ready
  4. spec:replicas:3resources:requests:cpu:1memory: 2Gi
  5. limits:cpu:1memory: 2Gi
  6. rabbitmq:additionalConfig:|
  7. cluster_partition_handling = pause_minority
  8. vm_memory_high_watermark_paging_ratio = 0.99
  9. disk_free_limit.relative = 1.0
  10. collect_statistics_interval = 10000persistence:storageClassName: rook-ceph-block
  11. storage:"12Gi"

4.使用cluster-operator创建RabbitMQ集群

  1. [root@k8s-master production-ready]# pwd
  2. /k8s/middleware/rabbitmq/cluster-operator/docs/examples/production-ready
  3. [root@k8s-master production-ready]# ll
  4. total 16
  5. -rw-r--r-- 1 root root 199 Nov 3011:52 pod-disruption-budget.yaml
  6. -rw-r--r-- 1 root root 1193 Dec 110:28 rabbitmq.yaml
  7. -rw-r--r-- 1 root root 3134 Nov 3011:52 README.md
  8. -rw-r--r-- 1 root root 409 Nov 3011:52 ssd-gke.yaml
  9. # 创建[root@k8s-master production-ready]# kubectl apply -f rabbitmq.yaml
  10. rabbitmqcluster.rabbitmq.com/production-ready created
  11. [root@k8s-master production-ready]# [root@k8s-master production-ready]# kubectl apply -f pod-disruption-budget.yaml
  12. poddisruptionbudget.policy/production-ready-rabbitmq created
  13. # 查看[root@k8s-master production-ready]# kubectl get all -owide
  14. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  15. pod/production-ready-server-0 1/1 Running 0 50s 10.244.36.117 k8s-node1 <none><none>
  16. pod/production-ready-server-1 1/1 Running 0 50s 10.244.169.164 k8s-node2 <none><none>
  17. pod/production-ready-server-2 1/1 Running 0 50s 10.244.107.251 k8s-node3 <none><none>
  18. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  19. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 105d <none>
  20. service/production-ready ClusterIP 10.111.187.65 <none>5672/TCP,15672/TCP,15692/TCP 50s app.kubernetes.io/name=production-ready
  21. service/production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 50s app.kubernetes.io/name=production-ready
  22. NAME READY AGE CONTAINERS IMAGES
  23. statefulset.apps/production-ready-server 3/3 50s rabbitmq rabbitmq:3.8.21-management
  24. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  25. rabbitmqcluster.rabbitmq.com/production-ready True True 50s
  26. [root@k8s-master production-ready]# kubectl get all -n rabbitmq-system
  27. NAME READY STATUS RESTARTS AGE
  28. pod/rabbitmq-cluster-operator-7cbf865f89-s95gq 1/1 Running 0 105m
  29. NAME READY UP-TO-DATE AVAILABLE AGE
  30. deployment.apps/rabbitmq-cluster-operator 1/1 11 105m
  31. NAME DESIRED CURRENT READY AGE
  32. replicaset.apps/rabbitmq-cluster-operator-7cbf865f89 111 105m
  33. # PVC[root@k8s-master production-ready]# kubectl get pvc
  34. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  35. persistence-production-ready-server-0 Bound pvc-71d4c7b8-554c-4f9e-856e-325e80ed88f2 12Gi RWO rook-ceph-block 6m10s
  36. persistence-production-ready-server-1 Bound pvc-6be2c9b3-a0b0-43ae-af1f-74ae92bce5ce 12Gi RWO rook-ceph-block 6m10s
  37. persistence-production-ready-server-2 Bound pvc-2243a65e-f38e-4164-a2f1-ec0a65be486d 12Gi RWO rook-ceph-block 6m10s

3.获取用户名和密码

获取用户名和密码

  1. # user
  2. kubectl -n NAMESPACE get secret INSTANCE-default-user -ojsonpath="{.data.username}"| base64 --decode# pass
  3. kubectl -n NAMESPACE get secret INSTANCE-default-user -ojsonpath="{.data.password}"| base64 --decode# 获取获取用户名和密码 production-readyusername="$(kubectl get secret production-ready-default-user -ojsonpath='{.data.username}'| base64 --decode)"echo"username: $username"password="$(kubectl get secret production-ready-default-user -ojsonpath='{.data.password}'| base64 --decode)"echo"password: $password"[root@k8s-master production-ready]# username="$(kubectl get secret production-ready-default-user -o jsonpath='{.data.username}' | base64 --decode)"[root@k8s-master production-ready]# echo "username: $username"
  4. username: default_user_vmOg0aiF8VarwVhb0sE
  5. [root@k8s-master production-ready]# password="$(kubectl get secret production-ready-default-user -o jsonpath='{.data.password}' | base64 --decode)"[root@k8s-master production-ready]# echo "password: $password"
  6. password: rKr9N2v-Cxrec78zo1LUnigjyOla_YLv
  7. # 登录信息:
  8. username: default_user_vmOg0aiF8VarwVhb0sE
  9. password: rKr9N2v-Cxrec78zo1LUnigjyOla_YLv
  10. kubectl port-forward "service/production-ready"15672
  1. # 系统创建service# production-ready[root@k8s-master production-ready]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. production-ready ClusterIP 10.111.187.65 <none>5672/TCP,15672/TCP,15692/TCP 6m48s
  4. production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 6m48s
  5. [root@k8s-master production-ready]# kubectl port-forward "service/production-ready" 15672
  6. Forwarding from 127.0.0.1:15672 ->15672
  7. Forwarding from [::1]:15672 ->15672curl -u$username:$password localhost:15672/api/overview
  8. {"management_version":"3.8.9","rates_mode":"basic", ...}curl -udefault_user_vmOg0aiF8VarwVhb0sE:rKr9N2v-Cxrec78zo1LUnigjyOla_YLv 10.111.187.65:15672/api/overview
  9. # 访问[root@k8s-master production-ready]# curl -udefault_user_vmOg0aiF8VarwVhb0sE:rKr9N2v-Cxrec78zo1LUnigjyOla_YLv 10.111.187.65:15672/api/overview{"management_version":"3.8.21","rates_mode":"basic","sample_retention_policies":{"global":[600,3600,28800,86400],"basic":[600,3600],"detailed":[600]},"exchange_types":[{"name":"direct","description":"AMQP direct exchange, as per the AMQP specification","enabled":true},{"name":"fanout","description":"AMQP fanout exchange, as per the AMQP specification","enabled":true},{"name":"headers","description":"AMQP headers exchange, as per the AMQP specification","enabled":true},{"name":"topic","description":"AMQP topic exchange, as per the AMQP specification","enabled":true}],"product_version":"3.8.21","product_name":"RabbitMQ","rabbitmq_version":"3.8.21","cluster_name":"production-ready","erlang_version":"24.0.5","erlang_full_version":"Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:4:1] [ds:4:1:10] [async-threads:1] [jit]","disable_stats":false,"enable_queue_totals":false,"message_stats":{},"churn_rates":{"channel_closed":0,"channel_closed_details":{"rate":0.0},"channel_created":0,"channel_created_details":{"rate":0.0},"connection_closed":153,"connection_closed_details":{"rate":0.2},"connection_created":0,"connection_created_details":{"rate":0.0},"queue_created":0,"queue_created_details":{"rate":0.0},"queue_declared":0,"queue_declared_details":{"rate":0.0},"queue_deleted":0,"queue_deleted_details":{"rate":0.0}},"queue_totals":{},"object_totals":{"channels":0,"connections":0,"consumers":0,"exchanges":7,"queues":0},"statistics_db_event_queue":0,"node":"rabbit@production-ready-server-1.production-ready-nodes.default","listeners":[{"node":"rabbit@production-ready-server-0.production-ready-nodes.default","protocol":"amqp","ip_address":"::","port":5672,"socket_opts":{"backlog":128,"nodelay":true,"linger":[true,0],"exit_on_close":false}},{"node":"rabbit@production-ready-server-1.production-ready-nodes.default","protocol":"amqp","ip_address":"::","port":5672,"socket_opts":{"backlog":128,"nodelay":true,"linger":[true,0],"exit_on_close":false}},{"node":"rabbit@production-ready-server-2.production-ready-nodes.default","protocol":"amqp","ip_address":"::","port":5672,"socket_opts":{"backlog":128,"nodelay":true,"linger":[true,0],"exit_on_close":false}},{"node":"rabbit@production-ready-server-0.production-ready-nodes.default","protocol":"clustering","ip_address":"::","port":25672,"socket_opts":[]},{"node":"rabbit@production-ready-server-1.production-ready-nodes.default","protocol":"clustering","ip_address":"::","port":25672,"socket_opts":[]},{"node":"rabbit@production-ready-server-2.production-ready-nodes.default","protocol":"clustering","ip_address":"::","port":25672,"socket_opts":[]},{"node":"rabbit@production-ready-server-0.production-ready-nodes.default","protocol":"http","ip_address":"::","port":15672,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15672}},{"node":"rabbit@production-ready-server-1.production-ready-nodes.default","protocol":"http","ip_address":"::","port":15672,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15672}},{"node":"rabbit@production-ready-server-2.production-ready-nodes.default","protocol":"http","ip_address":"::","port":15672,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15672}},{"node":"rabbit@production-ready-server-0.production-ready-nodes.default","protocol":"http/prometheus","ip_address":"::","port":15692,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15692,"protocol":"http/prometheus"}},{"node":"rabbit@production-ready-server-1.production-ready-nodes.default","protocol":"http/prometheus","ip_address":"::","port":15692,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15692,"protocol":"http/prometheus"}},{"node":"rabbit@production-ready-server-2.production-ready-nodes.default","protocol":"http/prometheus","ip_address":"::","port":15692,"socket_opts":{"cowboy_opts":{"sendfile":false},"port":15692,"protocol":"http/prometheus"}}],"contexts":[{"ssl_opts":[],"node":"rabbit@production-ready-server-0.production-ready-nodes.default","description":"RabbitMQ Management","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15672"},{"ssl_opts":[],"node":"rabbit@production-ready-server-1.production-ready-nodes.default","description":"RabbitMQ Management","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15672"},{"ssl_opts":[],"node":"rabbit@production-ready-server-2.production-ready-nodes.default","description":"RabbitMQ Management","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15672"},{"ssl_opts":[],"node":"rabbit@production-ready-server-0.production-ready-nodes.default","description":"RabbitMQ Prometheus","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15692","protocol":"'http/prometheus'"},{"ssl_opts":[],"node":"rabbit@production-ready-server-1.production-ready-nodes.default","description":"RabbitMQ Prometheus","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15692","protocol":"'http/prometheus'"},{"ssl_opts":[],"node":"rabbit@production-ready-server-2.production-ready-nodes.default","description":"RabbitMQ Prometheus","path":"/","cowboy_opts":"[{sendfile,false}]","port":"15692","protocol":"'http/prometheus'"}]}[root@k8s-master production-ready]#

4.访问RabbitMQ管理UI

rabbitmq-cluster-svc.yaml

  1. # rabbitmq-cluster-svc.yamlapiVersion: v1
  2. kind: Service
  3. metadata:namespace: default
  4. name: rabbitmq-cluster-svc
  5. labels:app.kubernetes.io/name: production-ready
  6. spec:type: NodePort
  7. ports:-port:15672name: rabbitmq-http
  8. targetPort:15672nodePort:30672-port:5672name: rabbitmq-tcp
  9. targetPort:5672nodePort:30673selector:app.kubernetes.io/name: production-ready
  1. [root@k8s-master rabbitmq]# kubectl apply -f rabbitmq-svc.yaml
  2. service/rabbitmq-svc created
  3. [root@k8s-master rabbitmq]# kubectl get svc
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. production-ready ClusterIP 10.111.187.65 <none>5672/TCP,15672/TCP,15692/TCP 10m
  6. production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 10m
  7. rabbitmq-cluster-svc NodePort 10.106.36.141 <none>15672:30672/TCP,5672:30673/TCP 5s
  8. curl -udefault_user_vmOg0aiF8VarwVhb0sE:rKr9N2v-Cxrec78zo1LUnigjyOla_YLv 10.106.36.141:15672/api/overview
  1. # 地址
  2. http://172.51.216.81:30672
  3. username: default_user_vmOg0aiF8VarwVhb0sE
  4. password: rKr9N2v-Cxrec78zo1LUnigjyOla_YLv

在这里插入图片描述

5.扩容

修改配置

  1. # rabbitmq.yaml# cpu、memory# storageClassName: rook-ceph-block[root@k8s-master production-ready]# vim rabbitmq.yaml apiVersion: rabbitmq.com/v1beta1
  2. kind: RabbitmqCluster
  3. metadata:name: production-ready
  4. spec:replicas:4resources:requests:cpu:1memory: 2Gi
  5. limits:cpu:1memory: 2Gi
  6. rabbitmq:additionalConfig:|
  7. cluster_partition_handling = pause_minority
  8. vm_memory_high_watermark_paging_ratio = 0.99
  9. disk_free_limit.relative = 1.0
  10. collect_statistics_interval = 10000persistence:storageClassName: rook-ceph-block
  11. storage:"12Gi"
  1. [root@k8s-master production-ready]# kubectl apply -f rabbitmq.yaml
  2. rabbitmqcluster.rabbitmq.com/production-ready configured
  3. # 查看[root@k8s-master production-ready]# kubectl get all -owide
  4. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  5. pod/production-ready-server-0 1/1 Running 0 24m 10.244.36.117 k8s-node1 <none><none>
  6. pod/production-ready-server-1 1/1 Running 0 24m 10.244.169.164 k8s-node2 <none><none>
  7. pod/production-ready-server-2 1/1 Running 0 24m 10.244.107.251 k8s-node3 <none><none>
  8. pod/production-ready-server-3 1/1 Running 0 45s 10.244.169.159 k8s-node2 <none><none>
  9. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  10. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 105d <none>
  11. service/production-ready ClusterIP 10.111.187.65 <none>5672/TCP,15672/TCP,15692/TCP 24m app.kubernetes.io/name=production-ready
  12. service/production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 24m app.kubernetes.io/name=production-ready
  13. service/rabbitmq-cluster-svc NodePort 10.106.36.141 <none>15672:30672/TCP,5672:30673/TCP 14m app.kubernetes.io/name=production-ready
  14. NAME READY AGE CONTAINERS IMAGES
  15. statefulset.apps/production-ready-server 4/4 24m rabbitmq rabbitmq:3.8.21-management
  16. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  17. rabbitmqcluster.rabbitmq.com/production-ready True True 24m
  18. [root@k8s-master production-ready]# kubectl get pvc
  19. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  20. persistence-production-ready-server-0 Bound pvc-71d4c7b8-554c-4f9e-856e-325e80ed88f2 12Gi RWO rook-ceph-block 27m
  21. persistence-production-ready-server-1 Bound pvc-6be2c9b3-a0b0-43ae-af1f-74ae92bce5ce 12Gi RWO rook-ceph-block 27m
  22. persistence-production-ready-server-2 Bound pvc-2243a65e-f38e-4164-a2f1-ec0a65be486d 12Gi RWO rook-ceph-block 27m
  23. persistence-production-ready-server-3 Bound pvc-26fca32f-d0d9-44af-8378-1a2aeefb1d94 12Gi RWO rook-ceph-block 3m21s

在这里插入图片描述

6.缩容

修改配置

  1. # rabbitmq.yaml# cpu、memory# storageClassName: rook-ceph-block[root@k8s-master production-ready]# vim rabbitmq.yaml apiVersion: rabbitmq.com/v1beta1
  2. kind: RabbitmqCluster
  3. metadata:name: production-ready
  4. spec:replicas:3resources:requests:cpu:1memory: 2Gi
  5. limits:cpu:1memory: 2Gi
  6. rabbitmq:additionalConfig:|
  7. cluster_partition_handling = pause_minority
  8. vm_memory_high_watermark_paging_ratio = 0.99
  9. disk_free_limit.relative = 1.0
  10. collect_statistics_interval = 10000persistence:storageClassName: rook-ceph-block
  11. storage:"12Gi"
  1. [root@k8s-master production-ready]# kubectl apply -f rabbitmq.yaml
  2. rabbitmqcluster.rabbitmq.com/production-ready configured
  3. # 查看[root@k8s-master production-ready]# kubectl get all -owide
  4. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  5. pod/production-ready-server-0 1/1 Running 0 24m 10.244.36.117 k8s-node1 <none><none>
  6. pod/production-ready-server-1 1/1 Running 0 24m 10.244.169.164 k8s-node2 <none><none>
  7. pod/production-ready-server-2 1/1 Running 0 24m 10.244.107.251 k8s-node3 <none><none>
  8. pod/production-ready-server-3 1/1 Running 0 45s 10.244.169.159 k8s-node2 <none><none>
  9. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  10. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 105d <none>
  11. service/production-ready ClusterIP 10.111.187.65 <none>5672/TCP,15672/TCP,15692/TCP 24m app.kubernetes.io/name=production-ready
  12. service/production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 24m app.kubernetes.io/name=production-ready
  13. service/rabbitmq-cluster-svc NodePort 10.106.36.141 <none>15672:30672/TCP,5672:30673/TCP 14m app.kubernetes.io/name=production-ready
  14. NAME READY AGE CONTAINERS IMAGES
  15. statefulset.apps/production-ready-server 4/4 24m rabbitmq rabbitmq:3.8.21-management
  16. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  17. rabbitmqcluster.rabbitmq.com/production-ready True True 24m
  18. [root@k8s-master production-ready]# kubectl get pvc
  19. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  20. persistence-production-ready-server-0 Bound pvc-71d4c7b8-554c-4f9e-856e-325e80ed88f2 12Gi RWO rook-ceph-block 27m
  21. persistence-production-ready-server-1 Bound pvc-6be2c9b3-a0b0-43ae-af1f-74ae92bce5ce 12Gi RWO rook-ceph-block 27m
  22. persistence-production-ready-server-2 Bound pvc-2243a65e-f38e-4164-a2f1-ec0a65be486d 12Gi RWO rook-ceph-block 27m
  23. persistence-production-ready-server-3 Bound pvc-26fca32f-d0d9-44af-8378-1a2aeefb1d94 12Gi RWO rook-ceph-block 3m21s

在这里插入图片描述

缩容不起作用。

7.Spring Boot测试

7.1.服务配置

msa-k8s-rabbitmq.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:namespace: dev
  4. name: msa-k8s-rabbitmq
  5. labels:app: msa-k8s-rabbitmq
  6. spec:type: ClusterIP
  7. ports:-port:8143targetPort:8143selector:app: msa-k8s-rabbitmq
  8. ---apiVersion: apps/v1
  9. kind: Deployment
  10. metadata:namespace: dev
  11. name: msa-k8s-rabbitmq
  12. spec:replicas:3selector:matchLabels:app: msa-k8s-rabbitmq
  13. template:metadata:labels:app: msa-k8s-rabbitmq
  14. spec:imagePullSecrets:-name: harborsecret #对应创建私有镜像密钥Secretcontainers:-name: msa-k8s-redis
  15. image: 172.51.216.85:8888/springcloud/msa-k8s-rabbitmq:1.0.0
  16. imagePullPolicy: Always #如果省略imagePullPolicy,策略为IfNotPresentports:-containerPort:8143env:-name: ACTIVE
  17. value:"-Dspring.profiles.active=dev"

application-k8s.yml

  1. # Redis集群配置server:port:8143spring:application:name: msa-k8s-rabbitmq
  2. #配置rabbitMq 服务器rabbitmq:host: production-ready.default.svc.cluster.local
  3. port:5672username: default_user_IdbGSVeW1plrpskejhI
  4. password: mYaPNH_Hlb-g1rNYMiPWGdKex-3l8po5
  5. #虚拟host 可以不设置,使用server默认host#virtual-host: JCcccHost# 说明# 集群内部访问:
  6. production-ready
  7. #完整配置host: production-ready.default.svc.cluster.local
  8. # 或host: rabbitmq-cluster-svc.default.svc.cluster.local
  9. [root@k8s-master rabbitmq]# kubectl get svc
  10. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  11. production-ready ClusterIP 10.102.238.59 <none> 5672/TCP,15672/TCP,15692/TCP 89m
  12. production-ready-nodes ClusterIP None <none> 4369/TCP,25672/TCP 89m
  13. rabbitmq-cluster-svc NodePort 10.106.36.141 <none> 15672:30672/TCP,5672:30673/TCP 145m
  14. # 集群外部访问,需要每个实例配置一个service,暴露端口# 参考erver:port:8143spring:application:name: msa-k8s-rabbitmq
  15. rabbitmq:# 集群地址,用逗号分隔addresses: 192.168.11.71:5672,192.168.11.72:5672,192.168.11.71:5673connection-timeout:15000password: guest
  16. # 使用启用消息确认模式publisher-confirms:trueusername: guest
  17. virtual-host: /
  18. # 配置每个实例的service addresses: 192.168.11.71:5672,192.168.11.72:5672,192.168.11.71:5673
7.2.创建服务
  1. docker build -t172.51.216.85:8888/springcloud/msa-k8s-rabbitmq:1.1.0 .docker push 172.51.216.85:8888/springcloud/msa-k8s-rabbitmq:1.1.0
  2. [root@k8s-master operator]# kubectl apply -f msa-k8s-rabbitmq.yaml
  3. service/msa-k8s-redis created
  4. deployment.apps/msa-k8s-redis created
  5. root@k8s-master rabbitmq]# kubectl get all -n dev | grep msa-k8s-rabbitmq
  6. pod/msa-k8s-rabbitmq-84976bbd47-8mh6p 1/1 Running 0 8m25s
  7. pod/msa-k8s-rabbitmq-84976bbd47-9swjn 1/1 Running 0 8m25s
  8. pod/msa-k8s-rabbitmq-84976bbd47-nsqbt 1/1 Running 0 8m25s
  9. service/msa-k8s-rabbitmq ClusterIP 10.107.32.2 <none>8143/TCP 8m25s
  10. deployment.apps/msa-k8s-rabbitmq 3/3 33 8m25s
  11. replicaset.apps/msa-k8s-rabbitmq-84976bbd47 333 8m25s
  1. # 测试curl10.107.32.2:8143/send
  2. [root@k8s-master rabbitmq]# curl 10.107.32.2:8143/send
  3. 发送消息成功!
  4. [root@k8s-master rabbitmq]# curl 10.107.32.2:8143/send
  5. 发送消息成功!
  6. [root@k8s-master rabbitmq]# kubectl logs -f msa-k8s-rabbitmq-84976bbd47-8mh6p
  7. Error from server (NotFound): pods "msa-k8s-rabbitmq-84976bbd47-8mh6p" not found
  8. [root@k8s-master rabbitmq]# kubectl logs -f msa-k8s-rabbitmq-84976bbd47-8mh6p -n dev
  9. OpenJDK 64-Bit Server VM warning: ignoring option PermSize=512M; support was removed in8.0
  10. OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=512m; support was removed in8.0. ____ _ __ _ _
  11. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  12. ( ( )\___ | '_ |'_| | '_ \/ _` |\\\\\\/ ___)||_)|||||||(_||))))' |____| .__|_| |_|_| |_\__, | / / / /
  13. =========|_|==============|___/=/_/_/_/
  14. :: Spring Boot :: (v2.3.10.RELEASE)
  15. 2021-12-01 14:15:22.184 INFO 1 --- [ main] com.k8s.msa.MsaK8sRabbitmqApplication : Starting MsaK8sRabbitmqApplication v1.0.0 on msa-k8s-rabbitmq-84976bbd47-8mh6p with PID 1 (/app.jar started by root in /)
  16. 2021-12-01 14:15:22.189 INFO 1 --- [ main] com.k8s.msa.MsaK8sRabbitmqApplication : The following profiles are active: dev
  17. 2021-12-01 14:15:23.880 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8143 (http)
  18. 2021-12-01 14:15:23.896 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  19. 2021-12-01 14:15:23.896 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45]
  20. 2021-12-01 14:15:23.999 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  21. 2021-12-01 14:15:23.999 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1721 ms
  22. 2021-12-01 14:15:24.898 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  23. 2021-12-01 14:15:25.349 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8143 (http) with context path ''
  24. 2021-12-01 14:15:25.351 INFO 1 --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [production-ready.default.svc.cluster.local:5672]2021-12-01 14:15:25.447 INFO 1 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#185a6e9:0/SimpleConnection@6cd28fa7 [delegate=amqp://default_user_IdbGSVeW1plrpskejhI@10.102.238.59:5672/, localPort= 33022]2021-12-01 14:15:25.530 INFO 1 --- [ main] com.k8s.msa.MsaK8sRabbitmqApplication : Started MsaK8sRabbitmqApplication in4.14 seconds (JVM running for4.824)2021-12-01 14:16:30.850 INFO 1 --- [nio-8143-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring DispatcherServlet 'dispatcherServlet'2021-12-01 14:16:30.850 INFO 1 --- [nio-8143-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'2021-12-01 14:16:30.856 INFO 1 --- [nio-8143-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in6 ms
  25. 发送消息成功!:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:30 CST 2021
  26. 发送消息成功!:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:32 CST 2021
  27. 消费者消费消息了:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:32 CST 2021
  28. 发送消息成功!:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:35 CST 2021
  29. 消费者消费消息了:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:35 CST 2021
  30. 发送消息成功!:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:38 CST 2021
  31. 消费者消费消息了:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:40 CST 2021
  32. 发送消息成功!:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:41 CST 2021
  33. 消费者消费消息了:send,this is first messge! $$$ -----V2Wed Dec 01 14:16:44 CST 2021
7.3.总结

1.开发测试:

每个RabbitMQ实例单独配置service,以NodePort方式暴露端口

2.集群内服访问

  1. # Spring Boot# 集群内部访问:
  2. production-ready
  3. #完整配置
  4. host: production-ready.default.svc.cluster.local
  5. # 或
  6. host: rabbitmq-cluster-svc.default.svc.cluster.local

8.删除集群

  1. # 删除Rabbitmq[root@k8s-master production-ready]# pwd
  2. /k8s/middleware/rabbitmq/cluster-operator/docs/examples/production-ready
  3. [root@k8s-master production-ready]# ll
  4. total 16
  5. -rw-r--r-- 1 root root 199 Nov 3011:52 pod-disruption-budget.yaml
  6. -rw-r--r-- 1 root root 500 Dec 112:14 rabbitmq.yaml
  7. -rw-r--r-- 1 root root 3134 Nov 3011:52 README.md
  8. -rw-r--r-- 1 root root 409 Nov 3011:52 ssd-gke.yaml
  9. # 删除
  10. kubectl delete -f pod-disruption-budget.yaml
  11. kubectl delete -f rabbitmq.yaml
  12. # 删除operator
  13. kubectl delete -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  1. # 查看[root@k8s-master production-ready]# kubectl get all -n rabbitmq-system
  2. NAME READY STATUS RESTARTS AGE
  3. pod/rabbitmq-cluster-operator-7cbf865f89-s95gq 1/1 Running 0 4h31m
  4. NAME READY UP-TO-DATE AVAILABLE AGE
  5. deployment.apps/rabbitmq-cluster-operator 1/1 11 4h31m
  6. NAME DESIRED CURRENT READY AGE
  7. replicaset.apps/rabbitmq-cluster-operator-7cbf865f89 111 4h31m
  8. [root@k8s-master production-ready]# kubectl get all
  9. NAME READY STATUS RESTARTS AGE
  10. pod/production-ready-server-0 1/1 Running 0 102m
  11. pod/production-ready-server-1 1/1 Running 0 102m
  12. pod/production-ready-server-2 1/1 Running 0 102m
  13. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  14. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 106d
  15. service/production-ready ClusterIP 10.102.238.59 <none>5672/TCP,15672/TCP,15692/TCP 102m
  16. service/production-ready-nodes ClusterIP None <none>4369/TCP,25672/TCP 102m
  17. service/rabbitmq-cluster-svc NodePort 10.106.36.141 <none>15672:30672/TCP,5672:30673/TCP 158m
  18. NAME READY AGE
  19. statefulset.apps/production-ready-server 3/3 102m
  20. NAME ALLREPLICASREADY RECONCILESUCCESS AGE
  21. rabbitmqcluster.rabbitmq.com/production-ready True True 102m
  22. [root@k8s-master production-ready]# kubectl get pvc
  23. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  24. persistence-production-ready-server-0 Bound pvc-0c3c4767-669c-497e-a6a7-c6ee4b6ff283 12Gi RWO rook-ceph-block 102m
  25. persistence-production-ready-server-1 Bound pvc-8ecc6b70-26ff-483d-a456-2325dd4376b3 12Gi RWO rook-ceph-block 102m
  26. persistence-production-ready-server-2 Bound pvc-6a9f3349-0396-46c1-a7a9-7fd8ad9c218b 12Gi RWO rook-ceph-block 102m
  27. # 删除# 删除svc[root@k8s-master rabbitmq]# kubectl delete -f rabbitmq-cluster-svc.yaml service"rabbitmq-cluster-svc" deleted
  28. # 删除rabbitmq[root@k8s-master production-ready]# kubectl delete -f pod-disruption-budget.yaml
  29. poddisruptionbudget.policy "production-ready-rabbitmq" deleted
  30. [root@k8s-master production-ready]# kubectl delete -f rabbitmq.yaml
  31. rabbitmqcluster.rabbitmq.com "production-ready" deleted
  32. [root@k8s-master rabbitmq]# kubectl get all
  33. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  34. service/kubernetes ClusterIP 10.96.0.1 <none>443/TCP 105d
  35. # 删除operator[root@k8s-master rabbitmq]# kubectl delete -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
  36. namespace "rabbitmq-system" deleted
  37. customresourcedefinition.apiextensions.k8s.io "rabbitmqclusters.rabbitmq.com" deleted
  38. serviceaccount "rabbitmq-cluster-operator" deleted
  39. role.rbac.authorization.k8s.io "rabbitmq-cluster-leader-election-role" deleted
  40. clusterrole.rbac.authorization.k8s.io "rabbitmq-cluster-operator-role" deleted
  41. rolebinding.rbac.authorization.k8s.io "rabbitmq-cluster-leader-election-rolebinding" deleted
  42. clusterrolebinding.rbac.authorization.k8s.io "rabbitmq-cluster-operator-rolebinding" deleted
  43. deployment.apps "rabbitmq-cluster-operator" deleted
  44. [root@k8s-master rabbitmq]# kubectl get all -n rabbitmq-system
  45. No resources found in rabbitmq-system namespace.
  46. [root@k8s-master rabbitmq]# kubectl get pvc
  47. No resources found in default namespace.
  • Kubernetes
  1. # Kubernetes
  2. https://iothub.org.cn/docs/kubernetes/
  3. https://iothub.org.cn/docs/kubernetes/middleware/rabbitmq/

本文转载自: https://blog.csdn.net/iiothub/article/details/136020866
版权归原作者 IoTHub - 物联网开源技术社区 所有, 如有侵权,请联系我们删除。

“Kubernetes 部署 RabbitMQ”的评论:

还没有评论