mysql集群+k8s集群

Ethereal Lv4
  1. 创建nfs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # 安装
    apt install nfs-kernel-server
    # 创建文件夹
    mkdir -p /root/sharedata/mysql-0
    mkdir -p /root/sharedata/mysql-1
    mkdir -p /root/sharedata/mysql-2
    # 编辑
    vim /etc/exports
    /root/sharedata/mysql-0 *(rw,sync,no_root_squash)
    /root/sharedata/mysql-1 *(rw,sync,no_root_squash)
    /root/sharedata/mysql-2 *(rw,sync,no_root_squash)
    # 使配置生效,不用重启 nfs 服务器,客户端实时更新
    exportfs -rv
    # 启动
    systemctl start rpcbind
    systemctl start nfs-server
    systemctl enable --now nfs-server
    # 查看
    showmount -e
  2. 创建持久卷
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    apiVersion: v1
    kind: PersistentVolume
    metadata:
    name: mysql-pv-0
    spec:
    capacity:
    storage: 1Gi
    accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
    #persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除)
    persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用
    nfs:
    server: 127.0.0.1
    path: /root/sharedata/mysql-0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
#persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除)
persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用
nfs:
server: 127.0.0.1
path: /root/sharedata/mysql-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-2
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
#persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除)
persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用
nfs:
server: 127.0.0.1
path: /root/sharedata/mysql-2
  1. 创建configMap

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: 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. 部署服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Service
    metadata:
    name: mysql-headless
    labels:
    app: mysql
    spec:
    ports:
    - name: mysql
    port: 3306
    clusterIP: None
    selector:
    app: mysql
  3. 部署statefulset

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
    name: mysql-ss
    spec:
    selector:
    matchLabels:
    app: mysql
    serviceName: mysql-headless
    replicas: 1
    template:
    metadata:
    labels:
    app: mysql
    spec:
    initContainers:
    - name: init-mysql
    image: mysql:8.0.18
    command:
    - bash
    - "-c"
    - |
    set ex
    # 从hostname中获取索引,比如(mysql-1)会获取(1)
    [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
    ordinal=${BASH_REMATCH[1]}
    echo [mysqld] > /mnt/conf.d/server-id.cnf
    # 为了不让server-id=0而增加偏移量
    echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
    # 拷贝对应的文件到/mnt/conf.d/文件夹中
    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: jstang/xtrabackup:2.3
    command:
    - bash
    - "-c"
    - |
    set -ex
    # 整体意思:
    # 1.如果是主mysql中的xtrabackup,就不需要克隆自己了,直接退出
    # 2.如果是从mysql中的xtrabackup,先判断是否是第一次创建,因为第二次重启本地就有数据库,无需克隆。若是第一次创建(通过/var/lib/mysql/mysql文件是否存在判断),就需要克隆数据库到本地。
    # 如果有数据不必克隆数据,直接退出()
    [[ -d /var/lib/mysql/mysql ]] && exit 0
    # 如果是master数据也不必克隆
    [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
    ordinal=${BASH_REMATCH[1]}
    [[ $ordinal -eq 0 ]] && exit 0
    # 从序列号比自己小一的数据库克隆数据,比如mysql-2会从mysql-1处克隆数据
    ncat --recv-only mysql-ss-$(($ordinal-1)).mysql-headless 3307 | xbstream -x -C /var/lib/mysql
    # 比较数据
    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:8.0.18
    args: ["--default-authentication-plugin=mysql_native_password"]
    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: 50m
    memory: 50Mi
    livenessProbe:
    exec:
    command: ["mysqladmin", "ping"]
    initialDelaySeconds: 30
    periodSeconds: 10
    timeoutSeconds: 5
    readinessProbe:
    exec:
    command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
    initialDelaySeconds: 5
    periodSeconds: 2
    timeoutSeconds: 1
    - name: xtrabackup
    image: jstang/xtrabackup:2.3
    ports:
    - name: xtrabackup
    containerPort: 3307
    command:
    - bash
    - "-c"
    - |
    set -ex
    # 确定binlog 克隆数据位置(如果binlog存在的话).
    cd /var/lib/mysql
    # 如果存在该文件,则该xrabackup是从现有的从节点克隆出来的。
    if [[ -s xtrabackup_slave_info ]]; then
    mv xtrabackup_slave_info change_master_to.sql.in
    rm -f xtrabackup_binlog_info
    elif [[ -f xtrabackup_binlog_info ]]; then
    [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
    rm xtrabackup_binlog_info
    echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
    MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
    fi
    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"
    mv change_master_to.sql.in change_master_to.sql.orig
    mysql -h 127.0.0.1 <<EOF
    $(<change_master_to.sql.orig),
    MASTER_HOST='mysql-ss-0.mysql-headless',
    MASTER_USER='root',
    MASTER_PASSWORD='',
    MASTER_CONNECT_RETRY=10;
    START SLAVE;
    EOF
    fi
    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: 10m
    memory: 10Mi
    volumes:
    - name: conf
    emptyDir: {}
    - name: config-map
    configMap:
    name: mysql
    volumeClaimTemplates:
    - metadata:
    name: data
    spec:
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 0.1Gi
  4. 附制作xtrabackup镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    FROM paigeeworld/centos7:latest

    MAINTAINER jstang <389634070@qq.com>

    RUN rpm --rebuilddb && \
    yum -y install wget hostname mariadb && \
    wget https://downloads.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.9/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.9-1.el7.x86_64.rpm && \
    yum -y localinstall percona-xtrabackup-80-8.0.9-1.el7.x86_64.rpm

    RUN rm percona-xtrabackup-80-8.0.9-1.el7.x86_64.rpm -rf

    EXPOSE 3307
1
2
3
docker build -t jstang/xtrabackup:2.3
docker login
docker push jstang/xtrabackup:2.3
  1. 搭建服务
    mysql_readonly.yaml这里可以直接修改4中的yaml配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    apiVersion: v1
    kind: Service
    metadata:
    name: mysql-headless
    labels:
    app: mysql
    spec:
    type: NodePort
    ports:
    - name: mysql
    port: 3306
    nodePort: 30080
    targetPort: 3306
    selector:
    app: mysql

mysql_write.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Service
metadata:
name: mysql-write
labels:
app: mysql
spec:
type: NodePort
ports:
- port: 3306
nodePort: 30081 #对k8s外部30080端口
targetPort: 3306
selector:
statefulset.kubernetes.io/pod-name: mysql-ss-0
  1. 其他
    1
    2
    SHOW SLAVE STATUS\G # 检查slave状态
    kubectl get pods --show-labels # 查看pod的labels
  2. 参考
    【全网最全最详细】K8s部署Mysql 8主从复制+读写分离
  • Title: mysql集群+k8s集群
  • Author: Ethereal
  • Created at: 2023-12-07 00:37:15
  • Updated at: 2023-12-07 20:29:45
  • Link: https://ethereal-o.github.io/2023/12/07/mysql集群-k8s集群/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
mysql集群+k8s集群