企业运维实战--Docker三剑客二之docker-compose
前言-docker compose简介
Docker Compose是一种编排服务,基于pyhton语言实现,是一个用于在 Docker 上定义并运行复杂应用的工具,可以让用户在集群中部署分布式应用。用户可以很容易地用一个配置文件定义一个多容器的应用,然后使用一条指令安装这个应用的所有依赖,完成构建。 解决了容器与容器之间如何管理编排的问题。
Docker Compose 中有两个重要的概念: 服务 (service) :一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。 项目 (project) :由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。

docker compose实践
docker-compose安装
与docker-machine类似将二进制包移动到/usr/local/bin/docker-compose即可  用户在命令行输入命令后,系统会在环境变量$PATH 内找命令。 

docker-compose配置haproxy+nginx负载均衡
家目录下创建项目目录
mkdir compose
编写主配置文件,docker-compose在up的时候会自动读取 cat docker-compose.yml
version: "3.8"
services:
web1:
image: nginx
networks:
- webnet
lumes:
- ./web1:/usr/share/nginx/html
web2:
image: nginx
networks:
- webnet
lumes:
- ./web2:/usr/share/nginx/html
haproxy:
image: haproxy
networks:
- webnet
volumes:
- ./haproxy:/usr/local/etc/haproxy
ports:
- "80:80"
networks:
webnet:
创建haproxy目录并编辑修改haproxy.cfg的主配置文件
cat haproxy/haproxy.cfg
global
maxconn 65535
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 200
gid 200
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
retries 2
option redispatch
timeout connect 5s
timeout server 5s
stats uri /admin/stats
frontend public
bind *:80 name clear
default_backend dynamic
backend dynamic
balance roundrobin
server a web1:80 check inter 1000
server b web2:80 check inter 1000
查看默认发布文件内容
cat web1/index.html
web1
cat web2/index.html
web2
查看~/compose结构 tree ~/compose 
启动服务,-d打入后台
docker-compose up -d

查看compose进程状态,全为up表示状态健康
docker-compose ps
 访问测试
curl 172.25.9.1

容器内haproxy+nginx负载均衡配置完成。
|