環境
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加服務發現的端口。