k8s 为 1个master 3个node
下载镜像 : mariadb 镜像版本是10.2.13 (此时10.3还没发布正式版)
docker pull mariadb
push到私有仓库
docker tag e1dd445713ae 192.168.91.137:5000/mariadbdocker push 192.168.91.137:5000/mariadb
-v /usr/local/docker/mariadb/0/conf:/etc/mysql/conf.d
挂载配置文件不靠谱,所以把容器内/etc/mysql 拷贝到glusterfs
docker cp d07feca8b0df:/etc/mysql /mnt/gv0/k8s-springcloud/mariadb/master/conf/mysqldocker cp d07feca8b0df:/etc/mysql /mnt/gv0/k8s-springcloud/mariadb/slave/conf/mysql
在master 的my.cnf中的[mysqld] 添加以下内容 server-id master ip
binlog-format=ROWlog-bin=master-binlog-slave-updates=truesync-master-info=1slave-parallel-threads=2binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1server-id=141report-port=3306port=3306
在slave 的my.cnf中的[mysqld] 添加以下内容 server-id slave ip (第一次启动slave 时不加,待初始化好了数据库以后,第二次启动容器的时候,加入)
binlog-format=ROWlog-bin=master-binlog-slave-updates=truesync-master-info=1slave-parallel-threads=2binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1server-id=142report-port=3306port=3306
master.yaml 以nodePort 方式访问
kubectl create -f master.yaml
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: mariadb-master namespace: k8s-springcloudspec: replicas: 1 selector: matchLabels: app: mariadb-master template: metadata: labels: app: mariadb-master spec: nodeName: k8s-node-0 hostNetwork: true terminationGracePeriodSeconds: 60 containers: - name: mariadb-master image: 192.168.91.137:5000/mariadb env: - name: MYSQL_ROOT_PASSWORD value: root ports: - containerPort: 3306 hostPort: 3306 livenessProbe: exec: command: - mysql - -uroot - -proot - -e - "select 1" initialDelaySeconds: 10 periodSeconds: 30 timeoutSeconds: 1 readinessProbe: exec: command: - mysql - -uroot - -proot - -e - "select 1" initialDelaySeconds: 10 periodSeconds: 30 timeoutSeconds: 1 volumeMounts: - name: data mountPath: /var/lib/mysql - name: log mountPath: /var/log - name: conf mountPath: /etc/mysql volumes: - name: data hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/master/data - name: log hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/master/log - name: conf hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/master/conf/mysql---apiVersion: v1kind: Servicemetadata: name: mariadb-master namespace: k8s-springcloud labels: app: mariadb-masterspec: type: NodePort ports: - port: 3306 nodePort: 3306 targetPort: 3306
slave.yaml 以nodePort 方式访问
kubectl create -f slave.yaml
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: mariadb-slave namespace: k8s-springcloudspec: replicas: 1 selector: matchLabels: app: mariadb-slave template: metadata: labels: app: mariadb-slave spec: nodeName: k8s-node-1 terminationGracePeriodSeconds: 60 containers: - name: mariadb-slave image: 192.168.91.137:5000/mariadb env: - name: MYSQL_ROOT_PASSWORD value: root ports: - containerPort: 3306 hostPort: 3307 livenessProbe: exec: command: - mysql - -uroot - -proot - -e - "select 1" initialDelaySeconds: 10 periodSeconds: 30 timeoutSeconds: 1 readinessProbe: exec: command: - mysql - -uroot - -proot - -e - "select 1" initialDelaySeconds: 10 periodSeconds: 30 timeoutSeconds: 1 volumeMounts: - name: data mountPath: /var/lib/mysql - name: log mountPath: /var/log - name: conf mountPath: /etc/mysql volumes: - name: data hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/slave/data - name: log hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/slave/log - name: conf hostPath: path: /mnt/gv0/k8s-springcloud/mariadb/slave/conf/mysql---apiVersion: v1kind: Servicemetadata: name: mariadb-slave namespace: k8s-springcloud labels: app: mariadb-slavespec: type: NodePort ports: - port: 3307 nodePort: 3307 targetPort: 3307
进入master 给slave 分配权限
grant replication slave,replication client on *.* to 'slave'@'%' identified by 'slave';flush privileges;
进入slave
change master to MASTER_HOST='192.168.1.207',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_USE_GTID=current_pos;START SLAVE;
show slave status\G
在master创建表
-- ----------------------------DROP TABLE IF EXISTS `tenant_user_info`;CREATE TABLE `tenant_user_info` ( `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `creator` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `create_time` datetime(0) DEFAULT NULL, `modifier` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `modify_time` datetime(0) DEFAULT NULL, `version` int(20) DEFAULT NULL, `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `account_non_expired` bit(1) DEFAULT NULL, `account_non_locked` bit(1) DEFAULT NULL, `credentials_non_expired` bit(1) DEFAULT NULL, `enabled` bit(1) DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;
编写java程序,在插入过程中,直接把slave关机,然后插入结束后,看数据是否自动同步
@RunWith(SpringRunner.class)@SpringBootTestpublic class UserInfoServiceTest { @Resource private UserInfoService userInfoService; private ExecutorService executorService = Executors.newFixedThreadPool(20); @Test public void saveTest(){ int dataCount = 10000; List> futures = new ArrayList<>(dataCount); for(int i=0;i userInfoService.saveAndFlush(userInfo), executorService).exceptionally(e -> { e.printStackTrace(); return null; })); } futures.stream().map(CompletableFuture::join).collect(Collectors.toList()).forEach(u-> System.out.println(u.getId())); }}
其中10条数据,是之前插入的
附上几个简单命令
#查看master gtid位置
select @@global.gtid_current_pos;#查看slave gitd位置select @@global.gtid_slave_pos;#跳过几个错误
stop slave;set global sql_slave_skip_counter=1;start slave;
如果是已存在的数据库 改为gtid,那么复制数据库文件到slave 以后操作
master 操作
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+| master-bin.000005 | 359 | | |+-------------------+----------+--------------+------------------+1 row in set (0.000 sec)
slave 操作
change master to master_host='192.168.91.141', MASTER_PORT=3306,master_user='slave', master_password='slave', master_log_file='master-bin.000005', master_log_pos=359;START SLAVE;