rabbitmq-cluster-template.yaml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. apiVersion: v1
  2. kind: Template
  3. metadata:
  4. name: rabbitmq-cluster
  5. annotations:
  6. description: "Deploys a RabbitMQ cluster"
  7. iconClass: icon-rabbitmq
  8. tags: rabbitmq,mq,messaging
  9. parameters:
  10. - name: NAMESPACE
  11. description: "OpenShift project (current namespace)"
  12. required: true
  13. - name: CLUSTER_NAME
  14. description: "Name of the RabbitMQ cluster"
  15. value: rabbitmq-cluster
  16. - name: ISTAG
  17. description: "Image to deploy"
  18. value: rabbitmq:3.7-management
  19. - name: RABBITMQ_USER
  20. description: "Username for the RabbitMQ instance"
  21. value: rabbitmq
  22. - name: RABBITMQ_PASS
  23. description: "Password securing the RabbitMQ instance"
  24. generate: expression
  25. from: "[a-zA-Z0-9]{16}"
  26. - name: ERLANG_COOKIE
  27. description: "Cookie used for authentication of cluster nodes"
  28. generate: expression
  29. from: "[a-zA-Z0-9]{16}"
  30. - name: SERVICE_ACCOUNT
  31. description: "Name of the service account used by RabbitMQ k8s plugin"
  32. value: rabbitmq-discovery
  33. - name: VOLUME_SIZE
  34. description: "Size of the RabbitMQ data volume"
  35. value: 1Gi
  36. objects:
  37. # This service account is needed for rabbit_peer_discovery_k8s plugin to be able to discover
  38. # cluster nodes
  39. - apiVersion: v1
  40. kind: ServiceAccount
  41. metadata:
  42. name: ${SERVICE_ACCOUNT}
  43. - apiVersion: v1
  44. kind: RoleBinding
  45. metadata:
  46. name: ${SERVICE_ACCOUNT}-view
  47. roleRef:
  48. kind: Role
  49. name: view
  50. subjects:
  51. - kind: ServiceAccount
  52. name: ${SERVICE_ACCOUNT}
  53. - apiVersion: v1
  54. kind: Secret
  55. stringData:
  56. username: ${RABBITMQ_USER}
  57. password: ${RABBITMQ_PASS}
  58. url: "amqp://${RABBITMQ_USER}:${RABBITMQ_PASS}@${CLUSTER_NAME}-balancer"
  59. cookie: ${ERLANG_COOKIE}
  60. metadata:
  61. name: ${CLUSTER_NAME}-secret
  62. type: Opaque
  63. - apiVersion: v1
  64. kind: ConfigMap
  65. metadata:
  66. name: ${CLUSTER_NAME}-config
  67. data:
  68. rabbitmq.conf: |
  69. loopback_users.guest = false
  70. ## Clustering
  71. cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
  72. cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
  73. cluster_formation.k8s.address_type = hostname
  74. cluster_formation.k8s.hostname_suffix = .${CLUSTER_NAME}.${NAMESPACE}.svc.cluster.local
  75. cluster_formation.node_cleanup.interval = 10
  76. cluster_formation.node_cleanup.only_log_warning = true
  77. cluster_partition_handling = autoheal
  78. ## queue master locator
  79. queue_master_locator=min-masters
  80. enabled_plugins: |
  81. [rabbitmq_management,rabbitmq_peer_discovery_k8s].
  82. # Load balancer
  83. - kind: Service
  84. apiVersion: v1
  85. metadata:
  86. name: ${CLUSTER_NAME}-balancer
  87. labels:
  88. app: ${CLUSTER_NAME}
  89. type: LoadBalancer
  90. spec:
  91. type: ClusterIP
  92. ports:
  93. - name: http
  94. protocol: TCP
  95. port: 15672
  96. targetPort: 15672
  97. - name: amqp
  98. protocol: TCP
  99. port: 5672
  100. targetPort: 5672
  101. selector:
  102. app: ${CLUSTER_NAME}
  103. # Headless service that makes it possible to lookup individual rabbitmq nodes
  104. - apiVersion: v1
  105. kind: Service
  106. metadata:
  107. name: ${CLUSTER_NAME}
  108. labels:
  109. app: ${CLUSTER_NAME}
  110. spec:
  111. selector:
  112. app: ${CLUSTER_NAME}
  113. clusterIP: None
  114. ports:
  115. - name: amqp
  116. port: 5672
  117. targetPort: 5672
  118. - apiVersion: apps/v1beta1
  119. kind: StatefulSet
  120. metadata:
  121. name: ${CLUSTER_NAME}
  122. labels:
  123. app: ${CLUSTER_NAME}
  124. spec:
  125. serviceName: ${CLUSTER_NAME}
  126. replicas: 2
  127. selector:
  128. matchLabels:
  129. app: ${CLUSTER_NAME}
  130. template:
  131. metadata:
  132. labels:
  133. app: ${CLUSTER_NAME}
  134. spec:
  135. serviceAccountName: ${SERVICE_ACCOUNT}
  136. terminationGracePeriodSeconds: 30
  137. containers:
  138. - name: rabbitmq
  139. command:
  140. - sh
  141. args:
  142. - -c
  143. - cp -v /etc/rabbitmq/rabbitmq.conf ${RABBITMQ_CONFIG_FILE}; exec docker-entrypoint.sh rabbitmq-server
  144. image: ${ISTAG}
  145. imagePullPolicy: IfNotPresent
  146. volumeMounts:
  147. - name: config-volume
  148. mountPath: /etc/rabbitmq
  149. - name: rabbitmq-storage
  150. mountPath: /var/lib/rabbitmq
  151. ports:
  152. - name: http
  153. protocol: TCP
  154. containerPort: 15672
  155. - name: amqp
  156. protocol: TCP
  157. containerPort: 5672
  158. livenessProbe:
  159. exec:
  160. command: ["rabbitmqctl", "status"]
  161. initialDelaySeconds: 30
  162. timeoutSeconds: 10
  163. readinessProbe:
  164. exec:
  165. command: ["rabbitmqctl", "status"]
  166. initialDelaySeconds: 10
  167. timeoutSeconds: 10
  168. env:
  169. - name: RABBITMQ_DEFAULT_USER
  170. valueFrom:
  171. secretKeyRef:
  172. name: ${CLUSTER_NAME}-secret
  173. key: username
  174. - name: RABBITMQ_DEFAULT_PASS
  175. valueFrom:
  176. secretKeyRef:
  177. name: ${CLUSTER_NAME}-secret
  178. key: password
  179. - name: RABBITMQ_ERLANG_COOKIE
  180. valueFrom:
  181. secretKeyRef:
  182. name: ${CLUSTER_NAME}-secret
  183. key: cookie
  184. - name: K8S_SERVICE_NAME
  185. value: ${CLUSTER_NAME}
  186. - name: POD_IP
  187. valueFrom:
  188. fieldRef:
  189. fieldPath: status.podIP
  190. - name: POD_NAME
  191. valueFrom:
  192. fieldRef:
  193. fieldPath: metadata.name
  194. - name: POD_NAMESPACE
  195. valueFrom:
  196. fieldRef:
  197. fieldPath: metadata.namespace
  198. - name: RABBITMQ_USE_LONGNAME
  199. value: "true"
  200. - name: RABBITMQ_NODENAME
  201. value: "rabbit@$(POD_NAME).${CLUSTER_NAME}.$(POD_NAMESPACE).svc.cluster.local"
  202. - name: RABBITMQ_CONFIG_FILE
  203. value: /var/lib/rabbitmq/rabbitmq.conf
  204. volumes:
  205. - name: config-volume
  206. configMap:
  207. name: ${CLUSTER_NAME}-config
  208. items:
  209. - key: rabbitmq.conf
  210. path: rabbitmq.conf
  211. - key: enabled_plugins
  212. path: enabled_plugins
  213. volumeClaimTemplates:
  214. - metadata:
  215. name: rabbitmq-storage
  216. spec:
  217. accessModes: [ "ReadWriteOnce" ]
  218. resources:
  219. requests:
  220. storage: ${VOLUME_SIZE}