docker-compose概述
docker compose前身是Fig,它是一个运行多个docker容器的工具,使用 docker compose不再需要使用shell脚本来启动容器,适合组合使用多个容器进行并发的场景
YAML语言
YAML是一种标记语言很直观的数据序列化格式
YAML格式:
- 不支持制表符tab键缩进,需要使用空格缩进
- 通常开头缩进两个空格
- 字符后缩进一个空格,如冒号、逗号、横杠
- #表示注释
- 如果包含特殊字符用单引号引起来
- 布尔值不行用引号括起来
Docker Compose配置常用字段
字段 | 描述 |
---|
build dockerfile context | 指定Dockerfile文件名构建镜像上下文路径 | image | 指定镜像 | command | 执行命令,覆盖默认命令 | container name | 指定容器名称 | deploy | 指定部署和运行服务相关配置,只能在Swarm模式使用 | environment | 添加环境变量 | networks | 加入网络 | ports | 暴露容器端口,与-p相同,但端口不能低于60 | volumes | 挂载宿主机路径或命令卷 | restart | 重启策略,默认no,always,no-failure,unless-stoped | hostname | 容器主机名 |
Docker Compose常用命令
build | 重新构建服务 |
---|
ps | 列出容器 | up | 创建和启动容器 | exec | 在容器里面执行命令 | scale | 指定一个服务容器启动数量 | top | 显示容器进程 | logs | 查看容器输出 | down | 删除容器、网络、数据卷和镜像 | stop/start/restart | 停止/启动/重启服务 |
Consul
Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
特性:
- 支持健康检查,运行存储键值对
- 基于Golong语言,可移植性强
- 支持ACL访问控制
- 与docker等轻量级容器可无缝配合
compose部署
设备:192.168.118.44(Docker-ce、 Compose 3、Consul、 Consul-template) 192.168.118.88(Docker-ce、registrator)
192.168.118.44:
[root@docker ~]
[root@docker ~]
[root@docker compose_nginx]
[root@docker compose_nginx]
[root@docker nginx]
[root@docker nginx]
[root@docker tomcat]
[root@docker tomcat]
[root@docker compose_nginx]
[root@docker compose_nginx]
.
├── nginx
│ ├── Dockerfile
│ └── nginx-1.15.9.tar.gz
└── wwwroot
└── Dockerfile
[root@docker compose_nginx]
[root@docker nginx]
FROM centos:7 as build
ADD nginx-1.15.9.tar.gz /opt
WORKDIR /opt/nginx-1.15.9
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && \
make install &> /dev/null && \
rm -rf /opt/nginx-1.15.9
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]
[root@docker wwwroot]
this is NG web
- 下载compose(上传docker_compose)
[root@docker ~]
[root@docker ~]
[root@docker compose_nginx]
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[root@docker compose_nginx]
[root@docker compose_nginx]
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------------------------
compose_nginx_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:1217->443/tcp,:::1217->443/tcp,
0.0.0.0:1216->80/tcp,:::1216->80/tcp
- 查看浏览器192.168.118.44:1216
Consul部署
设备:centos7,安装dockers(consul、registrator(自动发现),template(更新)) 192.2168.118.44:
[root@docker ~]
[root@docker ~]
[root@docker consul]
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@cons[root@consul consul]
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.118.44 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
ul consul]
[root@docker consul]
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.118.44 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@docker consul]
Node Address Status Type Build Protocol DC
consul-server01 192.168.118.44:8301 alive server 0.9.2 2 dc1
[root@docker
consul]
leader = true
leader_addr = 192.168.118.44:8300
- 在192.168.118.88上安装 Gliderlabs/Registrator
root@docker nginx]
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.118.88 \
> consul://192.168.118.44:8500
[root@docker nginx]
[root@docker nginx]
[root@docker nginx]
[root@docker nginx]
浏览器访问192.168.118.44:8500,查看添加的容器服务
安装 consul-template
192.168.118.44节点上安装配置
[root@docker consul]
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 192.168.118.44;
access_log /var/log/nginx/whd.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
[root@docker consul]
[root@docker consul]
[root@docker consul]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
[root@docker nginx-1.12.2]
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28727/nginx: master
[root@docker opt]
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@docker opt]
[root@docker opt]
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/whd.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
[root@docker opt]
upstream http_backend {
server 192.168.118.88:83;
server 192.168.118.88:84;
}
server {
listen 83;
server_name localhost 192.168.226.128;
access_log /var/log/nginx/whd.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
- 在192.168.118.88节点新建容器,确认注册到consul
[root@server docker]
dcc0eb9e689df6ef3128b4474627bbc85e4f8229f99c081888aa91425c3192c3
- 在192.168.118.44节点再关联一次nginx 虚拟目录中的子配置文件,查看文件内容是否有新的server添加
[root@docker opt]
[root@docker opt]
upstream http_backend {
server 192.168.118.88:83;
server 192.168.118.88:84;
server 192.168.118.88:85;
}
|