https://blog.csdn.net/weixin_42953006/article/details/105671388
如上图:
nginx提供出nginx.conf配置文件,定义在consul template的模板中,在整个容器中增加内容时,
consultemplate会控制其中的配置文件进行自动更新,如反向代理的配置文件,后端的web上都安装
registrator工具,(registrator优先发现容器变动)用于自动发现web中的容器是否进行了变动从而带
动consul template进行更新。
[root@consul ~]# [root@consul ~]# systemctl stop firewalld
[root@consul ~]# setenforce 0
[root@consul ~]# yum install gcc gcc-c++ pcre-devel zlib-devel -y
[root@consul ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@consul ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@consul ~]# cd /opt/nginx-1.12.2/
[root@consul nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@consul nginx-1.12.2]# make && make install
[root@consul nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
##在http字段中添加 “include vhost/*.conf;”
http {
include mime.types;
include vhost/*.conf;
default_type application/octet-stream;
[root@consul nginx-1.12.2]# mkdir /var/log/nginx ##创建nginx日志目录
[root@consul nginx-1.12.2]# /usr/local/nginx/sbin/nginx ##启动服务
[root@consul nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48582/nginx: master
[root@consul nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost ##创建虚拟主机目录
[root@consul nginx-1.12.2]# mkdir /var/log/nginx ##创建日志文件目录
[root@consul ~]# mkdir /root/consul
[root@consul ~]# unzip consul_0.9.2_linux_amd64.zip
[root@consul ~]# mv consul /usr/bin ##移动工具到/usr/bin让系统能识别
[root@consul ~]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.5.233 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[root@consul ~]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.5.233:8301 alive server 0.9.2 2 dc1
[root@consul ~]# consul info |grep leader
leader = true
leader_addr = 192.168.5.233:8300
[root@consul ~]# netstat -natp | grep 8500
tcp6 0 0 :::8500 :::* LISTEN 45930/consul
[root@consul ~]# vim /root/consul/nginx.ctmpl
##添加以下内容
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 1111;
server_name localhost 192.168.5.233;
access_log /var/log/nginx/ng.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@client ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.5.129 \
> consul://192.168.5.233:8500
[root@consul ~]# unzip consul-template_0.19.3_linux_amd64.zip
[root@consul ~]# mv consul-template /usr/bin
[root@consul ~]# consul-template -consul-addr 192.168.5.233:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ng.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
2020/04/22 16:58:18.987120 [INFO] consul-template v0.19.3 (ebf2d3d)
2020/04/22 16:58:18.987149 [INFO] (runner) creating new runner (dry: false, once: false)
2020/04/22 16:58:18.988963 [INFO] (runner) creating watcher
2020/04/22 16:58:18.989530 [INFO] (runner) starting
2020/04/22 16:58:18.989563 [INFO] (runner) initiating run
2020/04/22 16:58:19.002724 [INFO] (runner) initiating run
[root@consul ~]# ls /usr/local/nginx/conf/vhost/
ng.conf
[root@consul ~]# cat /usr/local/nginx/conf/vhost/ng.conf
uptream http_backend {
}
server {
listen 1111;
server_name localhost 192.168.5.233;
access_log /var/log/nginx/ng.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@client ~]# docker run -itd -p:100:80 --name test-01 -h test01 nginx
[root@client ~]# docker run -itd -p:200:80 --name test-02 -h test02 nginx
[root@client ~]# docker logs -f test-01