docker-compose
- docker-compose概述
- YAML语言
- Docker Compose配置常用字段
- Docker Compose常用命令
- Consul
- compose部署
- Consul部署
- 安装 consul-template
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 ~]# mkdir whd
[root@docker ~]# cd compose_nginx
[root@docker compose_nginx]# mkdir nginx wwwroot
[root@docker compose_nginx]# cd nginx/
[root@docker nginx]# touch Dockerfile
[root@docker nginx]# cd ../tomcat/
[root@docker tomcat]# touch Dockerfile
[root@docker tomcat]# cd ..
[root@docker compose_nginx]# yum install -y tree
[root@docker compose_nginx]# tree -L 2
.
├── nginx
│ ├── Dockerfile
│ └── nginx-1.15.9.tar.gz
└── wwwroot
└── Dockerfile
[root@docker compose_nginx]# cd nginx
[root@docker nginx]# vim Dockerfile ##编写Dockerfile
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]# cd ../wwwroot/
[root@docker wwwroot]# vim index.html
this is NG web
- 下载compose(上传docker_compose)
[root@docker ~]# cp docker-compose /usr/local/bin
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker compose_nginx]# vim docker-compose.yml
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]# docker-compose -f docker-compose.yml up -d
[root@docker compose_nginx]# docker-compose ps
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 ~]# mkdir consul
[root@docker ~]# cd consul/
[root@docker consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@cons[root@consul consul]# 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 &
ul consul]# mv consul /usr/bin
[root@docker consul]# jobs
[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]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.118.44:8301 alive server 0.9.2 2 dc1
[root@docker
consul]# consul info | grep leader
leader = true
leader_addr = 192.168.118.44:8300
- 在192.168.118.88上安装 Gliderlabs/Registrator
root@docker nginx]# docker run -d \ ## 安装 Gliderlabs/Registrator Gliderlabs/Registrator
> --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]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
[root@docker nginx]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
[root@docker nginx]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
[root@docker nginx]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
浏览器访问192.168.118.44:8500,查看添加的容器服务
安装 consul-template
192.168.118.44节点上安装配置
[root@docker consul]# vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}}; #此处引用的变量会指向后端的地址和端口(动态变化)
{{end}}
}
server {
listen 83;
server_name localhost 192.168.118.44; #反向代理的IP地址(前端展示的NG服务的IP)
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; #后端真实IP
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #转发地址
proxy_pass http://http_backend;
}
}
- 手工编译安装nginx
[root@docker consul]# yum install gcc pcre-devel zlib-devel -y
[root@docker consul]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@docker consul]# cd /opt/nginx-1.12.2/
[root@docker nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@docker nginx-1.12.2]# make && make install
[root@docker nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf ##修改配置文件
[root@docker nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost #创建虚拟机目录
[root@docker nginx-1.12.2]# mkdir /var/log/nginx ##创建日志文件目录
[root@docker nginx-1.12.2]# /usr/local/nginx/sbin/nginx ##启动nginx
[root@docker nginx-1.12.2]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28727/nginx: master
- 配置并启动 template
[root@docker opt]# unzip consul-template_0.19.3_linux_amd64.zip ##上去consul-template包并解压
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@docker opt]# mv consul-template /usr/bin/
##关联nginx 虚拟目录中的子配置文件
[root@docker opt]# consul-template -consul-addr 192.168.118.44:8500 \
> -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]# cat /usr/local/nginx/conf/vhost/whd.conf
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]# docker run -itd -p:85:80 --name test-06 -h test06 nginx
dcc0eb9e689df6ef3128b4474627bbc85e4f8229f99c081888aa91425c3192c3
- 在192.168.118.44节点再关联一次nginx 虚拟目录中的子配置文件,查看文件内容是否有新的server添加
[root@docker opt]# consul-template -consul-addr 192.168.118.44:8500 -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]# cat /usr/local/nginx/conf/vhost/whd.conf
upstream http_backend {
server 192.168.118.88:83;
server 192.168.118.88:84;
server 192.168.118.88:85; ##可以看到我们添加的nginx服务
}