Docker容器--Consul部署

一、简介

1、概述

consul是google开源的一个使用go语言开发的服务管理软件。支持多数据中心、分布式高可用的、服务发现和配置共享。采用Raft算法,用来保证服务的高可用。内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。
服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server 和 client。 每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。
安装consul是用于服务注册,也就是容器本身的一些信息注册到consul里面,其他程序可以通过consul获取注册的相关服务信息,这就是服务注册与发现。

2、Consul两种模式

client模式下,所有注册到当前节点的服务会被转发到server节点,本身是不持久化这些信息。
server模式下,功能和client模式相似,唯一不同的是,它会把所有的信息持久化到本地,这样遇到故障,信息是可以被保留的。
server-leader是所有server节点的老大,它和其它server节点不同的是,它需要负责同步注册的信息给其它的server节点,同时也要负责各个节点的健康监测。

二、Consul特性

1、特性

  • 支持健康检查、允许存储键值对

  • 基于Golong语言,可移植性强

  • 支持ACL访问控制

2、应用场景

Consul的应用场景包括服务发现、服务隔离、服务配置:

服务发现场景中consul作为注册中心,服务地址被注册到consul中以后,可以使用consul提供的dns、http接口查询,consul支持health check。
服务隔离场景中consul支持以服务为单位设置访问策略,能同时支持经典的平台和新兴的平台,支持tls证书分发,service-to-service加密。
服务配置场景中consul提供key-value数据存储功能,并且能将变动迅速地通知出去,借助Consul可以实现配置共享,需要读取配置的服务可以从Consul中读取到准确的配置信息。
Consul可以帮助系统管理者更清晰的了解复杂系统内部的系统架构,运维人员可以将Consul看成一种监控软件,也可以看成一种资产(资源)管理系统。

三、部署Consul集群(Server端)

1、建立Consul服务

[root@localhost ~]#mkdir /opt/consul
[root@localhost ~]#rz -E
rz waiting to receive.
[root@localhost ~]#cp consul_0.9.2_linux_amd64.zip /opt/consul
[root@localhost ~]#cd /opt/consul/
[root@localhost consul]#unzip consul_0.9.2_linux_amd64.zip 
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul                  
[root@localhost consul]#mv consul /usr/local/bin/

在这里插入图片描述

2、设置代理,在后台启动consul服务端

[root@localhost consul]#consul  agent \
 -server \
 -bootstrap \
 -ui \
 -data-dir=/var/lib/consul-data \
 -bind=192.168.10.130 \
 -client=0.0.0.0 \
 -node=consul-server01 &> /var/log/consul.log &

在这里插入图片描述

3、查看集群信息

[root@localhost consul]#consul members  #查看members状态
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.10.130:8301  alive   server  0.9.2  2         dc1

[root@localhost consul]#consul operator raft list-peers  #查看集群状态
Node             ID                   Address              State   Voter  RaftProtocol
consul-server01  192.168.10.130:8300  192.168.10.130:8300  leader  true   2

[root@localhost consul]#consul info | grep leader   #查看详细信息
	leader = true
	leader_addr = 192.168.10.130:8300

在这里插入图片描述
在这里插入图片描述

四、Consul部署(Client端)

1、安装Gliderlabs/Registrator Gliderlabs/Registrator

容器服务自动加入nginx集群

[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.10.132 \
 consul://192.168.10.130:8500

在这里插入图片描述
在这里插入图片描述

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

[root@localhost ~]#systemctl restart docker
[root@localhost ~]#docker run -itd -p:83:80 --name test-01 -h test01 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
2438a4c3f02984fab72ea57ac0c0dcc08b8a264bb0e6717039dd6ae50b46e824
[root@localhost ~]#docker run -itd -p:84:80 --name test-02 -h test02 nginx
62bde8ed1e6f63f96445752c3074c1d0c713b5fdcdd255427810bf17beee709d
[root@localhost ~]#docker run -itd -p:88:80 --name test-03 -h test03 httpd
faebe153b94b49bcb49cd4bbb91768a8480b1a30bea35ffd3fffec20bcdd181a
[root@localhost ~]#docker run -itd -p:89:80 --name test-04 -h test04 httpd
c6098db0e786373b3a842e4e4b3589b230effeaa1a7095607eb2d92b13cbf9ac
[root@localhost ~]#

在这里插入图片描述

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

浏览器中,输入 http://192.168.10.130:8500,在 Web 页面中“单击 NODES”,然后单击“consurl-server01”,会出现 5 个服务。
在这里插入图片描述
在这里插入图片描述

五、Consul-Template

Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。

Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。

1、配置template自动更新

Server端

[root@localhost consul]#vim nginx.ctmpl
#定义nginx upstream一个简单模板
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};
   {{end}}
}

#定义一个server,监听8000端口,反向代理到upstream
server {
    listen 8000;
    server_name localhost 192.168.10.130;
    access_log /var/log/nginx/zzt.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;
    }
}
#保存退出

在这里插入图片描述

2、编译安装nginx

[root@localhost consul]#yum install  -y pcre-devel zlib-devel gcc gcc-c++ make
[root@localhost consul]#useradd  -M -s /sbin/nologin nginx
[root@localhost consul]#cd /opt/
[root@localhost opt]#rz -E  #上传nginx软件包
rz waiting to receive.
[root@localhost opt]#tar zxf nginx-1.12.0.tar.gz 
[root@localhost opt]#cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j && make install
[root@localhost nginx-1.12.0]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

在这里插入图片描述
在这里插入图片描述

3、配置nginx

[root@localhost nginx-1.12.0]#vim /usr/local/nginx/conf/nginx.conf

include vhost/*.conf;
#保存退出
[root@localhost nginx-1.12.0]#mkdir /usr/local/nginx/conf/vhost   #创建虚拟主机目录
[root@localhost nginx-1.12.0]#mkdir /var/log/nginx   #创建日志文件目录
[root@localhost nginx-1.12.0]#/usr/local/nginx/sbin/nginx   #启动nginx

在这里插入图片描述
在这里插入图片描述

4、配置并启动 template

[root@localhost opt]#rz -E   #上传所需包
rz waiting to receive.
[root@localhost opt]#unzip consul-template_0.19.3_linux_amd64.zip 
[root@localhost opt]#mv consul-template /usr/local/bin/

在这里插入图片描述

[root@localhost opt]#consul-template --consul-addr 192.168.10.130:8500 --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/zzt.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

在这里插入图片描述
在开一个终端
在这里插入图片描述

在Client

[root@localhost ~]#run -itd -p:85:80 --name test-05 -h test05 nginx

在这里插入图片描述
在这里插入图片描述

5、测试访问代理服务器

是否可以完成代理访问轮询
192.168.10.130

在Client

[root@localhost ~]#run -itd -p:85:80 --name test-05 -h test05 nginx

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值