使用Rancher配置mysql主从
环境
rancher版本:v2.3.5
mysql版本:5.7.29
配置PV与PVC
这里使用NFS测试
选择对应的命名空间创建PVC
为mysql配置文件创建映射
配置文件内容如下:
[client] port= 3306 socket = /tmp/mysql.sock #default-character-set = utf8mb4 ## The MySQL server [mysqld] port = 3306 socket = /tmp/mysql.sock #user = mysql skip-external-locking skip-name-resolve #skip-grant-tables #skip-networking ###################################### dir #basedir=/usr/local/mysql datadir=/var/lib/mysql tmpdir=/var/lib/mysql secure_file_priv=/var/lib/mysql ###################################### some app log-error=mysql.err pid-file=/var/lib/mysql/mysql.pid local-infile=1 event_scheduler=0 federated default-storage-engine=InnoDB #default-time-zone= '+8:00' log_timestamps=SYSTEM character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' #fulltext innodb_optimize_fulltext_only ft_min_word_len=1 #ft_max_word_len innodb_ft_min_token_size=1 ###################################### memory allocate and myisam configure max_connections=3000 #back_log=200 max_connect_errors=10000 key_buffer_size = 16M max_allowed_packet = 16M table_open_cache = 10240 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 2M join_buffer_size=2M myisam_sort_buffer_size = 4M #net_buffer_length = 2M thread_cache_size = 24 query_cache_type=1 query_cache_size=256M query_cache_limit=32M tmp_table_size=1G max_heap_table_size=1G #thread_concurrency =48 ###################################### replication server-id = 10953 relay_log=/var/lib/mysql/relay_log relay_log_index=/var/lib/mysql/relay_log_index relay_log_info_file=/var/lib/mysql/relay_log.info log-bin=mysql-bin binlog_format=mixed max_binlog_size=1G #binlog_cache_size=512M log_slave_updates=true log_bin_trust_function_creators=true expire_logs_days=15 replicate-ignore-db=mysql replicate-ignore-db=test replicate-ignore-db=information_schema replicate-ignore-db=performance_schema replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% replicate-wild-ignore-table=performance_schema.% lower_case_table_names = 1 #read_only=1 master_info_repository=TABLE relay_log_info_repository=TABLE ###################################### slow-query long_query_time=1 slow_query_log=1 slow_query_log_file=/var/lib/mysql/slow-query.log interactive_timeout=600 wait_timeout=600 #log_queries_not_using_indexes=1 ###################################### innodb configure innodb_file_per_table #innodb_file_format=Barracuda #innodb_io_capacity=200 innodb_data_home_dir = /var/lib/mysql #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend innodb_log_group_home_dir = /var/lib/mysql innodb_buffer_pool_size =4G # Set .._log_file_size to 25 % of buffer pool size innodb_log_file_size = 1G innodb_log_files_in_group = 3 innodb_log_buffer_size = 32M #innodb_lock_wait_timeout = 50 innodb_flush_log_at_trx_commit = 1 sync_binlog=0 sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" ########################################## [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash default-character-set = utf8mb4 prompt=\\U \\h \\R:\\m:\\s \\d> [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
部署mysql
这里使用的私有仓库镜像,可以根据需求换成其他的镜像地址。下面查看状态:
原因是NFS服务器对应的挂载目录不存在(即上面创建PV时填的/data/mysqlmaster),创建之后重新部署:
同样的操作创建从库,需要注意的是从库的配置文件里server_id要和主库不一样,从库的relay_log、relay_log_index、relay_log_info_file参数要配置上,因为默认是主机名加序列号的组合,这样的话从库容器重启后主机名会随机生成导致从库找不到中继日志报Slave failed to initialize relay log info structure from the repository的错误,需要手动重新指定主库才能恢复。
下面是配置完成的3个mysql
查看服务发现,由于我们上面已经配置了端口映射会自动创建服务发现,服务发现可以在容器重建、内部IP改变的情况下IP不变。
master的集群IP加端口3306可以用来做主从,k8s集群任意主机IP加49477可以允许外部访问。
配置主从同步
主库执行命令行进行授权和查看状态
两个从库分别执行命令
注意IP要写主库服务发现的集群IP,端口则用3306.
至此搭建完成,测试就不放了 简单说下,无论是主库的pod重建还是从库的pod重建都不影响主从、服务发现里集群IP不会改变,外部访问可以用k8s集群的任意主机IP加服务发现的端口。