kubernetes安装rabbitmq集群

1.准备K8S环境

2.下载基础镜像,需要安装两种插件:autocluster、rabbitmq_management

方法一:

下载已有插件镜像

[root@localhost ~]#docker pull registry.cn-hangzhou.aliyuncs.com/wise2c/kubernetes-rabbitmq-autocluster

下面是自定义的Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/wise2c/kubernetes-rabbitmq-autocluster:latest
 
RUN rabbitmq-plugins enable --offline rabbitmq_management

构建新镜像(在当前目录新建Dockerfile)

docker build -t  . rabbitmq:new

方法二:

下载原始镜像

下面是自定义的Dockerfile(需要下载autocluster-0.6.1.ez插件)

FROM rabbitmq:3.6.8

ENV RABBITMQ_USE_LONGNAME=true \
    AUTOCLUSTER_LOG_LEVEL=debug \
    AUTOCLUSTER_CLEANUP=true \
    CLEANUP_INTERVAL=60 \
    CLEANUP_WARN_ONLY=false \
    AUTOCLUSTER_TYPE=k8s \
    LANG=en_US.UTF-8

ADD plugins/*.ez /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.8/plugins/

RUN rabbitmq-plugins enable --offline autocluster
RUN rabbitmq-plugins enable --offline rabbitmq_management

构建新镜像(在当前目录新建Dockerfile)

docker build -t  . rabbitmq:new

3.kubernetes配置文件(serviceAccount.yaml、service.yaml、deployment.yaml)

创建serviceAccount,这样autocluster可以通过serviceAccount去访问rabbitmq-service的endpoints

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: rabbitmq 
  namespace: default
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: endpoint-reader
  namespace: default 
rules:
- apiGroups: [""]
  resources: ["endpoints"]
  verbs: ["get"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: endpoint-reader
  namespace: default
subjects:
- kind: ServiceAccount
  name: rabbitmq
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: endpoint-reader

创建service.yaml

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: rabbitmq
  name: rabbitmq
spec:
  ports:
  - port: 5672
    name: port-5672
    targetPort: 5672
    protocol: TCP
    nodePort: 5672
  - port: 4369
    name: port-4369
    targetPort: 4369
    protocol: TCP
  - port: 5671
    name: port-5671
    targetPort: 5671
    protocol: TCP
  - port: 15672
    targetPort: 15672
    nodePort: 15672
    name: port-15672
    protocol: TCP
  - port: 25672
    name: port-25672
    targetPort: 25672
    protocol: TCP
  type: NodePort
  selector:
    app: rabbitmq

创建deployment.yaml,在此之前需要先生成cookie文件

[root@localhost ~]echo $(openssl rand -base64 32) > erlang.cookie
[root@localhost ~]kubectl -n cat create secret generic erlang.cookie --from-file=erlang.cookie
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rabbitmq
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      containers:
      - name: rabbitmq
        image: rabbitmq:new
        ports:
          - containerPort: 5672
            name: port-5672
          - containerPort: 4369
            name: port-4369
          - containerPort: 5671
            name: port-5671
          - containerPort: 15672
            name: port-15672
          - containerPort: 25672
            name: port-25672
        env:
          - name: HOSTNAME
            valueFrom:
             fieldRef:
              fieldPath: status.podIP
          - name: MY_POD_IP
            valueFrom:
             fieldRef:
              fieldPath: status.podIP
          - name: AUTOCLUSTER_CLEANUP
            value: "true"
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          - name: AUTOCLUSTER_LOG_LEVEL
            value: "debug"
          - name: CLEANUP_INTERVAL
            value: "60"
          - name: CLEANUP_WARN_ONLY
            value: "false"
          - name: AUTOCLUSTER_TYPE
            value: "k8s"
          - name: LANG
            value: "en_US.UTF-8"
          - name: RABBITMQ_ERLANG_COOKIE
            valueFrom:
             secretKeyRef:
              name: erlang.cookie
              key: erlang.cookie

[root@localhost ~]# kubectl create -f serviceAccount.yaml

[root@localhost ~]# kubectl create -f service.yaml

[root@localhost ~]# kubectl create -f deployment.yaml

启动后出现报错: 

=INFO REPORT==== 8-Aug-2018::06:18:41 ===
autocluster: (cleanup) No partitioned nodes found.
 
 解决办法:需要将其他两个节点加入到第一个节点中
[root@localhost ~]#kubectl exec -it rabbitmq-84c7bb6bf9-6jq88 /bin/bash -n default
root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl stop_app
root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl join_cluster  rabbit@rabbitmq1
root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl start_app

若出现节点无法加入集群的问题

root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl reset
root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl join_cluster  rabbit@rabbitmq1
root@rabbitmq-84c7bb6bf9-6jq88:/#rabbitmqctl start_app
 
测试访问地址:http://K8S地址:15672

 20181102更新

部署单节点rabbitmq,使用自带管理界面插件的镜像,在docker hub上搜索rabbitmq,下载镜像docker pull rabbitmq:3.6-management

posted @ 2018-08-08 14:40  噔噔蹬蹬  阅读(2100)  评论(0编辑  收藏  举报