K8S容器编排之StatefulSet持久化部署RabbitMQ集群

假设目前的环境是如下配置:

  • K8S 1.12集群
  • Gluster 共享存储集群(并且配置好了逻辑卷)

1.创建pv存储以及pvc存储请求

apiVersion: v1
kind: PersistentVolume
metadata:
  name: rabbitmq
  labels:
    pv: rabbitmq
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "models"
    readOnly: false

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dev-rabbitmq-volume
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      pv: rabbitmq

2.配置好rabbitmqservice account

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

3.创建rabbitmqstatefulset持久化方案

---
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-management
  labels:
    app: rabbitmq
spec:
  ports:
  - port: 15672
    name: http
    nodePort: 32001
  - port: 5672
    name: amqp
    nodePort: 32002
  selector:
    app: rabbitmq
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
  
  labels:
    app: rabbitmq
spec:
  clusterIP: None
  ports:
  - port: 5672
    name: amqp
  selector:
    app: rabbitmq
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: rabbitmq
  
spec:
  serviceName: rabbitmq
  replicas: 3
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      serviceAccountName: rabbitmq
      imagePullSecrets: 
        - name: regsecret
      containers:
      - name: rabbitmq
        image: registry.cn-shenzhen.aliyuncs.com/di_chen/kubernetes-rabbitmq-k8s:3.6.14
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            memory: "256Mi"
            cpu: "150m"
          limits:
            memory: "512Mi"
            cpu: "250m"
        ports:
        - containerPort: 5672
          name: amqp
        env:
          - name: RABBITMQ_ERLANG_COOKIE
            value: UeCinu9N8FmL6Z/z8OOqGDU1MZT9VyRFHBDJJUdQrK4=
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: K8S_SERVICE_NAME
            value: "rabbitmq"
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          - name: RABBITMQ_NODENAME
            value: "rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME)"
          - name: RABBITMQ_NODE_TYPE
            value: disc
          - name: AUTOCLUSTER_TYPE
            value: "k8s"
          - name: AUTOCLUSTER_DELAY
            value: "10"
          - name: AUTOCLUSTER_CLEANUP
            value: "true"
          - name: CLEANUP_WARN_ONLY
            value: "false"
          - name: K8S_ADDRESS_TYPE
            value: "hostname"
          - name: K8S_HOSTNAME_SUFFIX
            value: ".$(K8S_SERVICE_NAME)"
        volumeMounts:
        - name: rabbitmq-volume
          mountPath: /var/lib/rabbitmq
      volumes:
      - name: rabbitmq-volume
        persistentVolumeClaim:
          claimName: dev-rabbitmq-volume
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: NotIn
                values:
                - node04
                - node05

4.总结

使用statefulset的经验就是先配置好共享存储的集群,然后就像配置deployment一样只是需要指定volume

你可能感兴趣的:(K8S容器编排)