kubernets安装rabbitmq集群.

RabbitMQ集群的两种模式
1)普通模式:默认的集群模式,队列消息只存在单个节点上
2)镜像模式:队列为镜像队列,队列消息存在每个节点上

配置同步:
配置同步:
1.Ha mode 同步模式,以下可选:
1)all 所有的节点都将被同步
2)exactly 指定个数的节点被同步
3)nodes 指定的名称的节点被同步
2.Ha params 上一项中的参数
3.Ha async mode 同步方式,自动或手动

创建rabbitmq集群的yaml文件

下面yaml中涉及的Kubernetes组件有:

  1. Service
    1. 提供http服务的service
    2. 提供StatfulSet的headless service
  2. StatefulSet
    1. 有状态pods的集合,真正提供rabbitmq的服务
[root@k8smaster centos]# cat rabbitmq.yaml 
---
apiVersion: v1
kind: Service
metadata:
  # Expose the management HTTP port on each node
  name: rabbitmq-management
  labels:
    app: rabbitmq
spec:
  ports:
  - port: 15672
    name: http
  selector:
    app: rabbitmq
  type: NodePort # Or LoadBalancer in production w/ proper security
---
apiVersion: v1
kind: Service
metadata:
  # The required headless service for StatefulSets
  name: rabbitmq
  labels:
    app: rabbitmq
spec:
  ports:
  - port: 5672
    name: amqp
  - port: 4369
    name: epmd
  - port: 25672
    name: rabbitmq-dist
  clusterIP: None
  selector:
    app: rabbitmq
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: rabbitmq
spec:
  serviceName: "rabbitmq"
  replicas: 3
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      containers:
      - name: rabbitmq
        image: rabbitmq:3.7-rc-management
        lifecycle:
          postStart:
            exec:
              command:
              - /bin/sh
              - -c
              - >
                if [ -z "$(grep rabbitmq /etc/resolv.conf)" ]; then
                  sed "s/^search \([^ ]\+\)/search rabbitmq.\1 \1/" /etc/resolv.conf > /etc/resolv.conf.new;
                  cat /etc/resolv.conf.new > /etc/resolv.conf;
                  rm /etc/resolv.conf.new;
                fi;
                until rabbitmqctl node_health_check; do sleep 1; done;
                if [ -z "$(rabbitmqctl cluster_status | grep rabbitmq-0)" ]; then
                  touch /gotit
                  rabbitmqctl stop_app;
                  rabbitmqctl reset;
                  rabbitmqctl join_cluster rabbit@rabbitmq-0;
                  rabbitmqctl start_app;
                else
                  touch /notget
                fi;
        env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: RABBITMQ_ERLANG_COOKIE
          value: "YZSDHWMFSMKEMBDHSGGZ"
        - name: RABBITMQ_NODENAME
          value: "rabbit@$(MY_POD_NAME)"
        ports:
        - containerPort: 5672

通过Pod的postStart来执行命令,把后面的两个Pod加入rabbitmq集群。

Pod的生命周期里有两个个hook部分:PostStart,PreStop

创建不同的rabbitmq集群,需要把yaml文件的很多处rabbitmq关键字替换掉。。。

也可以通过参数来生成yaml文件

kubectl create -f rabbitmq.yam
service "rabbitmq-management" created
service "rabbitmq" created
statefulset "rabbitmq" created
[root@k8smaster centos]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7db9fccd9b-99h6b   1/1     Running   0          2d23h
rabbitmq-0               1/1     Running   0          57m
rabbitmq-1               1/1     Running   0          55m
rabbitmq-2               1/1     Running   0          51m

访问集群

[root@k8smaster centos]# kubectl get svc
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                       AGE
kubernetes            ClusterIP   10.96.0.1               443/TCP                       4d
rabbitmq              ClusterIP   None                    5672/TCP,4369/TCP,25672/TCP   105m
rabbitmq-management   NodePort    10.107.192.23           15672:32201/TCP               105m

可以通过 宿主机主机ip:32219访问
默认用户名guest密码guest
kubernets安装rabbitmq集群._第1张图片

之后补充.

参考:
http://www.yangguanjun.com/2018/08/06/rabbitmq-deploy-with-k8s/
https://segmentfault.com/a/1190000017338025
StatefulSet 基本使用
https://kubernetes.io/zh/docs/tutorials/stateful-application/basic-stateful-set/
RabbitMQ安装与初始配置
https://www.cnblogs.com/chrischennx/p/7071471.html

转载于:https://www.cnblogs.com/lovesKey/p/10909625.html

你可能感兴趣的:(kubernets安装rabbitmq集群.)