consul是HashCorp公司推出使用go语言编写的开源工具,用于实现分布式系统的服务发现与配置,其特性如下:
环境需求:
主机 | IP | 所需软件 |
---|---|---|
主节点 | 20.0.0.111 | Docker-ce、Consul |
nginx服务器 | 20.0.0.112 | Docker-ce、registrator |
[root@localhost ~]# mkdir /consul
[root@localhost ~]# cd /consul/
[root@localhost consul]# rz -E '//上传压缩包'
rz waiting to receive.
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip -d /usr/bin '//解压'
Archive: consul_0.9.2_linux_amd64.zip
inflating: /usr/bin/consul
[root@localhost consul]# consul agent \ '//设置代理'
> -server \ '//服务功能'
> -bootstrap \ '//参与选举'
> -ui \ '//提供web界面'
> -data-dir=/var/lib/consul-data \ '//提供一个代理存储数据目录'
> -bind=20.0.0.111 \ '//绑定本地地址'
> -client=0.0.0.0 \ '//面对的客户端地址'
> -node=consul-server01 &> /var/log/consul.log& ' //定义节点名称,日志混合输出到log,并且放到后台运行'
[1] 38462
[root@localhost consul]# jobs
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.111 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@localhost consul]# consul members '//查看集群信息'
Node Address Status Type Build Protocol DC
consul-server01 20.0.0.111:8301 alive server 0.9.2 2 dc1
[root@localhost consul]# consul info |grep leader '//查看管理信息'
leader = true
leader_addr = 20.0.0.111:8300
[root@localhost ~]# docker run -d \
> --name=registrator \ '//定义容器名称'
> --net=host \ '定义网络'
> -v /var/run/docker.sock:/tmp/docker.sock \ '指定数据卷,存储信息'
> --restart=always \
> gliderlabs/registrator:latest \ '定义镜像'
> -ip=20.0.0.112 \ '指定本地地址'
> consul://20.0.0.111:8500 '指定consul管理节点地址、端口'
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
703ee87d399d gliderlabs/registrator:latest "/bin/registrator -i…" 2 minutes ago Restarting (1) 31 seconds ago registrator
[root@localhost ~]# docker run -itd -p 83:80 --name test -h test nginx '//创建容器,用来测试服务发现功能是否正常'
[root@localhost ~]# docker run -itd -p 84:80 --name test-02 -h test02 nginx
[root@localhost ~]# docker run -itd -p 85:80 --name test-03 -h test03 httpd
[root@localhost ~]# docker run -itd -p 86:80 --name test-04 -h test04 httpd
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb5045196611 httpd "httpd-foreground" 7 seconds ago Up 7 seconds 0.0.0.0:86->80/tcp test-04
03473a1137bd httpd "httpd-foreground" 10 seconds ago Up 9 seconds 0.0.0.0:85->80/tcp test-03
2e2cee1e6494 nginx "nginx -g 'daemon of…" 39 seconds ago Up 38 seconds 0.0.0.0:84->80/tcp test-02
87a47780cfd1 nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:83->80/tcp test-01
[root@localhost consul]# firewall-cmd --get-active-zones
public
interfaces: ens33
[root@localhost consul]# firewall-cmd --zone=public --add-port=8500/tcp '//设置防火墙规则或者关闭防火墙'
success
compose-template简介:
主节点安装consul-template,准备模板文件
[root@localhost consul]# rz -E '//上传consul-template软件包'
rz waiting to receive.
[root@localhost consul]# ls
consul-template_0.19.3_linux_amd64.zip
[root@localhost consul]# unzip consul-template_0.19.3_linux_amd64.zip -d /usr/bin
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: /usr/bin/consul-template
主节点编写template nginx模板文件
[root@localhost consul]# mkdir /var/log/nginx
[root@localhost consul]# vim nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 88;
server_name localhost 20.0.0.111;
access_log /var/log/nginx/tang.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@localhost consul]# yum install gcc gcc-c++ make expat* pcre* perl* zlib* -y '//安装环境报'
[root@localhost consul]# rz -E '//上传源码包并编译'
rz waiting to receive.
[root@localhost consul]# ls
consul-template_0.19.3_linux_amd64.zip nginx-1.12.2.tar.gz nginx.ctmpl
[root@localhost consul]# tar zxf nginx-1.12.2.tar.gz
[root@localhost consul]# ls
consul-template_0.19.3_linux_amd64.zip nginx-1.12.2 nginx-1.12.2.tar.gz nginx.ctmpl
[root@localhost consul]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost/ '//创建虚拟主机目录'
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf '//编辑配置文件,添加目录'
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
include vhost/*.conf; '//添加路径'
default_type application/octet-stream;
...省略内容
[root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx '/启动nginx'
[root@localhost nginx-1.12.2]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5819/nginx: master
[root@localhost nginx-1.12.2]# firewall-cmd --zone=public --add-port=80/tcp '//防火墙规则设置80端口'
[root@localhost consul]# consul-template -consul-addr 20.0.0.111:8500 \
> -template "/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/tang.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info '//指定模板路径,/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/tang.conf,然后重载nginx -s reload'
接下来会进入监控状态
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/tang.conf
upstream http_backend {
server 20.0.0.112:83; '//能够通过consul自动发现nginx服务器的IP地址与端口'
server 20.0.0.112:84;
}
server {
listen 88;
server_name localhost 20.0.0.112;
access_log /var/log/nginx/tang.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@localhost ~]# docker logs -f test-01 '//nginx服务器动态查看test-01日志'
[root@localhost ~]# docker logs -f test-02 '//重新打开终端,动态查看test-02日志'
[root@localhost ~]# firewall-cmd --zone=public --add-port=88/tcp '//主节点设置防火墙规则,放通88端口'
success
[root@localhost ~]# docker run -itd --name test-06 -p 89:80 -h test06 nginx
136853509dee16aeda4afe1b2d5bd5277341ef8157d6976745188005c69cb5c7 '//nginx服务器添加一个容器'
[root@localhost consul]# cat /usr/local/nginx/conf/vhost/tang.conf
upstream http_backend {
server 20.0.0.112:83;
server 20.0.0.112:84;
server 20.0.0.112:89; '//已经自动更新'
}
server {
listen 88;
server_name localhost 20.0.0.112;
access_log /var/log/nginx/tang.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;
}
}