zoukankan      html  css  js  c++  java
  • k8s搭建rabbitmq和rabbitmq集群

    版本说明:

    k8s:v1.15.1

    RabbitMQ 3.8.2 on Erlang 22.2.2

    Docker Image

    docker pull rabbitmq:3.8.2-alpine

    1、rabbitmq单例模式:

    rabbit.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: rabbit-service
      namespace: r
      labels:
       name: rabbit-out
    spec:
      type: NodePort
      ports:
      - port: 5672
        targetPort: 5672
        protocol: TCP
        nodePort: 31001
      selector:
        name: rabbit
    ---
    apiVersion: v1
    kind: ReplicationController
    metadata:
     name: rabbit
     namespace: r
     labels:
      name: rabbit
    spec:
      replicas: 1
      selector:
       name: rabbit
      template:
       metadata:
        labels:
         name: rabbit
       spec:
    #    nodeSelector:
    #      zone: outside
        hostAliases:
        - ip: "127.0.0.1"
          hostnames:
          - "rabbit-host-1"
        containers:
        - name: rabbit
          imagePullPolicy: IfNotPresent
          image: 192.168.1.1:1234/rabbit:v1.0.1
          env:
          - name: LANG
            value: "C.UTF-8"
          - name: HOSTNAME
            value: "rabbit-host-1"
          volumeMounts:
          - mountPath: /var/lib/rabbitmq/mnesia
            name: rabbit-data
        volumes:
        - name: rabbit-data
          nfs:
            path: /home/rabbit/etc
            server: 192.168.1.1
    kubectl create -f ./rabbit.yaml
    kubectl exec -it -n r rabbit-xxxx /bin/bash
    rabbitmqctl add_user admin password
    rabbitmqctl set_user_tags admin administrator
    rabbitmqctl set_permissions -p /  admin '.*' '.*' '.*'
    #重启后配置不会丢失

    2、rabbit_cluster集群模式

    # 创建唯一erlang.cookie
    echo $(openssl rand -base64 32) > erlang.cookie
    kubectl create secret generic erlang.cookie --from-file=erlang.cookie -n r

    rabbitmq.yaml

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: rabbitmq
      namespace: r
    ---
    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: rabbitmq
      namespace: r
    rules:
      - apiGroups:
          - ""
        resources:
          - endpoints
        verbs:
          - get
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: rabbitmq
      namespace: r
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: Role
      name: rabbitmq
    subjects:
    - kind: ServiceAccount
      name: rabbitmq
      namespace: r
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: rabbitmq-service #rabbit以外使用
      namespace: r
    spec:
      type: NodePort
      ports:
        - name: mangement
          protocol: TCP
          port: 15672
          nodePort: 31002
        - name: smp 
          protocol: TCP
          port: 5672
          nodePort: 31001
      selector:
        app: rabbitmq
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: rabbitmq #rabbitmq内部使用
      namespace: r
      labels:
        app: rabbitmq
    spec:
      clusterIP: None
      ports:
      - port: 5672
        name: amqp
      selector:
        app: rabbitmq
    ---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: rabbitmq
      namespace: r
    spec:
      serviceName: rabbitmq
      updateStrategy:
        type: RollingUpdate
      replicas: 3
      selector:
        matchLabels:
          app: rabbitmq
      template:
        metadata:
          name: rabbitmq
          labels:
            app: rabbitmq
        spec:
          serviceAccountName: rabbitmq
          containers:
          - name: rabbitmq
            image: 192.168.1.1:1234/rabbit:v1.0.1
            imagePullPolicy: IfNotPresent
            resources:
              requests:
                memory: "800Mi"
                cpu: "0.4"
              limits:
                memory: "900Mi"
                cpu: "0.6"
            volumeMounts:
              - name: rabbitmq-data
                mountPath: /var/lib/rabbitmq/mnesia
            ports:
            - containerPort: 5672
              name: amqp
            env:
              - name: RABBITMQ_DEFAULT_USER
                value: admin
              - name: RABBITMQ_DEFAULT_PASS
                value: password
              - name: RABBITMQ_ERLANG_COOKIE
                valueFrom:
                  secretKeyRef:
                    name: erlang.cookie
                    key: erlang.cookie
              - name: HOSTNAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
              - name: SERVICE_INTERNAL_NAME
                value: "rabbitmq"
              - name: RABBITMQ_USE_LONGNAME
                value: "true"
              - name: RABBITMQ_NODENAME
                value: "rabbit@$(HOSTNAME).$(SERVICE_INTERNAL_NAME).$(NAMESPACE).svc.cluster.local"
          volumes:
            - name: rabbitmq-data
              nfs:
                path: /home/rabbit/etc
                server: 192.168.1.1
    kubectl create -f ./rabbitmq.yaml
    
    kubectl exec -it -n r rabbitmq-1 /bin/bash
    rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) stop_app
    rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) join_cluster rabbit@rabbitmq-0
    rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) start_app
    #rabbitmq-2同上
    
    kubectl exec -it -n r rabbitmq-0 /bin/bash
    rabbitmqctl --erlang-cookie $(cat $HOME/.erlang.cookie) cluster_status
    ----Running Nodes
    ----rabbit@rabbitmq-0.rabbitmq.r.svc.cluster.local
    ----rabbit@rabbitmq-1.rabbitmq.r.svc.cluster.local
    ----rabbit@rabbitmq-2.rabbitmq.r.svc.cluster.local

    参考链接:

    https://registry.hub.docker.com/_/rabbitmq/

    https://zupzup.org/k8s-rabbitmq-cluster/

  • 相关阅读:
    Herny
    机器学习No.4
    机器学习No.3
    机器学习No.2
    机器学习No.1
    算法第五章上机实践报告
    算法第五章作业
    算法第四章实践报告
    算法第四章作业
    算法第三章作业
  • 原文地址:https://www.cnblogs.com/floud/p/12703509.html
Copyright © 2011-2022 走看看