实现"mysql on k8s高可用"教程

整体步骤

步骤 内容
1 创建MySQL StatefulSet
2 部署MySQL高可用方案
3 部署MySQL监控
4 部署MySQL备份
gantt
    title 实现"mysql on k8s高可用"流程
    section 创建MySQL StatefulSet
    创建StatefulSet: done, 2022-01-01, 2d
    section 部署MySQL高可用方案
    部署高可用方案: done, 2022-01-03, 2d
    section 部署MySQL监控
    部署监控: done, 2022-01-05, 2d
    section 部署MySQL备份
    部署备份: done, 2022-01-07, 2d

创建MySQL StatefulSet

  1. 首先,创建一个名为mysql-statefulset.yaml的YAML文件,并填入以下内容:
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-persistent-storage
  volumeClaimTemplates:
    - metadata:
        name: mysql-persistent-storage
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi
  1. 使用kubectl命令部署StatefulSet:
kubectl apply -f mysql-statefulset.yaml

部署MySQL高可用方案

  1. 创建名为mysql-ha.yaml的YAML文件,填入以下内容:
apiVersion: v1
kind: Service
metadata:
  name: mysql-ha
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-ha
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql-ha
          image: mysql:5.7
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: password
            - name: MYSQL_REPLICATION_MODE
              value: master
  1. 使用kubectl命令部署高可用方案:
kubectl apply -f mysql-ha.yaml

部署MySQL监控

  1. 创建名为mysql-monitor.yaml的YAML文件,填入以下内容:
apiVersion: v1
kind: Service
metadata:
  name: prometheus-mysql
spec:
  selector:
    app: mysql
  ports:
    - port: 9104
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: prometheus-mysql
          image: prom/mysqld-exporter
          ports:
            - containerPort: 9104
          env:
            - name: DATA_SOURCE_NAME
              value: "user:password@(mysql-exporter:3306)/"
  1. 使用kubectl命令部署监控:
kubectl apply -f mysql-monitor.yaml

部署MySQL备份

  1. 创建名为mysql-backup.yaml的YAML文件,填入以下内容:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mysql-backup
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: mysql-backup
        spec:
          containers:
            - name: mysql-backup
              image: mysql:5.7
              command: ["sh", "-c", "mysqldump -h mysql -uroot -ppassword mydatabase > /backup/mydatabase.sql"]
              volumeMounts:
                - mountPath: /backup