阿里云ack部署rabbitmq集群
1、创建持久化存储pv和pvc,我整理使用的是阿里云的NAS。
这里需要注意的是:
- 设置下子目录,避免共用 NAS 时,文件组织混乱。
- 给子目录分配权限,我这里设置的是777。
2、创建服务部署的yaml文件。
登录后复制
cat mq-Account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: rabbitmq
namespace: service-rabbitmq
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: rabbitmq
namespace: service-rabbitmq
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: rabbitmq
namespace: service-rabbitmq
subjects:
- kind: ServiceAccount
name: rabbitmq
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: rabbitmq
cat mq-Configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config
namespace: service-rabbitmq
data:
rabbitmq.conf: |
cluster_formation.peer_discovery_backend = k8s
cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
cluster_formation.k8s.address_type = hostname
cluster_formation.k8s.service_name = rabbitmq-headless
queue_master_locator=min-masters
enabled_plugins: |
[rabbitmq_management,rabbitmq_peer_discovery_k8s,rabbitmq_delayed_message_exchange].
# 按需启用插件
cat mq-HeadlessService.yaml
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-headless
namespace: service-rabbitmq
spec:
clusterIP: None
ports:
- name: epmd
port: 4369
protocol: TCP
targetPort: 4369
- name: cluster-links
port: 25672
protocol: TCP
targetPort: 25672
selector:
app: rabbitmq
sessionAffinity: None
type: ClusterIP
cat mq-Service.yaml
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-external
namespace: service-rabbitmq
spec:
ports:
- name: http
port: 15672
protocol: TCP
targetPort: 15672
- name: amqp
port: 5672
protocol: TCP
targetPort: 5672
selector:
app: rabbitmq
sessionAffinity: None
type: ClusterIP
cat mq-Service.yaml
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-external
namespace: service-rabbitmq
spec:
ports:
- name: http
port: 15672
protocol: TCP
targetPort: 15672
- name: amqp
port: 5672
protocol: TCP
targetPort: 5672
selector:
app: rabbitmq
sessionAffinity: None
type: ClusterIP
[root@jumpserver test1]# cat mq-StatefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: rabbitmq-cluster
namespace: service-rabbitmq
spec:
replicas: 3
selector:
matchLabels:
app: rabbitmq
serviceName: rabbitmq-headless
template:
metadata:
labels:
app: rabbitmq
spec:
serviceAccountName: rabbitmq
securityContext:
fsGroup: 999
runAsUser: 999
runAsGroup: 999
volumes:
- name: config-volume
configMap:
name: rabbitmq-config
items:
- key: rabbitmq.conf
path: "rabbitmq.conf"
- key: enabled_plugins
path: "enabled_plugins"
- name: rabbitmq-config-rw
emptyDir: {}
- name: rabbitmq-data
persistentVolumeClaim:
claimName: nas-service-rabbitmq-pvc
initContainers:
- name: init
image: harbor.hkfuliao.com/library/busybox:latest
volumeMounts:
- name: config-volume
mountPath: /tmp/rabbitmq
- name: rabbitmq-config-rw
mountPath: /etc/rabbitmq
command:
- sh
- -c
- cp /tmp/rabbitmq/rabbitmq.conf /etc/rabbitmq/rabbitmq.conf && echo '' >> /etc/rabbitmq/rabbitmq.conf;
cp /tmp/rabbitmq/enabled_plugins /etc/rabbitmq/enabled_plugins
containers:
- name: rabbitmq
image: harbor.hkfuliao.com/service-rabbitmq/rabbitmq:3.9.13-management
livenessProbe:
exec:
command: ["rabbitmq-diagnostics", "status"]
initialDelaySeconds: 60
periodSeconds: 60
timeoutSeconds: 15
readinessProbe:
exec:
command: ["rabbitmq-diagnostics", "ping"]
initialDelaySeconds: 60
periodSeconds: 60
timeoutSeconds: 10
ports:
- name: amqp
containerPort: 5672
protocol: TCP
- name: management-http
containerPort: 15672
protocol: TCP
- name: epmd
containerPort: 4369
protocol: TCP
resources:
limits:
memory: 2Gi
requests:
memory: 2Gi
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name # get pod.metadata.name, e.g. rabbitmq-cluster-0
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace # get pod.metadata.namespace
- name: RABBITMQ_DEFAULT_USER
value: "admin"
- name: RABBITMQ_DEFAULT_PASS
value: "admin@123"
- name: RABBITMQ_USE_LONGNAME
value: "true"
- name: K8S_SERVICE_NAME
value: "rabbitmq-headless"
- 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: "91/rHX2a3GZw3RCHT1Q9y/G0Wo3cbX3qS06DyD4fAUs=" # generator by: echo $(openssl rand -base64 32)
volumeMounts:
- name: rabbitmq-config-rw
mountPath: "/etc/rabbitmq"
- name: rabbitmq-data
mountPath: "/var/lib/rabbitmq/mnesia"
3、创建ingress路由,15672端口映射到指定域名上,然后DNS解析到此ingress的公网IP即可