kubernetes deployment安装mysql

  sre

ns

apiVersion: v1
kind: Namespace
metadata:
  name: database

pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "22Gi"
  volumeName: 
  storageClassName: db

mysql-config

apiVersion: v1
metadata:
  name: mysql-config
data:
  custom.cnf: |
        [mysqld]
        default_storage_engine=innodb
        skip_external_locking
        lower_case_table_names=1
        skip_host_cache
        skip_name_resolve
        character_set_server=utf8
        sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
kind: ConfigMap

mysql密码

#echo -n Changeme | base64
apiVersion: v1
kind: Secret
metadata:
  name: mysql-user-pwd
data:
  mysql-root-pwd: Q2hhbmdlbWU=
  mysql-app-user-pwd: Q2hhbmdlbWU=
  mysql-test-user-pwd: Q2hhbmdlbWU=

service

kind: Service
apiVersion: v1
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
  - name: mysql
    port: 3306
    nodePort: 30306
    targetPort: 3306
    protocol: TCP
  selector:
    name: mysql

/share/backup/db/mysql_bak

deploy

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pwd
              key: mysql-root-pwd
        volumeMounts:
        - name: mysql-pvc
          mountPath: /var/lib/mysql
        volumeMounts:
        - name: mysql-config
          mountPath: /etc/mysql/conf.d/   
      volumes:
       - name: mysql-pvc
         persistentVolumeClaim:
           claimName: mysql
      volumes:
      - name: mysql-config
        configMap:
          name: mysql-config    
      #健康检查
      livenessProbe:
        exec:
          command:
          - /bin/sh
          - "-c"
          - MYSQL_PWD="{MYSQL_ROOT_PASSWORD}"
          - mysql -h 127.0.0.1 -u root -e "SELECT 1"
        initialDelaySeconds: 30
        timeoutSeconds: 5
        successThreshold: 1
        failureThreshold: 3
      readinessProbe:
        exec:
          command:
          - /bin/sh
          - "-c"
          - MYSQL_PWD="{MYSQL_ROOT_PASSWORD}"
          - mysql -h 127.0.0.1 -u root -e "SELECT 1"
        initialDelaySeconds: 10
        timeoutSeconds: 1
        successThreshold: 1
        failureThreshold: 3

备份pv

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqlbackup
spec:
  capacity:
    storage: 930Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /share/backup/db/mysql_bak
    server: nfs.host

备份pvc

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysqlbackuppvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 930Gi

每天02:00备份

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mysql-backup
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: mysql-backup
            imagePullPolicy: IfNotPresent
            image: mysql
            env:
            - name: MYSQL_BACKUP_USER
              value: root
            - name: MYSQL_BACKUP_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-user-pwd
                  key: mysql-root-pwd
            - name: MYSQL_HOST
              value: mysql
            command:
            - /bin/sh
            - -c
            - |
              set -ex
              mysqldump --host=MYSQL_HOST --user=MYSQL_BACKUP_USER \
                        --password=$MYSQL_BACKUP_USER_PASSWORD \
                        --routines --databases appdb --single-transaction \
                        > /mysql-backup/mysql-`date +"%Y%m%d"`.sql
            volumeMounts:
            - name: mysql-backup
              mountPath: /mysql-backup
          restartPolicy: OnFailure
          volumes:
          - name: mysql-backup
            persistentVolumeClaim:
              claimName: mysqlbackuppvc

LEAVE A COMMENT

Captcha Code