1.环境:
三台centos7,docker 1.12.1
120 manager
121 worker
122 manager
(以下#121 表示 121主机)
2.集群搭建:
#120: docker swarm init Swarm initialized: current node (5bbbwyuc5s05ih0t9x0cru77w) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-52rsedmj5whynm5juyxtj7e7fwqu5xc5k42guc9uhgx0138cns-171u68nez4392iaj19xkq0al1 \ 10.16.147.120:2377 To add a manager to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-52rsedmj5whynm5juyxtj7e7fwqu5xc5k42guc9uhgx0138cns-c3dqg81p080ouybxby61ipsap \ 10.16.147.120:2377 #121: docker swarm join \ --token SWMTKN-1-52rsedmj5whynm5juyxtj7e7fwqu5xc5k42guc9uhgx0138cns-171u68nez4392iaj19xkq0al1 \ 10.16.147.120:2377 #122: docker swarm join \ --token SWMTKN-1-52rsedmj5whynm5juyxtj7e7fwqu5xc5k42guc9uhgx0138cns-c3dqg81p080ouybxby61ipsap \ 10.16.147.120:2377 [root@node122 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 5bbbwyuc5s05ih0t9x0cru77w node120 Ready Active Leader 8eunz61rpy0uaww0owpkr02s5 node121 Ready Active de6wda7z9m0i8bon6njqo08a9 * node122 Ready Active Reachable
3.#121 上安装私有镜像仓库:
docker run --name midea_registry --restart=always -d -p 5000:5000 \ -v /nfs/data_registry:/var/lib/registry registry:2.5 (常用镜像已经保存在/nfs/data_registry,下面可以直接使用。Doker 私有仓库 管理 http://blog.mreald.com/197)
4.#120(或者任一个manager)上创建应用:
docker network create --driver overlay --subnet 10.0.9.0/24 app01_network docker service create --name mysql -e MYSQL_ROOT_PASSWORD=wordpress \ --network app01_network --replicas 3 -p 3306:3306 10.16.147.121:5000/mysql:midea docker service create --name docker_wp --network app01_network \ -e WORDPRESS_DB_PASSWORD=wordpress --replicas 2 -p 8080:80 10.16.147.121:5000/wordpress:midea
为什么要设置-e 参数,因为wordpress 官方镜像配置文件:
[root@node121 ~]# docker exec -ti docker_wp.1.ejlf6o55cl8t8aku607ueysf1 cat /var/www/html/wp-config.php <?php ..... /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'root'); /** MySQL database password */ define('DB_PASSWORD', 'wordpress'); /** MySQL hostname */ define('DB_HOST', 'mysql'); ......
自动会连mysql 名字主机。 用户root 密码wordpress
架构图:
docker 1.12.1 开始支持overlay_network 的域名解析,对应主机名或者容器名。 能连通 但ping 不通
[root@node122 ~]# docker exec -it 22c3322cf576 ping docker_wp PING docker_wp (10.0.9.4): 56 data bytes 92 bytes from 22c3322cf576 (10.0.9.3): Destination Host Unreachable 92 bytes from 22c3322cf576 (10.0.9.3): Destination Host Unreachable 92 bytes from 22c3322cf576 (10.0.9.3): Destination Host Unreachable 92 bytes from 22c3322cf576 (10.0.9.3): Destination Host Unreachable ^C--- docker_wp ping statistics --- 5 packets transmitted, 0 packets received, 100% packet loss
打开下面任何一个,效果都是一样的:
http://10.16.147.120:8080
http://10.16.147.121:8080
http://10.16.147.122:8080
5.查看运行情况:
[root@node122 ~]# docker service ls ID NAME REPLICAS IMAGE COMMAND 7a04ty7q9txj docker_wp 1/1 10.16.147.121:5000/wordpress:midea dnacctn7wx1v mysql 1/1 10.16.147.121:5000/mysql:midea [root@node122 ~]# docker service ps mysql ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 2wr9eljyn7p24uyijemobacjt mysql.1 10.16.147.121:5000/mysql:midea node122 Running Running about an hour ago [root@node122 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 22c3322cf576 10.16.147.121:5000/mysql:midea "docker-entrypoint.sh" About an hour ago Up About an hour 3306/tcp mysql.1.2wr9eljyn7p24uyijemobacjt [root@node122 ~]# docker logs mysql.1.2wr9eljyn7p24uyijemobacjt Initializing database Database initialized MySQL init process in progress... Warning: Unable to load '/usr/share/zoneinfo/Factory' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/posix/Factory' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/right/Factory' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. MySQL init process done. Ready for start up.
对比单主机使用过程:
docker run --name mysql_wordpress -e MYSQL_ROOT_PASSWORD=wordpress \ -d 10.16.147.121:5000/mysql:midea docker run --name docker_wordpress --link mysql_wordpress:mysql \ -p 8080:80 -d 10.16.147.121:5000/wordpress:midea
参考:
http://blog.csdn.net/kongxx/article/details/38676917
http://severalnines.com/blog/mysql-docker-introduction-docker-swarm-mode-and-multi-host-networking
http://blog.csdn.net/lincyang/article/details/43850867
https://yq.aliyun.com/articles/57576