kong000dao0 2020-04-25
[ ~]# mkdir consul [ ~]# cp /opt/consul_0.9.2_linux_amd64.zip /root/consul/ [ ~]# cd /root/consul/ [ consul]# unzip consul_0.9.2_linux_amd64.zip [ consul]# ls consul consul_0.9.2_linux_amd64.zip [ consul]# mv consul /usr/bin/ [ consul]# consul agent \ //使用代理功能 -server \ //提供server功能 -bootstrap \ //参与选举为领袖 -ui \ //提供web访问界面 -data-dir=/var/lib/consul-data \ //参数存储位置 -bind=192.168.7.129 \ //绑定地址 -client=0.0.0.0 \ //面对所有节点终端 -node=consul-server01 &> /var/log/consul.log & //指定本地节点名称,指定日志文件目录,放在后台运行
2.查看集群信息
[ consul]# consul members Node Address Status Type Build Protocol DC consul-server01 192.168.7.129:8301 alive server 0.9.2 2 dc1 [ consul]# consul info | grep leader leader = true leader_addr = 192.168.7.129:8300
3.通过httpd api获取集群信息
[ ~]# curl 127.0.0.1:8500/v1/status/peers //查看集群server成员 [ ~]# curl 127.0.0.1:8500/v1/status/leader //查看领导者信息 [ ~]# curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务 [ ~]# curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx服务信息 [ ~]# curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
二、容器服务自动添加到nginx集群
1.安装Gliderlabs/Registrator
[ ~]# docker run -d --name=registrator \ //指定名称 --net=host \ //指定网络 -v /var/run/docker.sock:/tmp/docker.sock \ //指定数据卷 --restart=always \ //restart功能 gliderlabs/registrator:latest -ip=192.168.7.168 \ //指定本地IP地址 consul://192.168.7.129:8500
2.添加nginx服务
[ ~]# 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 ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 960d01d212c4 nginx "nginx -g ‘daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:84->80/tcp test-02 bc075c1c3670 nginx "nginx -g ‘daemon of…" 59 seconds ago Up 57 seconds 0.0.0.0:83->80/tcp test-01 5dd120cd1232 gliderlabs/registrator:latest "/bin/registrator -i…" 3 minutes ago Up 3 minutes registrator
3.验证nginx服务是否注册到consul
在consul服务器上查看服务
[ consul]# curl 127.0.0.1:8500/v1/catalog/services {"consul":[],"nginx":[]}
4.准备template nginx模板文件
[ ~]# vim /root/consul/nginx.ctmpl upstream http_backend { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 83; server_name localhost 192.168.7.129; access_log /var/log/nginx/kgc.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; } }
5.编译安装并配置nginx
[ ~]# yum install gcc pcre-devel zlib-devel -y [ ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt [ ~]# cd /opt/nginx-1.12.0 [ ~]# ./configure --prefix=/usr/local/nginx [ ~]# make && make install [ ~]# vim /usr/local/nginx/conf/nginx.conf http { include mime.types; include vhost/*.conf; //添加虚拟主机目录 default_type application/octet-stream; #创建虚拟主机目录 [ ~]# mkdir /usr/local/nginx/conf/vhost #创建日志文件目录 [ ~]# mkdir /var/log/nginx #启动nginx /usr/local/nginx/sbin/nginx
6.配置并启动template
上传consul-template_0.19.3_linux_amd64.zip到/root/consul目录下
[ consul]# unzip consul-template_0.19.3_linux_amd64.zip [ consul]# mv consul-template /usr/bin [ consul]# consul-template -consul-addr 192.168.7.129:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/aaa.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info #启动后,会在vhost目录下自动生成aaa.conf配置文件 [ ~]# ls /usr/local/nginx/conf/vhost/ abc.conf
7.增加一个nginx容器节点,测试服务发现及配置更新功能(在registrator服务端增加
[ ~]# docker run -itd -p:85:80 --name test-03 -h test03 nginx`
consul服务器监控会有提示自动更新