一、环境准备
1、各主机配置静态域名解析:
[root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.254.20 reg.yunwei.edu #192.168.42.30 reg.yunwei.edu 192.168.42.172 node3 192.168.42.122 node2 192.168.42.121 node1 192.168.42.30 dlp
2、并配置各节点ssh无密钥登陆
3、安装docker环境
[root@node1 ~]# ls anaconda-ks.cfg docker docker.tar.gz image yum-repo.sh [root@node1 ~]# cd docker [root@node1 docker]# ls ca.crt docker-app.tar.gz docker.sh remove.sh [root@node1 docker]# bash docker.sh
4、下载配置文件并且导入镜像
wget http://download2.yunwei.edu/shell/ansible-galera.tar.gz
[root@dlp ~]# ls
anaconda-ks.cfg config.tar.gz docker.tar.gz kubernetes.tar.gz test username
ansible-galera.tar.gz data galera.tar.gz mariadb-galera.tar total.sh yum-repo.sh
bash-completion-2.1-6.el7.noarch.rpm docker kubernetes password try.html
[root@dlp ~]# tar xf ansible-galera.tar.gz
[root@dlp ~]# ls
anaconda-ks.cfg bash-completion-2.1-6.el7.noarch.rpm docker kubernetes password try.html
ansible-galera config.tar.gz docker.tar.gz kubernetes.tar.gz test username
ansible-galera.tar.gz data galera.tar.gz mariadb-galera.tar total.sh yum-repo.sh
[root@dlp ~]# cd ansible-galera
[root@dlp ansible-galera]# ls
bin delete.yml hosts install.yml roles
[root@dlp ansible-galera]# mv * /etc/ansible/
[root@dlp ansible-galera]# cd /etc/ansible/
[root@dlp ansible]# ls
bin delete.yml hosts install.yml roles
wget http://download2.yunwei.edu/shell/galera.tar.gz
[root@dlp ~]# mv config.tar.gz mariadb-galera.tar /etc/ansible/bin/
[root@dlp ~]# cd /etc/ansible/
[root@dlp ansible]# ls
bin delete.yml hosts install.yml roles
[root@dlp ansible]# cat hosts
[all]
192.168.42.121
192.168.42.122
192.168.42.172
[master]
192.168.42.121
[slave1]
192.168.42.122
[slave2]
192.168.42.172
[all:vars]
#定义集群ip
master="192.168.42.121"
slave1="192.168.42.122"
slave2="192.168.42.172"
#部署目录,即 ansible 工作目录
base_dir="/etc/ansible"
#默认软件安装目录
opt_dir="/opt"
在部署节点:
docker pull reg.yunwei.edu/learn/ansible:alpine3
docker run -itd -v /etc/ansible:/etc/ansible -v /etc/kubernetes/:/etc/kubernetes/ -v /root/.kube:/root/.kube -v /usr/local/bin/:/usr/local/bin/ 1acb4fd5df5b /bin/sh
进入容器设置免密钥,测试是否能ping通:
ansible all -m ping
/ # cd /etc/ansible/
/etc/ansible # ls
bin delete.yml hosts install.yml roles
/etc/ansible # ansible-playbook install.yml
二、配置集群
#######node1
docker run -d --net host --name galera1 \
-e WSREP_NODE_ADDRESS=192.168.210.131 \
-p 3306:3306 \
-p 4567:4567/udp \
-p 4567-4568:4567-4568 \
-p 4444:4444 \
-v /opt/mariadb/config/mysql:/etc/mysql \
-v /opt/mariadb/config/data:/var/lib/mysql:Z \
--restart=always \
panubo/mariadb-galera mysqld
#######node2
docker run -d --net host --name galera2 \
-e WSREP_NODE_ADDRESS=192.168.210.130 \
-e WSREP_CLUSTER_ADDRESS=gcomm://192.168.210.131:4567,192.168.210.130:4567,192.168.210.132:4567 \
-p 3306:3306 \
-p 4567:4567/udp \
-p 4567-4568:4567-4568 \
-p 4444:4444 \
-v /opt/mariadb/config/mysql:/etc/mysql \
-v /opt/mariadb/config/data:/var/lib/mysql:Z \
--restart=always \
panubo/mariadb-galera mysqld
#########node3
docker run -d --net host --name galera3 \
-e WSREP_NODE_ADDRESS=192.168.210.132 \
-e WSREP_CLUSTER_ADDRESS=gcomm://192.168.210.131:4567,192.168.210.130:4567,192.168.210.132:4567 \
-e MYSQL_ROOT_PASSWORD=123 \
-p 3306:3306 \
-p 4567:4567/udp \
-p 4567-4568:4567-4568 \
-p 4444:4444 \
-v /opt/mariadb/config/mysql:/etc/mysql \
-v /opt/mariadb/config/data:/var/lib/mysql:Z \
--restart=always \
panubo/mariadb-galera mysqld --wsrep-new-cluster
三、查看集群是否同步
[root@node1 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 08ccff9d97ad panubo/mariadb-galera "/galera-entrypoint.…" 26 minutes ago Up 26 minutes galera1 [root@node1 docker]# docker exec -it 08ccff9d97ad sh # mysql -uroot -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.32-MariaDB-1~jessie mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.34 sec) MariaDB [(none)]> create database test1; Query OK, 1 row affected (0.30 sec) [root@node2 ~]# docker exec -it d8b804886305 sh # mysql -uroot -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.1.32-MariaDB-1~jessie mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test1 | +--------------------+ 5 rows in set (0.73 sec)
四、登陆数据库时,安装mariadb客户端
yum install mariadb -y
mysql -h 192.168.210.131 -P 3306 -u root -p123
检查集群状态:
SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
五、故障恢复:
(1)集群中某slave节点down机,重启该节点后,重启docke容器即可;
(2)集群中master节点down机,重启该master节点后,master-docker容器不能正常启动,因为这是master节点已经切换,
需要将原先master-docker容器删除,再以非wrsep方式启动一个普通容器;
(3)集群全部意外down机,强制找一台节点作为master节点;到如下路径,将 safe_to_bootstrap: 0修改为1(平时集群正常时,每个节点都为0)
[root@con3 data]# pwd
/opt/mariadb/data
[root@con3 data]# cat grastate.dat
# GALERA saved state
version: 2.1
uuid: 6a26abec-e7b5-11e8-b15b-b74046986de4
seqno: -1
safe_to_bootstrap: 0
之后在该节点启动wsrep进程的docker