博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kubernetes 测试 Mariadb gtid 主从复制.
阅读量:5239 次
发布时间:2019-06-14

本文共 7388 字,大约阅读时间需要 24 分钟。

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;

 

转载于:https://www.cnblogs.com/sweetchildomine/p/8644600.html

你可能感兴趣的文章
349. Intersection of Two Arrays【双指针|二分】
查看>>
extjs gridpanel滚动条问题显示数据不完整
查看>>
springboot(四)设置Redis和Spring的整合
查看>>
mysql提示Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist解决方法...
查看>>
String字符串创建与存储机制
查看>>
现代程序设计 作业1
查看>>
事件和信号量
查看>>
在android开发中添加外挂字体
查看>>
Java中类体的构成
查看>>
HTML5实现图片文件异步上传
查看>>
Eclipse 4.2 汉化
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
网络时间获取
查看>>
多线程实现资源共享的问题学习与总结
查看>>
Code as IaaS for Azure : Terraform 初步
查看>>
WebFrom 小程序【分页功能 】
查看>>
Learning-Python【26】:反射及内置方法
查看>>
day7--面向对象进阶(内含反射和item系列)
查看>>
Python深入01 特殊方法与多范式
查看>>
torch教程[1]用numpy实现三层全连接神经网络
查看>>