一、部署環境 docker 版本 二、安裝docker(略) 三、創建swarm集群 選擇一臺伺服器作為manager,比如:manager107,在manager107上執行: 將加入口令,比如: 分別拷到worker68和worker80上執行,即可創建swarm集群。 查看集群節點,在mana ...
一、部署環境
序號 | hostname | ip | 備註 |
1 | manager107 | 10.0.3.107 | centos7;3.10.0-957.1.3.el7.x86_64 |
2 | worker68 | 10.0.3.68 | centos7;3.10.0-957.1.3.el7.x86_64 |
3 | worker80 | 10.0.3.80 | centos7;3.10.0-957.1.3.el7.x86_64 |
docker 版本
docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
二、安裝docker(略)
三、創建swarm集群
選擇一臺伺服器作為manager,比如:manager107,在manager107上執行:
docker swarm init
將加入口令,比如:
docker swarm join --token SWMTKN-1-06on43srtyq7pzogivo8hvja2lvew7zey9wpdf9cy1h8ju18nc-4l3ic9ur29h8yb30ntl0f2v6x 10.0.3.107:2377
分別拷到worker68和worker80上執行,即可創建swarm集群。
查看集群節點,在manager107上執行:
docker node ls
結果類似:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 2uuuareav7n9phpfhmpnfscml * manager107 Ready Active Leader 18.09.0 w0val5pdhhnhgwzuvbtb2qys9 worker68 Ready Active 18.09.0 h4bcpsgw2q9r46qbgbd8erbx7 worker80 Ready Active 18.09.0
四、創建一個overlay網路用於mysql集群之間通信
docker network create -d overlay --attachable pxc-net
查看network:
docker network ls NETWORK ID NAME DRIVER SCOPE 846a843726ca bridge bridge local 35ca60480ba7 docker_gwbridge bridge local fd148d0596c6 host host local 8f8ohxutlvam ingress overlay swarm bd8bdb46dd54 none null local qlvzoa8gmi3k pxc-net overlay swarm
五、拉取percona-xtradb-cluster鏡像,並改名
docker pull docker.io/percona/percona-xtradb-cluster
修改tag
docker tag percona/percona-xtradb-cluster pxc
六、分別在三台伺服器上創建數據捲和備份捲
在manager107上執行:
[root@manager107 ~]# docker volume create v1
[root@manager107 ~]# docker volume create backup
在worker68上執行:
[root@worker68 ~]# docker volume create v2
[root@worker68 ~]# docker volume create backup
在worker80上執行:
[root@worker80 ~]# docker volume create v3
[root@worker80 ~]# docker volume create backup
七、創建基於percona-xtradb-cluster集群的mysql容器
在manager107上執行:
[root@manager107 ~]# docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 -v v1:/var/lib/mysql -v backup:/data --privileged --name=node1 --net=pxc-net pxc
在worker68上執行:
[root@worker68 ~]# docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 -e CLUSTER_JOIN=node1 -v v2:/var/lib/mysql -v backup:/data --privileged --name=node2 --net=pxc-net pxc
在worker80上執行:
[root@worker80 ~]# docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 -e CLUSTER_JOIN=node1 -v v3:/var/lib/mysql -v backup:/data --privileged --name=node3 --net=pxc-net pxc
說明:
- CLUSTER_NAME:集群名稱
- XTRABACKUP_PASSWORD:同步密碼
- CLUSTER_JOIN:加入的集群節點
好了,現在可以隨便連接一臺伺服器的3310介面,訪問mysql,對該資料庫的任何操作都可以實時同步到另外兩台。
八、可能出現的問題
容器node1在manager107上正常運行,容器node2或者node3無法啟動
進入manager107上的node1容器中的mysql
docker exec -it node1 bash
查看mysql日誌
more /var/lib/mysql/innobackup.backup.log
可能出現錯誤(用戶沒有RELOAD和PROCESS許可權)
Error: failed to execute query LOCK TABLES FOR BACKUP: Access denied; you need (at least one of) the RELOAD privilege(s) for this operation Error: failed to execute query SHOW ENGINE INNODB STATUS: Access denied; you need (at least one of) the PROCESS privilege(s) for this operation
然後進入mysql
mysql -uroot -p
查看是否有“xtrabackup”和“sstuser”兩個用戶
use mysql; select * from user;
沒有的話,先創建這兩個用戶,有的話直接授權
創建用戶:
create user 'xtrabackup'@'localhost' identified by '123456'; create user 'sstuser'@'localhost' identified by '123456';
或者查看許可權(以“xtrabackup”為例):
select user,host,Reload_priv,Process_priv from user where user='xtrabackup';
授權:
grant reload, process,create tablespace,lock tables ,replication client,super on *.* to 'xtrabackup'@localhost;
刷新許可權:
flush privileges;