在k8s中创建一个rabbitmq 需要用到configmap\statefulset\集群相关的svc\管理页面访问相关的svc,然后还需要一个serviceaccount提供访问node节点上ep的权限。
创建serviceaccount
role\rolebinding\serviceaccount创建
kubectl -n default create role endpoint-reader --verb=get --resource=endpoints
kubectl -n default create serviceaccount rabbitmq
kubectl -n default create rolebinding endpoint-reader --role=endpoint-reader --serviceaccount=default:rabbitmq
sts\configmq\svc创建
#rabbitmq-cluster-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-cluster
namespace: default
labels:
app: rabbitmq-cluster
type: LoadBalancer
spec:
ports:
- name: http
protocol: TCP
port: 15672
targetPort: 15672
- name: amqp
protocol: TCP
port: 5672
targetPort: 5672
selector:
app: rabbitmq
clusterIP: None
type: ClusterIP
sessionAffinity: None
publishNotReadyAddresses: true
---
#rabbitmq-lb-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-loadbalancer
namespace: default
labels:
app: rabbitmq
spec:
ports:
- name: management
protocol: TCP
port: 15672
targetPort: 15672
- name: mqservice
protocol: TCP
port: 5672
targetPort: 5672
selector:
app: rabbitmq
type: NodePort
sessionAffinity: None
externalTrafficPolicy: Cluster
---
#rabbitmq-configmp.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config
namespace: default
selfLink: /api/v1/namespaces/default/configmaps/rabbitmq-config
uid: ac4801c3-2e12-4fc6-b1fb-c0d2b51c6d96
resourceVersion: '103920685'
creationTimestamp: '2021-03-29T08:19:53Z'
annotations:
kubectl.kubernetes.io/last-applied-configuration: >
{"apiVersion":"v1","data":{"enabled_plugins":"[rabbitmq_management,rabbitmq_peer_discovery_k8s].\n","rabbitmq.conf":"##
Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to
learn more.\ncluster_formation.peer_discovery_backend =
rabbit_peer_discovery_k8s\ncluster_formation.k8s.host =
kubernetes.default.svc.cluster.local\n## Should RabbitMQ node name be
computed from the pod's hostname or IP address?\n## IP addresses are not
stable, so using [stable] hostnames is recommended when possible.\n## Set
to \"hostname\" to use pod hostnames.\n## When this value is changed, so
should the variable used to set the RABBITMQ_NODENAME\n## environment
variable.\ncluster_formation.k8s.address_type = hostname\n## How often
should node cleanup checks run?\ncluster_formation.node_cleanup.interval =
30\n## Set to false if automatic removal of unknown/absent nodes\n## is
desired. This can be dangerous, see\n## *
https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup\n##
*
https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ\ncluster_formation.node_cleanup.only_log_warning
= true\ncluster_partition_handling = autoheal\n## See
https://www.rabbitmq.com/ha.html#master-migration-data-locality\nqueue_master_locator=min-masters\n##
See
https://www.rabbitmq.com/access-control.html#loopback-users\nloopback_users.guest
= false\n\ncluster_formation.randomized_startup_delay_range.min =
0\ncluster_formation.randomized_startup_delay_range.max = 2\n#
必须设置service_name,否则Pod无法正常启动,这里设置后可以不设置statefulset下env中的K8S_SERVICE_NAME变量\ncluster_formation.k8s.service_name
= rabbitmq-cluster\n#
必须设置hostname_suffix,否则节点不能成为集群,同时保证namespace正确\ncluster_formation.k8s.hostname_suffix
= .rabbitmq-cluster.default.svc.cluster.local\n#
内存上限\nvm_memory_high_watermark.absolute = 6GB\n#
硬盘上限\ndisk_free_limit.absolute =
20GB\n"},"kind":"ConfigMap","metadata":{"annotations":{},"creationTimestamp":"2021-03-29T08:19:53Z","name":"rabbitmq-config","namespace":"default","selfLink":"/api/v1/namespaces/default/configmaps/rabbitmq-config","uid":"ac4801c3-2e12-4fc6-b1fb-c0d2b51c6d96"}}
data:
enabled_plugins: |
[rabbitmq_management,rabbitmq_peer_discovery_k8s].
rabbitmq.conf: >
## Cluster formation. See https://www.rabbitmq.com/cluster-formation.html to
learn more.
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
## Should RabbitMQ node name be computed from the pod's hostname or IP
address?
## IP addresses are not stable, so using [stable] hostnames is recommended
when possible.
## Set to "hostname" to use pod hostnames.
## When this value is changed, so should the variable used to set the
RABBITMQ_NODENAME
## environment variable.
cluster_formation.k8s.address_type = hostname
## How often should node cleanup checks run?
cluster_formation.node_cleanup.interval = 30
## Set to false if automatic removal of unknown/absent nodes
## is desired. This can be dangerous, see
## *
https://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup
## *
https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ
cluster_formation.node_cleanup.only_log_warning = true
cluster_partition_handling = autoheal
## See https://www.rabbitmq.com/ha.html#master-migration-data-locality
queue_master_locator=min-masters
## See https://www.rabbitmq.com/access-control.html#loopback-users
loopback_users.guest = false
cluster_formation.randomized_startup_delay_range.min = 0
cluster_formation.randomized_startup_delay_range.max = 2
# 必须设置service_name,否则Pod无法正常启动,这里设置后可以不设置statefulset下env中的K8S_SERVICE_NAME变量
cluster_formation.k8s.service_name = rabbitmq-cluster
# 必须设置hostname_suffix,否则节点不能成为集群,同时保证namespace正确
cluster_formation.k8s.hostname_suffix =
.rabbitmq-cluster.default.svc.cluster.local
# 内存上限
vm_memory_high_watermark.absolute = 6GB
# 硬盘上限
disk_free_limit.absolute = 20GB
---
#rabbitmq-sts.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: rabbitmq
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: rabbitmq
template:
metadata:
creationTimestamp: null
labels:
app: rabbitmq
spec:
serviceAccountName: rabbitmq
volumes:
- name: config-volume
configMap:
name: rabbitmq-config
items:
- key: rabbitmq.conf
path: rabbitmq.conf
- key: enabled_plugins
path: enabled_plugins
defaultMode: 420
containers:
- name: rabbitmq
image: '567969457461.dkr.ecr.cn-northwest-1.amazonaws.com.cn/rabbitmq:3.7'
ports:
- name: http
containerPort: 15672
protocol: TCP
- name: amqp
containerPort: 5672
protocol: TCP
env:
- name: HOSTNAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: RABBITMQ_USE_LONGNAME
value: 'true'
- name: RABBITMQ_NODENAME
value: >-
rabbit@$(HOSTNAME).rabbitmq-cluster-c.$(NAMESPACE).svc.cluster.local
- name: RABBITMQ_ERLANG_COOKIE
value: a5mjCA21
resources: {}
volumeMounts:
- name: config-volume
mountPath: /etc/rabbitmq
- name: mq-data
mountPath: /var/lib/rabbitmq
livenessProbe:
exec:
command:
- rabbitmqctl
- status
initialDelaySeconds: 60
timeoutSeconds: 15
periodSeconds: 60
successThreshold: 1
failureThreshold: 3
readinessProbe:
exec:
command:
- rabbitmqctl
- status
initialDelaySeconds: 20
timeoutSeconds: 10
periodSeconds: 60
successThreshold: 1
failureThreshold: 3
volumeClaimTemplates:
- kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mq-data
creationTimestamp: null
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi
storageClassName: ebs-gp2-new
volumeMode: Filesystem
serviceName: rabbitmq-cluster