blank
blank
发布于 2019-11-12 / 992 阅读 / 0 评论 / 0 点赞

swarm mysql (mariadb)集群 高可用

基于 swarm 的自动容灾 路由 负载均衡的 mysql 集群

创建数据库专用网络

docker network create -d overlay mydbnet

Init/Bootstrap DB Cluster

初始化配置数据库集群

Note: the service name provided by --name has to match the environment variable DB_SERVICE_NAME set with --env DB_SERVICE_NAME. 注意: 服务名称 --name 必须跟 环境变量中的 --env DB_SERVICE_NAME 一致

docker service create --name dbcluster \
--network mydbnet \
--replicas=1 \
--env DB_SERVICE_NAME=dbcluster \
--env MYSQL_ROOT_PASSWORD=rootpass \
--env MYSQL_DATABASE=mydb \
--env MYSQL_USER=mydbuser \
--env MYSQL_PASSWORD=mydbpass \
toughiq/mariadb-cluster

Scale out additional cluster members Just after the first service instance/task is running with we are good to scale out. Check service with docker service ps dbcluster. The result should look like this, with CURRENT STATE telling something like Running.

伸缩数据库容器数量

我们先检查当前运行的数据库服务状态 CURRENT STATE = Running 表示正常运行中
在当前服务运行成功后即可进行提升容器数量

docker service ps dbcluster
ID                  NAME                IMAGE                            NODE                DESIRED STATE       CURRENT STATE           ERROR                              PORTS
3w0yeaid2m9h        dbcluster.1         toughiq/mariadb-cluster:latest   node16              Running             Running 41 seconds ago 

Lets scale out now:

现在让我们来提高运行容器数量

docker service scale dbcluster=3

This additional 2 nodes start will come up in "cluster join"-mode. Lets check again: docker service ps dbcluster 额外的 2 个容器节点将会以cluster join 模式运行
我们再次检查当前运行的数据库服务

docker service ps dbcluster
ID                  NAME                IMAGE                            NODE                DESIRED STATE       CURRENT STATE           ERROR                              PORTS
3w0yeaid2m9h        dbcluster.1         toughiq/mariadb-cluster:latest   node16              Running             Running 5 minutes ago
vcuru16shl3a        dbcluster.2         toughiq/mariadb-cluster:latest   node12              Running             Running 1 minutes ago
41hhixg2rjdf        dbcluster.3         toughiq/mariadb-cluster:latest   node13              Running             Running 1 minutes ago

Create MaxScale Proxy Service and connect to DBCluster

There is no absolute need for a MaxScale Proxy service with this Docker Swarm enabled DB cluster, since Swarm provides a loadbalancer. So it would be possible to connect to the cluster by using the loadbalancer DNS name, which is in our case dbcluster. Its the same name, which is provided at startup by --name.

But MaxScale provides some additional features regarding loadbalancing database traffic. And its an easy way to get information on the status of the cluster.

Details on this MaxScale image can be found here: https://github.com/toughIQ/docker-maxscale

创建 利用 MaxScale 代理服务来连接刚刚创建的数据库集群

MaxScale 详情请参阅 https://github.com/toughIQ/docker-maxscale

docker service create --name maxscale \
--network mydbnet \
--env DB_SERVICE_NAME=dbcluster \
--env ENABLE_ROOT_USER=1 \
--publish 3306:3306 \
toughiq/maxscale

To disable root access to the database via MaxScale just set --env ENABLE_ROOT_USER=0 or remove this line at all. Root access is disabled by default.
可以使用环境变量 --env ENABLE_ROOT_USER=0 或直接去掉这一行来禁用默认 root 用户连接
Check successful startup of Cluster & MaxScale
检查 MaxScale 和集群是否成功启动
Execute the following command. Just use autocompletion to get the SLOT and ID.
执行以下命令
docker exec -it maxscale.<SLOT>.<ID> maxadmin -pmariadb list servers
The result should report the cluster up and running:
结果应该是 Master/Slave Running

docker exec -it maxscale.1.j27fb7fzrgyk7on4zjl8lgmh0 maxadmin -pmariadb list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
10.0.2.8           | 10.0.2.8        |  3306 |           1 | Master, Synced, Running
10.0.2.17          | 10.0.2.17       |  3306 |           1 | Slave, Synced, Running
10.0.2.15          | 10.0.2.15       |  3306 |           1 | Slave, Synced, Running
-------------------+-----------------+-------+-------------+--------------------

连接到任意一台 swarm ip :3306 来测试连接此时应该都 OK

参阅 基于 mariadbswarm 集群 https://hub.docker.com/r/toughiq/mariadb-clusterP


评论