应用栈(APP Stack)是指由多个不同功能的应用相互联通,以集群的方式实现某一特定功能的组合。基于Docker集群构建的某一应用称为Docker应用栈。Docker应用栈本质上是一个分布式业务系统。
假设我们将药用Docker集群搭建一个带有负载均衡和数据库系统高可用的Web系统,它包括一个前端代理应用、两个Web应用和一个一主二从的数据库集群,其构成示意图如下:
利用Docker构建分布式应用栈_第1张图片

假定Proxy选用了HAProxy,Web应用使用了Metinfo,DB选用了MySQL,OS环境采用CentOS:
一、从Docker Hub上下载对应的镜像
docker pull centos
Docker pull metinfo
docker pull haproxy
docker pull mysql
二、启动各容器并用 --link连接相关容器
docker run --link mysql:mysql --name centos /bin/bash
docker run -it --name mysqlmaster mysql /bin/bash
docker run -it --name mysqlslave0 --link mysqlmaster:master mysql /bin/bash
docker run -it --name mysqlslave1 --link mysqlmaster:master mysql /bin/bash
docker run -it --name metinfo0 --link mysqlmaster:db -v ~/program/metinfo0:/usr/src/app metinfo /bin/bash
docker run -it --name metinfo1 --link mysqlmaster:db -v ~/program/metinfo1:/usr/src/app metinfo /bin/bash
docker run -it --name haproxy --link metinfo0:metinfo0 --link metinfo1:metinfo1 -p 6301:6301 -v ~/program/haproxy:tmp haproxy /bin/bash
三、配置各节点容器种的应用服务,以实现相应的功能和通信写作。
四、应用栈的访问测试,此处以WordPress和MySQL的单节点组合为例展示:
[googlebigtable@localhost ~]$ su root
Password:
[root@localhost googlebigtable]# pwd -P
/home/googlebigtable
[root@localhost googlebigtable]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/googlebigtable/.local/bin:/home/googlebigtable/bin
[root@localhost googlebigtable]# cd /home/googlebigtable/temp/dockerworking/
[root@localhost dockerworking]# pwd -P
/home/googlebigtable/temp/dockerworking
[root@localhost dockerworking]# ls -F
gbtwithsshv0.tar
[root@localhost dockerworking]# docker pull wordpress:latest
latest: Pulling from library/wordpress
afb6ec6fdc1c: Pull complete
3d895574014b: Pull complete
c309fdad6410: Pull complete
c201f6a5d6f9: Pull complete
e87f853892aa: Pull complete
998b2113b400: Pull complete
b3c0b4710d3b: Pull complete
c79fb2b38801: Pull complete
30aa6f0dd423: Pull complete
8af9a337c36d: Pull complete
64ec85e06910: Pull complete
606f88b4f608: Pull complete
845e768a44c5: Pull complete
232824f4bf64: Pull complete
c14e31cd46f2: Pull complete
df59d99840f5: Pull complete
0f3a9380af13: Pull complete
348bafbbcb22: Pull complete
d96f27aa3b63: Pull complete
58f35ab6fddc: Pull complete
0c1a0d01788e: Pull complete
Digest: sha256:0b452b7b45fa770f12e864720abb01bef506f4abe273669402434e94323c97d7
Status: Downloaded newer image for wordpress:latest
docker.io/library/wordpress:latest
[root@localhost dockerworking]# docker pull mysql:5.5
5.5: Pulling from library/mysql
743f2d6c1f65: Pull complete
3f0c413ee255: Pull complete
aef1ef8f1aac: Pull complete
f9ee573e34cb: Pull complete
3f237e01f153: Pull complete
03da1e065b16: Pull complete
04087a801070: Pull complete
7efd5395ab31: Pull complete
1b5cc03aaac8: Pull complete
2b7adaec9998: Pull complete
385b8f96a9ba: Pull complete
Digest: sha256:12da85ab88aedfdf39455872fb044f607c32fdc233cd59f1d26769fbf439b045
Status: Downloaded newer image for mysql:5.5
docker.io/library/mysql:5.5
[root@localhost dockerworking]#
[root@localhost dockerworking]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gbtwithssh v0 a371467d9077 19 hours ago 326MB
wordpress latest 675af3ca3193 4 days ago 540MB
mysql 5.5 d404d78aa797 12 months ago 205MB
centos 7.5.1804 cf49811e3cdb 14 months ago 200MB
[root@localhost dockerworking]#docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5
c90e359d5af088a71645b11d8b15653fcdbd3e10d7b00302a62a33cc59cf75c4
[root@localhost dockerworking]#docker run -itd --name wordpress -e WORDPRESS_DB_HOST=mysql -e WORDPRESS_DB_PASSORD=root -p 8080:80 --link mysql:mysql -d wordpress
38cb9c00ee5fe19121142909ca91df94c327063ab7b3d15863e34def4c5b1ce3
[root@localhost dockerworking]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38cb9c00ee5f wordpress "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 0.0.0.0:8080->80/tcp wordpress
c90e359d5af0 mysql:5.5 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp mysql
8c6eb8f8d4ba cf49811e3cdb "/usr/sbin/init" 20 hours ago Exited (137) 11 hours ago ssh
b2dfd487a2f3 cf49811e3cdb "/usr/sbin/init" 20 hours ago Exited (137) 20 hours ago great_taussig
89e35ee3bc0e cf49811e3cdb "/bin/bash" 20 hours ago Exited (137) 20 hours ago centos7.5
[root@localhost dockerworking]#
[root@localhost dockerworking]# ifconfig
docker0: flags=4163 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:ffff:fe0e:f21 prefixlen 64 scopeid 0x20
ether 02:42:ff:0e:0f:21 txqueuelen 0 (Ethernet)
RX packets 82 bytes 52537 (51.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 95 bytes 12026 (11.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags=4163 mtu 1500
inet 192.168.20.198 netmask 255.255.255.0 broadcast 192.168.20.255
inet6 fe80::b6ce:41aa:1a84:e55 prefixlen 64 scopeid 0x20
ether 00:0c:29:a5:3b:4a txqueuelen 1000 (Ethernet)
RX packets 311949 bytes 412505340 (393.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 127795 bytes 7973734 (7.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 144 bytes 41710 (40.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 144 bytes 41710 (40.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

veth40e86a0: flags=4163 mtu 1500
inet6 fe80::8060:66ff:fe4a:fc26 prefixlen 64 scopeid 0x20
ether 82:60:66:4a:fc:26 txqueuelen 0 (Ethernet)
RX packets 10 bytes 755 (755.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1244 (1.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vethe6ed6e5: flags=4163 mtu 1500
inet6 fe80::98e5:50ff:fed8:5c63 prefixlen 64 scopeid 0x20
ether 9a:e5:50:d8:5c:63 txqueuelen 0 (Ethernet)
RX packets 8 bytes 588 (588.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18 bytes 1411 (1.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:0a:f7:2c txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost dockerworking]#
利用Docker构建分布式应用栈_第2张图片

孟伯,20200520

交流联系:微信 1807479153 ,QQ 1807479153