谈到微服务的话题,技术上我们往往会涉及到多服务、多容器的部署与管理。
Docker 有三个主要的作用:Build, Ship和Run。使用docker compose我们可以在Run的层面解决很多实际问题,如:通过创建compose(基于YUML语法)文件,在这个文件上面描述应用的架构,如使用什么镜像、数据卷、网络、绑定服务端口等等,然后再用一条命令就可以管理所有的服务(如启动、停止、重启、日志监控等等)。
本文通过发布3个APP(App1,App2,App3),来演示Docker在服务发布、网络、共享分区以及信息隔离与连通方面的工作:
1.安装Compose:

[root@docker ~]# curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@docker ~]# docker-compose version
docker-compose version 1.9.0, build 2585387
docker-py version: 1.10.6
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

2.服务物理框架
这里写图片描述

说明:
a.Container1和2共在一个network namespace(netName1)内,Container3独立network namespace(netName2)
b.Container1挂载了一个系统卷(/opt/conf)到/mnt
c.Container3挂载了一个系统卷(./app/web)到/usr/share/nginx/html
c.Container1,2,3 nginx监听端口分别为:8080,8081,8082

3.docker-compose.yml 内容

[root@docker compose]# more docker-compose.yml 
version: '2'
services:
  App1:
    image: nginx
    ports:
      - "8080:80"
    networks:
      - "netName1"
    volumes:
      - /opt/conf/:/mnt
  App2:
    image: nginx
    ports:
      - "8081:80"
    networks:
      - "netName1"
    volumes:
      - /opt/conf/:/mnt
  App3:
    image: nginx
    ports:
      - "8082:80"
    networks:
      - "netName2"
networks:
  netName1:
    driver: bridge
  netName2:
    driver: bridge

4.运行

[root@docker compose]# docker-compose up
Creating network "compose_netName1" with driver "bridge"
Creating network "compose_netName2" with driver "bridge"
Creating volume "compose_vol1" with local driver
Creating compose_App2_1
Creating compose_App1_1
Creating compose_App3_1

在后台运行:[root@docker compose]# docker-compose up -d

5.查看容器运行状态

[root@docker compose]# docker-compose ps
     Name              Command          State               Ports             
-----------------------------------------------------------------------------
compose_App1_1   nginx -g daemon off;   Up      443/tcp, 0.0.0.0:8080->80/tcp 
compose_App2_1   nginx -g daemon off;   Up      443/tcp, 0.0.0.0:8081->80/tcp 
compose_App3_1   nginx -g daemon off;   Up      443/tcp, 0.0.0.0:8082->80/tcp 

其他命令:

[root@docker compose]# docker-compose restart #重启所有容器
[root@docker compose]# docker-compose restart App1  #重启App1
[root@docker compose]# docker-compose stop #停止所有容器
[root@docker compose]# docker-compose stop App1  #停止App1

6.验证网络隔离:
a.到App1 ping App2

[root@docker compose]# docker-compose exec App1 bash
root@dd01fa7315ae:/# ping App2
PING App2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.059 ms
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.108 ms
64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.062 ms
64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.062 ms

成功ping通
b.到App2 ping App1和App3

[root@docker compose]# docker-compose exec App2 bash
root@1905b2a875e3:/# ping App1
PING App1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.043 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.089 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.085 ms

App2 to App3-成功

[root@docker compose]# docker-compose exec App3 bash
root@595f744e8634:/# ping App1
PING App1 (172.31.213.13): 56 data bytes
....#App3 to App1失败
root@595f744e8634:/# ping App2
PING App2 (172.31.213.13): 56 data bytes
....#App3 to App2失败

7.验证共享卷
a. 在host /opt/conf/ 目录下新建nginx.conf 文件

[root@docker conf]# pwd
/opt/conf
[root@docker conf]# touch nginx.conf 

b.登录到App1 和App2查看

[root@docker compose]# docker-compose exec App1 bash
root@49d702fc8606:/# cd /mmt 
root@49d702fc8606:/mnt# ls
nginx.conf#成功挂载
[root@docker compose]# docker-compose exec App2 bash
root@1d161428d4dd:/# cd /mnt
root@1d161428d4dd:/mnt# ls
nginx.conf#成功挂载

c. 在App3中挂载文件到nginx
修改docker-compose.yml App3:

App3:
    image: nginx
    volumes:
      - ./app/web/:/usr/share/nginx/html
    ports:
      - "8082:80"
    networks:
      - "netName2"

在host ./app/web 中添加 app3.html

[root@docker web]# app3.html
[root@docker web]# ls
app3.html
[root@docker web]# more app3.html 
<!DOCTYPE html>
<html>
    <head>
         <meta charset="utf-8">
         <title>Welcome to leonyan's home, this is app3</title>
     </head>
   <body>
   Welcome to leonyan's home, this is app3!
   </body>
</html>

[root@docker compose]# cd app/web/

访问App3 nginx:
这里写图片描述

8.总结
本文演示了通过 Docker Compose进行多容器创建,以及容器间服务的管理,如网络隔离、卷数据共享。以及对容器的日常管理。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐