k8s中创建rabbitmq集群

在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容