環(huán)境
主機名 | 機器IP |
---|---|
10.0.102.214 | node1 |
10.0.102.220 | node2 |
10.0.102.116 | node3 |
部署
compose-mariadb.yaml文件說明
[root@k8s-node-1 ~]# vim compose-mariadb.yaml
version: '3'
services:
mariadb_galera0: #名稱修改下便于區(qū)別臭笆,各個節(jié)點不同
image: severalnines/mariadb:10.1 #鏡像
ports:
- "3306:3306" #各個節(jié)點訪問端口
environment: #環(huán)境變量
CLUSTER_NAME: "mariadb-cluster-new" #定義mariadb集群名稱
DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
MYSQL_ROOT_PASSWORD: "mypassword123"
XTRABACKUP_PASSWORD: "mypassword123"
command: #容器啟動參數(shù)
- --innodb_buffer_pool_size=256M
- --max_connections=81
healthcheck:
interval: 5s
timeout: 3s
retries: 200 # interval * retries > SST duration
volumes:
- /data:/var/lib/mysql #各個節(jié)點的掛載目錄妓柜,預(yù)先創(chuàng)好
- /etc/my.cnf.d:/etc/my.cnf.d #是否開通binlog配置好,放此目錄
- /etc/localtime:/etc/localtime #定義時區(qū)
network_mode: "host" #使用本機網(wǎng)絡(luò)
【見圖1】
k8s-node-1
部署:
創(chuàng)建掛載所需目錄與文件
mkdir /data/
mkdir /etc/my.cnf.d
[root@10-0-102-165 ~]# vim /etc/my.cnf.d/bin-log.cnf
[mysqld]
log-bin= mysql-bin
log_slave_updates = 1
expire_logs_days = 20
server-id = 211
執(zhí)行docker-compose命令進行部署(部署新集群時確保/data目錄為空)
[root@10-0-102-165 ~]# rm -rf /data/*
[root@10-0-102-165 ~]# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera0_1 ... done
【見圖2】
查詢?nèi)萜鲉尤罩?# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')
驗證容器是否啟動及集群狀態(tài)
mysql -uroot -pmypassword123 -h10.0.102.165
MariaDB [(none)]> show status like 'wsrep%';
【見圖3】
k8s-node-2
創(chuàng)建掛載所需目錄與文件
#mkdir /data/
#mkdir /etc/my.cnf.d #不開通binlog只創(chuàng)建目錄即可
啟動容器
# rm -rf /data/*
# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera1_1 ... done
# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')
【見圖4】
驗證
mysql -uroot -pmypassword123 -h 10.0.102.165 -e "show status like 'wsrep%';"
可以發(fā)現(xiàn)節(jié)點2的ip已經(jīng)加入到mariadb集群中稳其,只等第三個節(jié)點了
【見圖5】
k8s-node-3
創(chuàng)建掛載所需目錄與文件
mkdir /data/
mkdir /etc/my.cnf.d #不開通binlog只創(chuàng)建目錄即可
2.啟動容器
# rm -rf /data/*
# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera1_1 ... done
# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')
【見圖6】
3.驗證
mysql -uroot -pmypassword123 -h 10.0.102.165 -e "show status like 'wsrep%';"
【見圖7】
注意事項
1.擴容
擴容比較簡單执俩,只需copy一份compose文件药版,簡單修改名稱即可伯铣,創(chuàng)建相應(yīng)的目錄受裹,使用docker-compose -f 文件名 up –d即可事扭,請確保啟動前/data目錄為空2.縮容
縮容時,先使用docker stop NF}')停止容器桶癣,然后使用dockerrmNF}')刪除退出的容器即可3.集群啟動與停止時請按照順序執(zhí)行拥褂,不可并行,而且停止時建議使用docker stop命令
4.某個節(jié)點異常后重新加入集群需要停止服務(wù)并清空/data目錄再使用docker-compose牙寞,請確保compose文件中集群信息的配置與集群相符合
5備份恢復(fù)操作與k8s中mariadb集群的備份恢復(fù)操作相同
五饺鹃、配置文件(對應(yīng)各個節(jié)點,在相應(yīng)節(jié)點運行)
yaml文件
compose-mariadb1.yaml
version: '3'
services:
mariadb_galera0:
image: severalnines/mariadb:10.1
ports:
- "3306:3306"
environment:
CLUSTER_NAME: "mariadb-cluster-new"
DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
MYSQL_ROOT_PASSWORD: "mypassword123"
XTRABACKUP_PASSWORD: "mypassword123"
command:
- --innodb_buffer_pool_size=256M
- --max_connections=81
healthcheck:
interval: 5s
timeout: 3s
retries: 200 # interval * retries > SST duration
volumes:
- /data:/var/lib/mysql
- /etc/my.cnf.d:/etc/my.cnf.d
- /etc/localtime:/etc/localtime
network_mode: "host"
compose-mariadb2.yaml
version: '3'
services:
mariadb_galera1:
image: severalnines/mariadb:10.1
ports:
- "3306:3306"
- "4567:4567"
environment:
CLUSTER_NAME: "mariadb-cluster-new"
DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
MYSQL_ROOT_PASSWORD: "mypassword123"
XTRABACKUP_PASSWORD: "mypassword123"
command:
- --innodb_buffer_pool_size=256M
- --max_connections=81
healthcheck:
interval: 8s
timeout: 3s
retries: 300 # interval * retries > SST duration
volumes:
- /data:/var/lib/mysql
- /etc/my.cnf.d:/etc/my.cnf.d
- /etc/localtime:/etc/localtime
compose-mariadb3.yaml
version: '3'
services:
mariadb_galera2:
image: severalnines/mariadb:10.1
ports:
- "3306:3306"
- "4567:4567"
environment:
CLUSTER_NAME: "mariadb-cluster-new"
DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
MYSQL_ROOT_PASSWORD: "mypassword123"
XTRABACKUP_PASSWORD: "mypassword123"
command:
- --innodb_buffer_pool_size=256M
- --max_connections=81
healthcheck:
interval: 5s
timeout: 3s
retries: 200 # interval * retries > SST duration
volumes:
- /data:/var/lib/mysql
- /etc/my.cnf.d:/etc/my.cnf.d
- /etc/localtime:/etc/localtime
network_mode: host