《基于k8s的mysql主从配置方案》由会员分享,可在线阅读,更多相关《基于k8s的mysql主从配置方案(13页珍藏版)》请在金锄头文库上搜索。
1、基于k8s的mysql主从配置方案# 1、主从配置文件拉取镜像*xtrabackup*:在work节点上创建文件 pull-.sh#!/bin/bashimage=$1echo $1img=echo $image | sed s/k8s.gcr.io/anjia0532/google-containers/g;s/gcr.io/anjia0532/g;s/./g;s/ /n/g;s/_/-/g;s/anjia0532./anjia0532/g | uniq | awk print $1echo docker pull $imgdocker pull $imgecho docker tag $i
2、mg $imagedocker tag $img $imagechmod +x pull-.sh, 执行 sh pull-.sh *gcr.io/google-samples/xtrabackup:1.0*配置nfs:yum install nfs-utilsmkdir -p /net/mysql-0 /net/mysql-1 /net/mysql-2echo /net/mysql-0 *(rw,no_root_squash) /etc/exportsecho /net/mysql-1 *(rw,no_root_squash) /etc/exportsecho /net/mysql-2 *(r
3、w,no_root_squash) /etc/exportssystemctl restart nfs-serversystemctl enable nfs创建持久卷:yamlmysql_pv-a.yaml :apiVersion: v1kind: PersistentVolumemetadata: name: pv-aspec: capacity: storage: 1Gi accessModes: - ReadWriteOnce - ReadOnlyMany #persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除) pers
4、istentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用 nfs: server: 192.168.74.114 path: /net/mysql-0 mysql_pv-b.yamlapiVersion: v1kind: PersistentVolumemetadata: name: pv-bspec: capacity: storage: 1Gi accessModes: - ReadWriteOnce - ReadOnlyMany #persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清
5、理和删除) persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用 nfs: server: 192.168.74.114 path: /net/mysql-1mysql_pv-b.yamlapiVersion: v1kind: PersistentVolumemetadata: name: pv-cspec: capacity: storage: 1Gi accessModes: - ReadWriteOnce - ReadOnlyMany #persistentVolumeReclaimPolicy: Retain # 当声明被释放
6、,pv将保留(不清理和删除) persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用 nfs: server: 192.168.74.114 path: /net/mysql-2 这里的pv,当执行了kubectl apply -f pv-c.yaml 后,pv下的所有内容都会被删除,请注意操作配置configmap字典yamlapiVersion: v1kind: ConfigMapmetadata: name: mysql labels: app: mysqldata: f: | # Apply this config only o
7、n the master. mysqld log-bin default-storage-engine=INNODB character_set_server=utf8mb4 collation_server=utf8mb4_general_ci lower_case_table_names=1 expire_logs_days = 15 innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 2 sql_mode=ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TAB
8、LES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,PIPES_AS_CONCAT,ANSI_QUOTES client default-character-set=utf8mb4 mysql default-character-set=utf8mb4 f: | # Apply this config only on slaves. mysqld super-read-only default-storage-engine=INNODB character_set_server=utf8
9、mb4 collation_server=utf8mb4_general_ci lower_case_table_names=1 expire_logs_days = 15 innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 2 sql_mode=ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,PIPE
10、S_AS_CONCAT,ANSI_QUOTES client default-character-set=utf8mb4 mysql default-character-set=utf8mb4这个配置会挂载到pod的/etc/mysql/conf.d 中,配置完成后如果apply不能生效,那需要kubectl delete -f,再重新声明。修改了配置文件需要重启statelful kubectl delete -f mysql_statefulSet.yaml kubectl apply -f mysql_statefulSet.yaml配置service:yamlapiVersion: v
11、1kind: Servicemetadata: name: mysql-headless labels: app: mysqlspec: ports: - name: mysql port: 3306 clusterIP: None selector: app: mysql*配置数据库密码*yamlapiVersion: v1kind: Secretmetadata: name: mysql-secrettype: Opaquedata: passsword: YWJjMTIz #echo -c abc123 | base64*部署SatefulSet*yamlapiVersion: apps
12、/v1kind: StatefulSetmetadata: name: mysql-ssspec: selector: matchLabels: app: mysql serviceName: mysql-headless replicas: 2 template: metadata: labels: app: mysql spec: initContainers: - name: init-mysql #这里初始化容器的作用是获取statefulset启动的这个pod的顺序,判断是否为主或备 image: mysql:5.7 command: - bash - -c - | set ex #
13、 从hostname中获取索引,比如(mysql-1)会获取(1) hostname = -(0-9+)$ | exit 1 ordinal=$BASH_REMATCH1 echo mysqld /mnt/conf.d/server-f # 为了不让server-id相同而增加偏移量 echo server-id=$(100 + $ordinal) /mnt/conf.d/server-f #获取的数据进行emptyDir共享卷,给后面的容器使用数据。一个pod里面的卷都是共享的 # 拷贝对应的文件到/mnt/conf.d/文件夹中 if $ordinal -eq 0 ; then cp /mnt/config-map/f /mnt/conf.d/ else cp /mnt/config-map/f /mnt/conf.d/ fi volumeM