关于k8s环境部署mysql主从的问题

一、通过yaml部署mysql

1、mysql-configmap.yaml

apiversion: v1
kind: configmap
metadata:
  name: mysql
  namespace: mysql
  labels:
    app: mysql
data:
  master.cnf: |
    # apply this config only on the master.
    [mysqld]
    log-bin
  slave.cnf: |
    # apply this config only on slaves.
    [mysqld]
    super-read-only

2、mysql-service.yaml

# headless service for stable dns entries of statefulset members.
apiversion: v1
kind: service
metadata:
  name: mysql
  namespace: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterip: none
  selector:
    app: mysql
---
# client service for connecting to any mysql instance for reads.
# for writes, you must instead connect to the master: mysql-0.mysql.
apiversion: v1
kind: service
metadata:
  name: mysql-read
  namespace: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql

3、mysql-statefulset.yaml

apiversion: apps/v1
kind: statefulset
metadata:
  name: mysql
  namespace: mysql
spec:
  selector:
    matchlabels:
      app: mysql
  servicename: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      tolerations:
      - effect: noschedule
        operator: exists
      affinity:
        nodeaffinity:
          requiredduringschedulingignoredduringexecution:
            nodeselectorterms: # 调度指定标签节点
              - matchexpressions:
                  - key: node-role.kubernetes.io/compute
                    operator: in
                    values:
                    - dedicated-middleware
      initcontainers:
      - name: init-mysql
        image: mysql:5.7.35
        command:
        - bash
        - "-c"
        - |
          set -ex
          # generate mysql server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${bash_rematch[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # copy appropriate conf.d files from config-map to emptydir.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d/
          fi          
        volumemounts:
        - name: conf
          mountpath: /mnt/conf.d
        - name: config-map
          mountpath: /mnt/config-map
      - name: clone-mysql
        image: registry.cn-shanghai.aliyuncs.com/soulchild/xtrabackup:2.4
        command:
        - bash
        - "-c"
        - |
          set -ex
          # skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # skip the clone on master (ordinal index 0).
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${bash_rematch[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -c /var/lib/mysql
          # prepare the backup.
          xtrabackup --prepare --target-dir=/var/lib/mysql          
        volumemounts:
        - name: data
          mountpath: /var/lib/mysql
          subpath: mysql
        - name: conf
          mountpath: /etc/mysql/conf.d
      containers:
      - name: mysql
        image: mysql:5.7.35
        env:
        - name: mysql_allow_empty_password
          value: "1"
        ports:
        - name: mysql
          containerport: 3306
        volumemounts:
        - name: data
          mountpath: /var/lib/mysql
          subpath: mysql
        - name: conf
          mountpath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 500m
            memory: 1gi
        livenessprobe:
          exec:
            command: ["mysqladmin", "ping"]
          initialdelayseconds: 30
          periodseconds: 10
          timeoutseconds: 5
        readinessprobe:
          exec:
            # check we can execute queries over tcp (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "select 1"]
          initialdelayseconds: 5
          periodseconds: 2
          timeoutseconds: 1
      - name: xtrabackup
        image: registry.cn-shanghai.aliyuncs.com/soulchild/xtrabackup:2.4
        ports:
        - name: xtrabackup
          containerport: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # xtrabackup already generated a partial "change master to" query
            # because we're cloning from an existing slave. (need to remove the tailing semicolon!)
            cat xtrabackup_slave_info | sed -e 's/;$//g' > change_master_to.sql.in
            # ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # we're cloning directly from master. parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm -f xtrabackup_binlog_info xtrabackup_slave_info
            echo "change master to master_log_file='${bash_rematch[1]}',\
                  master_log_pos=${bash_rematch[2]}" > change_master_to.sql.in
          fi

          # check if we need to complete a clone by starting replication.
          if [[ -f change_master_to.sql.in ]]; then
            echo "waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "select 1"; do sleep 1; done

            echo "initializing replication from clone position"
            mysql -h 127.0.0.1 \
                  -e "$(<change_master_to.sql.in), \
                          master_host='mysql-0.mysql', \
                          master_user='root', \
                          master_password='', \
                          master_connect_retry=10; \
                        start slave;" || exit 1
            # in case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
          fi

          # start a server to send backups when requested by peers.
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"          
        volumemounts:
        - name: data
          mountpath: /var/lib/mysql
          subpath: mysql
        - name: conf
          mountpath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 100m
            memory: 100mi
      volumes:
      - name: conf
        emptydir: {}
      - name: config-map
        configmap:
          name: mysql
  volumeclaimtemplates:
  - metadata:
      name: data
    spec:
      accessmodes:
      - readwriteonce
      resources:
        requests:
          storage: 30gi

到此这篇关于k8s环境部署mysql主从的文章就介绍到这了,更多相关k8s部署mysql主从内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐