kubernetes部署rabbitmq集群

os:centos7 kubernetes版本:1.8 rabbitmq版本:3.7.4(只要有下面所说的功能就可以,版本不一定非要这个号的)

首先说明一下,rabbitmq是使用autocluster插件去调用kubernetes apiserver获取rabbitmq服务的endpoints获取node节点信息,并加入集群的。下面开始操作:

1. kubernetes环境

2. 下载docker镜像,我是从阿里下载的已经包含了autocluser这个插件

docker pull  registry.cn-hangzhou.aliyuncs.com/wise2c/kubernetes-rabbitmq-autocluster

3. 因为我需要web管理界面,所以自己再弄个带web管理插件的镜像,下面是自定义的Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/wise2c/kubernetes-rabbitmq-autocluster:latest

MAINTAINER demon
RUN rabbitmq-plugins enable --offline rabbitmq_management

执行

docker build -t rabbitmq:3.7.4 .

4. kubernetes部署文件

a. rabbitmq-serviceAccount.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: rabbitmq
  namespace: default
rules:
  - apiGroups:
      - ""
    resources:
      - endpoints
    verbs:
      - get
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: rabbitmq
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: rabbitmq
subjects:
- kind: ServiceAccount
  name: rabbitmq
  namespace: default

b. rabbitmq-service.yaml,创建服务的文件

apiVersion: v1
kind: Service
metadata:
  labels:
    app: rabbitmq
  name: rabbitmq
  namespace: default  
spec:
  ports:
  - port: 5672
    name: port-5672
    protocol: TCP
    targetPort: 5672    
  - port: 4369
    name: port-4369
    protocol: TCP
    targetPort: 4369    
  - port: 5671
    name: port-5671
    protocol: TCP
    targetPort: 5671    
  - port: 15672
    name: port-15672
    protocol: TCP
    targetPort: 15672    
  - port: 25672
    name: port-25672
    protocol: TCP
    targetPort: 25672    
  selector:
    app: rabbitmq
  externalIPs:
    - 192.168.23.135

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

echo $(openssl rand -base64 32) > erlang.cookie
kubectl -n cat create secret generic erlang.cookie --from-file=erlang.cookie

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rabbitmq
  namespace: default  
  labels:
    app: rabbitmq  
spec:
  serviceName: rabbitmq
  replicas: 3
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      serviceAccountName: rabbitmq
      containers:
      - name: rabbitmq
        image: rabbitmq:3.7.4
        imagePullPolicy: Never
        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: AUTOCLUSTER_TYPE
            value: "k8s"
          - name: AUTOCLUSTER_DELAY
            value: "10"
          - name: AUTOCLUSTER_CLEANUP
            value: "true"
          - name: RABBITMQ_DEFAULT_USER
            value: ehooo
          - name: RABBITMQ_DEFAULT_PASS
            value: ehooo100
          - name: HOSTNAME
            valueFrom:
             fieldRef:
              fieldPath: status.podIP
          - name: MY_POD_IP
            valueFrom:
             fieldRef:
              fieldPath: status.podIP
          - name: CLEANUP_INTERVAL
            value: "60"
          - name: CLEANUP_WARN_ONLY
            value: "false"
          - name: K8S_SERVICE_NAME
            value: "rabbitmq"
          - name: K8S_ADDRESS_TYPE
            value: "hostname"
          - name: K8S_HOSTNAME_SUFFIX
            value: ".$(K8S_SERVICE_NAME)"
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          - name: K8S_HOST
            value: "192.168.23.135"
          - name: K8S_PORT
            value: "6443"
          - name: RABBITMQ_ERLANG_COOKIE
            valueFrom:
             secretKeyRef:
              name: erlang.cookie
              key: erlang.cookie

5. 执行这些文件

kubectl create -f rabbitmq-serviceAccount.yaml
kubectl create -f rabbitmq-service.yaml
kubectl create -f rabbitmq-deployment.yaml
至此,环境已经OK了,但是在一开始运行的时候,会报一个错: {tables_not_present,{rabbit,start,[normal,[]]}}
这个错误意味着没有找到Erlang的Mnesia分布式数据库中某些Rabbit依赖其维护代理元数据的表,个人感觉挂载volumn应该就可以了

但是一会就又好了,最后截个图




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值