K8S安装rabbitmq集群
作者:互联网
踩了无数的坑,终于弄出比较准确简便的部署方式了
目前我这套部署方式参考的是 github:https://github.com/rabbitmq/rabbitmq-peer-discovery-k8s
然后K8S使用的镜像是rabbitmq:3.8,然后我这边所有的操作都是基于default空间。还有就是我粘贴上来的yaml,你们那边复制过去 可能有些不是很整齐,自己注意调试调试
第一步:创建rabbitmq的集群访问角色
apiVersion: v1
kind: ServiceAccount #集群访问apiserver的凭证
metadata:
name: rabbitmq
---
kind: Role #创建sa角色
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: rabbitmq
rules:
- apiGroups:
- ""
resources:
- endpoints
verbs:
- get
---
kind: RoleBinding #将角色绑定
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: rabbitmq
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: rabbitmq
subjects:
- kind: ServiceAccount
name: rabbitmq
-----------------------------------------------------------------------------------------------------------------
第二步:挂载rabbitmq的configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config #配置文件名字,可自己修改
data:
enabled_plugins: |
[rabbitmq_management,rabbitmq_peer_discovery_k8s]. ###这里是启动的插件,特别是第二个插件,rabbitmq低版本是没有的,启动就会报找不到
rabbitmq.conf: |
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
cluster_formation.k8s.address_type = hostname
cluster_formation.node_cleanup.interval = 30
cluster_formation.node_cleanup.only_log_warning = true
cluster_partition_handling = autoheal
queue_master_locator=min-masters
loopback_users.guest = false
-----------------------------------------------------------------------------------------------------------------
第三步:创建rabbitmq使用的pv-pvc
apiVersion: v1
kind: PersistentVolume
metadata:
name: rabbitmq-pv #创建的pv名称可创建多个.
spec:
capacity:
storage: 6Gi #创建的pv容量为1G
accessModes:
- ReadWriteMany #pv的访问模式:可读可写可挂在多个节点
persistentVolumeReclaimPolicy: Retain #回收策略,永不回收
nfs: #创建的pv数据来源
path: /nfs/mq #数据源目录
server: 192.168.0.175 #数据源ip
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: rabbitmq
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 6Gi
-----------------------------------------------------------------------------------------------------------------
第四步:创建service
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-management
labels:
app: rabbitmq
spec:
ports:
- port: 15672
name: http
nodePort: 32001 #集群外访问rabbitmq管理web界面,http://nodeip:32001,自己想要什么端口自己修改
- port: 5672
name: amqp
nodePort: 32002
selector:
app: rabbitmq
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: rabbitmq
labels:
app: rabbitmq
spec:
clusterIP: None
ports:
- port: 5672
name: amqp
selector:
app: rabbitmq
-----------------------------------------------------------------------------------------------------------------
最后一步创建有状态pod
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: rabbitmq
spec:
serviceName: rabbitmq
replicas: 3
selector:
matchLabels:
app: rabbitmq
template:
metadata:
labels:
app: rabbitmq
spec:
imagePullSecrets:
- name: alikey #这里根据自己的情况,docker仓库需要认证下载的话就配置,不需要就删掉
serviceAccountName: rabbitmq
terminationGracePeriodSeconds: 10
containers:
- name: rabbitmq
image: rabbitmq:3.8
volumeMounts:
- name: config-volume
mountPath: /etc/rabbitmq #配置文件
- name: nfs-volume
mountPath: /var/lib/rabbitmq/ #数据持久化
ports:
- name: http
protocol: TCP
containerPort: 15672
- name: amqp
protocol: TCP
containerPort: 5672
livenessProbe:
exec:
command: ["rabbitmq-diagnostics", "status"] #检活的,,,我也是复制的
initialDelaySeconds: 60
periodSeconds: 60
timeoutSeconds: 15
readinessProbe:
exec:
command: ["rabbitmq-diagnostics", "ping"] #检活的,,,我也是复制的
initialDelaySeconds: 20
periodSeconds: 60
timeoutSeconds: 10
imagePullPolicy: IfNotPresent
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: RABBITMQ_USE_LONGNAME
value: "true"
- name: K8S_SERVICE_NAME
value: rabbitmq
- name: RABBITMQ_NODENAME
value: rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
- name: K8S_HOSTNAME_SUFFIX
value: .$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local
- name: RABBITMQ_ERLANG_COOKIE
value: "mycookie" #这里也不用管
volumes:
- name: config-volume
configMap:
name: rabbitmq-config
items:
- key: rabbitmq.conf #配置文件
path: rabbitmq.conf
- key: enabled_plugins
path: enabled_plugins #这里就是我上面说的如果版本低了,,那个插件就找不到
- name: nfs-volume
persistentVolumeClaim:
claimName: rabbitmq
----------------------------------------------------------------------------------------
以上基本自己根据自己的情况,改改就很好搭建出mq集群了,性能方面我没测,数据持久化倒是测试了,没问题。然后我也参照有的教程用什么erlang.cookie的方式,然后自己打docker镜像什么的,,,都不方面,而且各种坑都要踩踩(可能自己很菜的问题)。
然后这个集群默认账号密码是guest/guest ,是个admin账号。集群搭建完后自己去图形化界面添加个管理员,然后把这个guest干掉就OK了。。
标签:k8s,name,v1kind,apiVersion,cluster,集群,rabbitmq,K8S 来源: https://www.cnblogs.com/2019peng/p/14232037.html