目录

1.容器服务更新与发现

2.consul部署

1.在主节点上部署consul

2.从节点上 容器服务自动注册到consul集群

1.安装Gliderlabs/Registrator(插件) Gliderlabs/Registrator

2.测试服务发现功能是否正常

3.验证nginx和http服务是否注册到consul

4.安装consul-template

5.准备template nginx模板文件(在consul服务器上)

6.编译安装nginx(在consul服务器上)

​7.测试 


1.容器服务更新与发现

容器服务更新与发现:先发现再更新,发现的是后端节点上容器的变化(registrator),更新的是nginx配置文件(agent)

registrator:是consul安插在docker容器里的眼线,用于监听监控节点上容器的变化(增加或减少,或者宕机),一旦有变化会把这些信息告诉并注册在consul server端(使用回调和协程的方式,所以它的延迟和资源消耗会很少),consul server发生一旦发生注册列表的变化后,会把注册的信息告诉agent

agent(代理):用来控制consul template模板,用template组件去和nginx.conf来进行对接,模板里全是变量,用变量的方式去加载后端由注册到consul server端之后,server端会把信息告诉agent,agent和template进行对接,写入template,template就有了镜像,更新完之后会作为nginx.conf子配置文件被前端的nginx识别,consul agent会控制reload之后会识别nginx.conf配置文件中的变化,相当于识别后端的节点,就可以在地址池中动态调整自己后端资源。

2.consul部署

192.168.68.30Docker-ce、Consul、Consul-template、nginx主节点
192.168.68.105Docker-ce、 registratornginx

template 模板(更新)

registrator(自动发现+注册到consul-server端)

后端每更新一个容器,会向registrator进行注册,控制consul完成更新操作,consul会触发consultemplate模板进行热更新(reload)

核心机制: consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)

1.在主节点上部署consul

#主节点
[root@docker ~]# systemctl stop firewalld.service 
[root@docker ~]# setenforce 0
[root@docker ~]# hostnamectl set-hostname consul
[root@docker ~]# su
[root@server ~]# mkdir /root/consul
[root@server ~]# cd /root/consul/
##将压缩包传入到/consul目录下
[root@server /consul]# ls
consul_0.9.2_linux_amd64.zip
[root@server /consul]# unzip consul_0.9.2_linux_amd64.zip      
[root@server /consul]# ls
consul  consul_0.9.2_linux_amd64.zip
[root@server /consul]# cp consul /usr/bin/
[root@server /consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.68.30 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 21822
#查看群集信息,可以看到当前节点
[root@server /consul]# consul members
Node             Address             Status  Type    Build  Protocol  DC
consul-server01  192.168.68.30:8301  alive   server  0.9.2  2         dc1
#查看管理信息,leader就是领袖
[root@server /consul]# consul info |grep leader
	leader = true
	leader_addr = 192.168.68.30:8300
[root@server /consul]# curl 127.0.0.1:8500/v1/catalog/nodes
[{"ID":"d4947593-4daf-1bb8-1753-3badc16eca1a","Node":"consul-server01","Address":"192.168.68.30","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.68.30","wan":"192.168.68.30"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]

curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息

[root@server consul]# consul agent \				      #设置代理
> -server \										    #服务功能
> -bootstrap \									    #参与选举领袖
> -ui \											   #提供web访问界面
> -data-dir=/var/lib/consul-data \					 #提供一个代理存储数据目录
> -bind=192.168.68.30 \							#绑定本机地址
> -client=0.0.0.0 \									#面对的客户端地址(所有) ——registrator
> -node=consul-server01 &> /var/log/consul.log &	   #定义节点名称,日志混合输出到log,并且放到后台运行
jobs -l												#查看当前终端放入后台的工作,并列出进程的PID号
consul members										 #查看群集信息
consul info|grep leader							      #查看管理信息,leader就是领袖

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)服务信息'
curl 127.0.0.1:8500/v1/catalog/nodes     	'//集群节点详细信息'

2.从节点上 容器服务自动注册到consul集群

1.安装Gliderlabs/Registrator(插件) Gliderlabs/Registrator

可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。(consul 8300——>8500展示)

目前支持consul、Etcd(自动发现、分布式。Etcd——分布式数据库——自动发现)和SkyDNS2。

容器的特性:短周期,IP地址会变化

[root@localhost ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.68.105 \
> consul://192.168.68.30:8500
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS          
a414b9109660   gliderlabs/registrator:latest   "/bin/registrator -i…"   4 minutes ago   Restarting (2)

2.测试服务发现功能是否正常

[root@localhost ~]# docker run -itd -p:81:80 --name oyyy1 -h oyyy01 nginx    
`-h选项表示指定host主机名称`
[root@localhost ~]# docker run -itd -p:82:80 --name oyyy2 -h oyyy02 nginx
[root@localhost ~]# docker run -itd -p:83:80 --name oyyy3 -h oyyy03 httpd
[root@localhost ~]# docker run -itd -p:84:80 --name oyyy4 -h oyyy04 httpd

3.验证nginx和http服务是否注册到consul

在consul服务器上查看服务

报错点

4.安装consul-template

Consul-Template 是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。 更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。Consul-Template可以查询Consul中的服务目录、key、key-values等。 这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件.

5.准备template nginx模板文件(在consul服务器上)

[root@consul ~/consul]# vim nginx.ctmpl
upstream http_backend {         协议的后端
  {{range service "nginx"}}     服务的范围只收集nginx信息
   server {{.Address}}:{{.Port}};   此处引用的变量会指向后端的地址和端口(动态变化)
   {{end}}
}

server {
  listen 84;
  server_name localhost 192.168.68.30;    反向代理的服务器地址,前端服务器地址
  access_log /var/log/nginx/oyyy.com-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;
  }
}
#将consul-template_0.19.3_linux_amd64.zip传到/opt目录下
[root@consul /opt]# unzip consul-template_0.19.3_linux_amd64.zip 
[root@consul /opt]# mv consul-template /usr/bin/
#关联nginx虚拟目录中的子配置文件操作
[root@consul opt]# mv consul-template /usr/bin
[root@consul /opt]# consul-template \
> -consul-addr 192.168.68.30:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/oyyy.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info      

#此时另外打开一个终端
[root@consul /opt]# cd /usr/local/nginx/conf/vhost
[root@consul /usr/local/nginx/conf/vhost]# ls
oyyy.conf
##已经出现配置文件
[root@consul /usr/local/nginx/conf/vhost]# cat oyyy.conf 
upstream http_backend {                               
                            
   server 192.168.68.105:81;     
                             
   server 192.168.68.105:82;     
   
}

server {
  listen 81;
  server_name localhost 192.168.68.30;
  access_log /var/log/nginx/oyyy.com-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;
  }
}

6.编译安装nginx(在consul服务器上)

[root@consul /usr/local/sbin]# ss -natp |grep nginx 
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=4834,fd=6),("nginx",pid=4833,fd=6))
[root@consul ~/consul]# yum install gcc pcre-devel zlib-devel -y 
#将nginx-1.12.0.tar.gz压缩包传入到/opt目录下
[root@consul /opt]# rz -E
rz waiting to receive.
[root@consul /opt]# ls
nginx-1.12.0.tar.gz
[root@consul /opt]# tar zxf nginx-1.12.0.tar.gz 
[root@consul /opt/nginx-1.12.0]# ./configure --prefix=/usr/local/nginx
[root@consul /opt/nginx-1.12.0]# make && make install
[root@consul /opt/nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
 19     include       vhost/*.conf
[root@consul /usr/local/nginx/conf]# mkdir vhost
[root@consul /usr/local/nginx/conf]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@consul /usr/local/sbin]# nginx 
[root@consul /usr/local/sbin]# ss -natp |grep nginx 
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=4834,fd=6),("nginx",pid=4833,fd=6))

7.测试 

[root@docker ~]# docker run -itd -p:85:80 --name oyyy5 -h oyyy05 nginx

 创建日志文件夹,如果没有consul上的nginx无法启动,必须要有这个目录根据模板内的设定建立

[root@consul /opt]# mkdir -p /var/log/nginx
[root@consul /opt]# nginx -s reload
[root@consul /opt]# curl 127.0.0.1:81

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐