前提假设:
1.已安装k8s环境;
2.因为我使用nfs作为pv存储介质,所以需要预先配置好nfs服务
nfs安装可参考:https://blog.csdn.net/wudinaniya/article/details/81068518
步骤:
1.规划mysql持久化文件在nfs中的存储路径;
2.创建mysql pv;
3.创建mysql configmap;
4.创建mysql service;
5.创建mysql statefulset;
6.验证mysql是否安装成功。
步骤1:
在nfs路径下(我是192.168.152.12的/home/nfs/volumes),创建mysql-datadir-1 mysql-datadir-2 mysql-datadir-3三个文件夹
mkdir mysql-datadir-1 mysql-datadir-2 mysql-datadir-3
步骤2:创建pv
pv.yaml:
指向步骤1中的3个mysql-datadir文件夹。 文件中的server填入nfs-server服务所在ip
--- apiVersion: v1 kind: PersistentVolume metadata: name: mysql-datadir-1 spec: capacity: storage: 2Gi accessModes: ["ReadWriteMany","ReadWriteOnce"] nfs: path: /home/nfs/volumes/mysql-datadir-1 server: 192.168.152.12 --- apiVersion: v1 kind: PersistentVolume metadata: name: mysql-datadir-2 spec: capacity: storage: 2Gi accessModes: ["ReadWriteMany","ReadWriteOnce"] nfs: path: /home/nfs/volumes/mysql-datadir-2 server: 192.168.152.12 --- apiVersion: v1 kind: PersistentVolume metadata: name: mysql-datadir-3 spec: capacity: storage: 2Gi accessModes: ["ReadWriteMany","ReadWriteOnce"] nfs: path: /home/nfs/volumes/mysql-datadir-3 server: 192.168.152.12
创建pv
kubectl apply -f pv.yaml
步骤3:创建mysql configmap
configmap.yaml:
apiVersion: v1 kind: ConfigMap metadata: name: mysql labels: app: mysql data: master.cnf: | [mysqld] log-bin default-time-zone='+8:00' log_bin_trust_function_creators=1 lower_case_table_names=1 slave.cnf: | [mysqld] super-read-only default-time-zone='+8:00' log_bin_trust_function_creators=1
创建configmap
kubectl apply -f configmap.yaml
步骤4:创建mysql service
service.yaml: 其中服务mysql用于读写;服务mysql-read用于只读。
apiVersion: v1 kind: Service metadata: name: mysql labels: app: mysql spec: ports: - name: mysql port: 3306 clusterIP: None selector: app: mysql --- apiVersion: v1 kind: Service metadata: name: mysql-read labels: app: mysql spec: ports: - name: mysql port: 3306 selector: app: mysql
创建service:
kubectl apply -f service.yaml
步骤5:创建statefulset
statefulset.yaml:
注意事项:官网或其他博客用的xtrabackup镜像无法拉取,所以使用国内阿里云的镜像替代:registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0
apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: selector: matchLabels: app: mysql serviceName: mysql replicas: 3 template: metadata: labels: app: mysql spec: initContainers: - name: init-mysql image: mysql:5.7 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-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 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 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-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 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: 2Gi
创建statefulset:
kubectl apply -f statefulset.yaml
可以看到,mysql pod正在初始化。因为需要拉取mysql镜像和xtrabackup镜像,所以需要等待
步骤6:验证是否安装成功
6.1查看mysql master状态并创建数据库testdb:
[root@k8s-master mysql]# kubectl exec -it mysql-0 sh # mysql mysql> show databases; mysql> show master status; mysql> create database testdb; mysql> show databases;
6.2 查看其中一个mysql slave的状态,并验证新创建的数据库testdb是否成功同步:
[root@k8s-master mysql-datadir-3]# kubectl exec -it mysql-1 sh # mysql mysql> show databases;
查看slave状态:
mysql> show slave status \G;
说明:Slave_IO_State: Waiting for master to send event 证明slave正在监听master
备注:
按照以上步骤,成功完成了基于k8s的mysql5.7主从集群安装。
过程并非顺利,可能会出现slave无法同步数据的问题。但重复安装2次后,问题消失。根因有待继续跟踪
参考链接:
https://blog.51cto.com/u_15127511/2657609
https://blog.csdn.net/weixin_43936969/article/details/106289127
https://www.cnblogs.com/cooper-73/p/12988614.html