YAML是一种标记语言很直观的数据序列化格式
文件格式及编写注意事项:
字段 | 描述 |
---|---|
build dockerfile context | 指定Dockerfile文件名构建镜像.上下文路径 |
image | 指定镜像 |
command | 执行命令,覆盖默认命令 |
container name | 指定容器名称,由于容器名称是唯一的, 如果指定自定义名称,则无法scale |
deploy | 指定部署和运行服务相关配置,只能在Swarm模式使用 |
environment | 添加环境变量 |
networks | 加入网络 |
ports | 暴露容器端口,与-p相同,但端口不能低于60 |
volumes | 挂载宿主机路径或命令卷 |
restart | 重启策略,默认no,always, no-failure,unless-stoped |
hostname | 容器主机名 |
字段 | 描述 |
---|---|
build | 重新构建服务,触发dockerfile文件 |
ps | 列出容器 |
up | 创建和启动容器,等同于run |
exec | 在容器里面执行命令 |
scale | 指定一个服务容器启动数量 |
top | 显示容器进程 |
logs | 查看容器输出 |
down | 删除容器、网络、数据卷和镜像 |
stop/start/restart | 停止/启动/重启服务 |
Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
环境:
ip 服务
192.168.220.101 docker concul agent/server/template (Docker-ce、 Compose 3、Consul、 Consul-template)nginx手工编译安装
192.168.220.102 docker nginx httpd
(Docker-ce、registrator)
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 633 100 633 0 0 837 0 --:--:-- --:--:-- --:--:-- 837
100 10.3M 100 10.3M 0 0 3338k 0 0:00:03 0:00:03 --:--:-- 5982k
//或者上传docker_compose即可
[root@server ~]# ls
anaconda-ks.cfg docker-compose initial-setup-ks.cfg
[root@server ~]# chmod +x docker-compose //加执行权限
[root@server ~]# cp -p docker-compose /usr/local/bin
[root@server ~]# mkdir /root/compose_nginx
[root@server ~]# cd compose_nginx/
[root@server compose_nginx]# mkdir nginx wwwroot
[root@docker compose_nginx]# cd nginx/ //上传nginx压缩包
[root@docker nginx]# ls
nginx-1.12.2.tar.gz
[root@docker nginx]# vim Dockerfile
FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
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 /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]# cd ../wwwroot/
[root@docker wwwroot]# vim index.html
this is web!!
[root@docker wwwroot]# vim /root/compose_nginx/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 nginx]# docker-compose -f docker-compose.yml up -d //基于yml文件运行启动
[root@docker compose_nginx]# docker-compose ps //使用Compose容器编排查询
Name Command State Ports
------------------------------------------------------------------------------------
compose_nginx_nginx_1 /usr/local/nginx/sbin/ngi Up 0.0.0.0:1217->443/tcp,::
n ... :1217->443/tcp, 0.0.0.0:
1216->80/tcp,:::1216->80
/tcp
[root@docker compose_nginx]# tree ./
./
├── docker-compose.yml
├── nginx
│?? ├── Dockerfile
│?? └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
2 directories, 4 files
环境:
ip 服务器
192.168.220.101 Docker-ce、 Compose 3、Consul、 Consul-template
192.168.220.102 Docker-ce、registrator
[root@docker ~]# mkdir consul
[root@docker ~]# cd consul/ //上传consul_0.9.2_linux_amd64.zip
[root@docker consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@docker consul]# mv consul /usr/local/bin/
[root@docker consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.220.101 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 112096
[root@docker consul]# jobs //查看状态
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.220.101 -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.220.101:8301 alive server 0.9.2 2 dc1
[root@docker consul]# consul info | grep leader
leader = true
leader_addr = 192.168.220.101:8300
curl 127.0.0.1:8500/v1/status/peers //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader //集群 Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看 nginx 服务信息
[root@docker consul]# curl 127.0.0.1:8500/v1/catalog/nodes
[{"ID":"a2de1f8a-7b6c-4104-60ca-2fff58911b76","Node":"consul-server01","Address":"192.168.220.101","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.220.101","wan":"192.168.220.101"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
可检查容器运行状态自动注册,还可注销 docker 容器的服务到服务配置中心。
目前支持 Consul、Etcd 和 SkyDNS2
[root@server consul]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.220.102 \
> consul://192.168.220.101:8500
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
浏览器输入 http://192.168.220.101:8500,“单击 NODES”,然后单击 “consurl-server01”,会出现 5 个服务
Consul-Template 是一个守护进程,用于实时查询 Consul 集群信息,并更新文件系统 上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新 操作,重新加载 Nginx。Consul-Template 可以查询 Consul 中的服务目录、Key、Key-values 等。
这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。
例如:创建 Apache/Nginx Proxy Balancers、Haproxy Backends
[root@docker consul]# vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}}; #此处引用的变量会指向后端的地址和端口(动态变化)
{{end}}
}
server {
listen 83; ##ngixn的监听端口
server_name localhost 192.168.220.101; #反向代理的IP地址(前端展示的NG服务的IP)
access_log /var/log/nginx/klj.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;
}
}
[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 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 115474/nginx: maste
[root@docker opt]# ls
consul-template_0.19.3_linux_amd64.zip containerd nginx-1.12.2 rh stress
[root@docker opt]# unzip consul-template_0.19.3_linux_amd64.zip
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.220.101:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/xzw.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
再开另一个终端查看
[root@docker opt]# cat /usr/local/nginx/conf/vhost/xzw.conf
1.增加一个nginx容器节点
[root@server ~]# docker run -itd -p:86:80 --name test-06 -h test06 nginx
3ef0368ef74d07c5e7eb62f83d8a351f5fd289ab76137c84494e6f01efac65ca
2.删除一个nginx容器节点
[root@server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ef0368ef74d nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:86->80/tcp, :::86->80/tcp test-06
210f8895fe36 httpd "httpd-foreground" 23 minutes ago Up 23 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
b020cd501576 httpd "httpd-foreground" 23 minutes ago Up 23 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
8b178f5a3f77 nginx "/docker-entrypoint.…" 24 minutes ago Up 23 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
2fb4bc1aebbf nginx "/docker-entrypoint.…" 24 minutes ago Up 24 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
520b77bec639 gliderlabs/registrator:latest "/bin/registrator -i…" 24 minutes ago Up 24 minutes registrator
[root@server ~]# docker rm -f 8b178f5a3f77
8b178f5a3f77