Kubernetes上搭建高可用RabbitMQ

部署介绍

在Kubernetes上搭建RabbitMQ有4种部署方法(IP模式、Pod与server的DNS、Statefulset 与Headless Service、hostname模式),以下是通过hostname模式来部署,经过多少测试使用yaml部署高可用RabbitMQ如下:

1.hostname模式部署yaml文件:

  1 apiVersion: v1
  2 kind: Namespace
  3 metadata:
  4   name: rabbitmq
  5 ---
  6 apiVersion: v1
  7 kind: ServiceAccount
  8 metadata:
  9   name: rabbitmq 
 10   namespace: rabbitmq 
 11 ---
 12 kind: Role
 13 apiVersion: rbac.authorization.k8s.io/v1beta1
 14 metadata:
 15   name: endpoint-reader
 16   namespace: rabbitmq 
 17 rules:
 18 - apiGroups: [""]
 19   resources: ["endpoints"]
 20   verbs: ["get"]
 21 ---
 22 kind: RoleBinding
 23 apiVersion: rbac.authorization.k8s.io/v1beta1
 24 metadata:
 25   name: endpoint-reader
 26   namespace: rabbitmq
 27 subjects:
 28 - kind: ServiceAccount
 29   name: rabbitmq
 30 roleRef:
 31   apiGroup: rbac.authorization.k8s.io
 32   kind: Role
 33   name: endpoint-reader
 34 ---
 35 apiVersion: v1
 36 kind: PersistentVolume
 37 metadata:
 38     name: rabbitmq-data
 39     labels:
 40       release: rabbitmq-data
 41     namespace: rabbitmq
 42 spec:
 43     capacity:
 44       storage: 2Gi
 45     accessModes:
 46       - ReadWriteMany
 47     persistentVolumeReclaimPolicy: Retain
 48     nfs:
 49       path: /opt/rabbitmq
 50       server: 192.168.163.130  
 51 ---
 52 apiVersion: v1
 53 kind: PersistentVolumeClaim
 54 metadata:
 55   name: rabbitmq-data-claim
 56   namespace: rabbitmq
 57 spec:
 58   accessModes:
 59     - ReadWriteMany
 60   resources:  
 61     requests:
 62       storage: 2Gi
 63   selector:
 64     matchLabels:
 65       release: rabbitmq-data
 66 ---
 67 kind: Service
 68 apiVersion: v1
 69 metadata:
 70   name: rabbitmq-headless
 71   namespace: rabbitmq
 72 spec:
 73   clusterIP: None
 74   publishNotReadyAddresses: true 
 75   ports: 
 76    - name: amqp
 77      port: 5672
 78    - name: http
 79      port: 15672
 80   selector:
 81     app: rabbitmq
 82 ---
 83 kind: Service
 84 apiVersion: v1
 85 metadata:
 86   namespace: rabbitmq
 87   name: rabbitmq-service
 88 spec:
 89   ports:
 90   - name: http
 91     protocol: TCP
 92     port: 15672
 93     targetPort: 15672 
 94     nodePort: 32001   
 95   - name: amqp
 96     protocol: TCP
 97     port: 5672
 98     targetPort: 5672  
 99 nodeport
100     nodePort: 32002
101   selector:
102     app: rabbitmq
103   type: NodePort
104 ---
105 apiVersion: v1
106 kind: ConfigMap
107 metadata:
108   name: rabbitmq-config
109   namespace: rabbitmq
110 data:
111   enabled_plugins: |
112       [rabbitmq_management,rabbitmq_peer_discovery_k8s].
113   rabbitmq.conf: |
114       cluster_formation.peer_discovery_backend  = rabbit_peer_discovery_k8s
115       cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
116       cluster_formation.k8s.address_type = hostname
117       cluster_formation.node_cleanup.interval = 10
118       cluster_formation.node_cleanup.only_log_warning = true
119       cluster_partition_handling = autoheal
120       queue_master_locator=min-masters
121       loopback_users.guest = false
122 
123       cluster_formation.randomized_startup_delay_range.min = 0
124       cluster_formation.randomized_startup_delay_range.max = 2
125       cluster_formation.k8s.service_name = rabbitmq-headless
126       cluster_formation.k8s.hostname_suffix = .rabbitmq-headless.rabbitmq.svc.cluster.local
127       vm_memory_high_watermark.absolute = 1GB
128       disk_free_limit.absolute = 2GB
129 ---
130 apiVersion: apps/v1
131 kind: StatefulSet
132 metadata:
133   name: rabbitmq
134   namespace: rabbitmq
135 spec:
136   serviceName: rabbitmq-headless   
137   selector:
138     matchLabels:
139       app: rabbitmq  
140   replicas: 2
141   template:
142     metadata:
143       labels:
144         app: rabbitmq
145       annotations:
146         scheduler.alpha.kubernetes.io/affinity: >
147             {
148               "podAntiAffinity": {
149                 "requiredDuringSchedulingIgnoredDuringExecution": [{
150                   "labelSelector": {
151                     "matchExpressions": [{
152                       "key": "app",
153                       "operator": "In",
154                       "values": ["rabbitmq"]
155                     }]
156                   },
157                   "topologyKey": "kubernetes.io/hostname"
158                 }]
159               }
160             }
161     spec:
162       serviceAccountName: rabbitmq
163       terminationGracePeriodSeconds: 10
164       containers:        
165       - name: rabbitmq
166         image: 192.168.163.129:60080/rabbitmq:3.7.19-management
167         resources:
168           limits:
169             cpu: 1
170             memory: 1Gi
171           requests:
172             cpu: 1
173             memory: 1Gi
174         volumeMounts:
175           - name: config-volume
176             mountPath: /etc/rabbitmq
177           - name: rabbitmq-data
178             mountPath: /var/lib/rabbitmq/mnesia
179         ports:
180           - name: http
181             protocol: TCP
182             containerPort: 15672
183           - name: amqp
184             protocol: TCP
185             containerPort: 5672
186         livenessProbe:
187           exec:
188             command: ["rabbitmqctl", "status"]
189           initialDelaySeconds: 60
190           periodSeconds: 60
191           timeoutSeconds: 5
192         readinessProbe:
193           exec:
194             command: ["rabbitmqctl", "status"]
195           initialDelaySeconds: 20
196           periodSeconds: 60
197           timeoutSeconds: 5
198         imagePullPolicy: Always
199         env:
200           - name: HOSTNAME
201             valueFrom:
202               fieldRef:
203                 fieldPath: metadata.name
204           - name: RABBITMQ_USE_LONGNAME
205             value: "true"
206           - name: RABBITMQ_NODENAME
207             value: "rabbit@$(HOSTNAME).rabbitmq-headless.rabbitmq.svc.cluster.local"
208           - name: RABBITMQ_ERLANG_COOKIE
209             value: "mycookie"       
210       volumes:
211         - name: config-volume
212           configMap:
213             name: rabbitmq-config
214             items:
215             - key: rabbitmq.conf
216               path: rabbitmq.conf
217             - key: enabled_plugins
218               path: enabled_plugins
219         - name: rabbitmq-data
220           persistentVolumeClaim:
221             claimName: rabbitmq-data-claim 

 

posted @ 2020-03-28 11:10  老村长de  阅读(893)  评论(0编辑  收藏  举报