Docker-consul + Nginx反向代理群集搭建

一、Consul概述

  • Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现和配置
  • Consul支持健康检查,允许存储键值对
  • 一致性协议采用Raft算法,用来保证服务的高可用
  • 成员管理和消息广播采用GOSSIP协议,支持ACL访问控制
  • 方便部署,与Docker等轻量级容器可无缝配合

二、Consul集群工作流程

在这里插入图片描述

  • consul是微服务中的一个重要内容,在docker中有个服务的更新和发现机制
  • consultemplate:主要用于更新,是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成后,可以选择运行shell命令执行更新操作重新加载nginx服务。
  • registrator:用于发现,可以检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心,目前支持Consul、Etcd和SKyDNS2

如上图:

nginx提供出nginx.conf配置文件,定义在consul template的模板中,在整个容器中增加内容时,

consultemplate会控制其中的配置文件进行自动更新,如反向代理的配置文件,后端的web上都安装

registrator工具,(registrator优先发现容器变动)用于自动发现web中的容器是否进行了变动从而带

动consul template进行更新。

三、Consul+nginx集群部署

3.1、建立Consul服务
  • 每个提供服务的节点上都要部署和运行Consul的agent
  • Consul agent有两种运行模式: Server和Client
  • Server和Client只是Consul群集层面的划分,与搭建在cluster上的应用服务没有关系
3.2、实验环境
  • 两台centos7服务器
  • server端:192.168.5.233/24 ,安装软件:docker-ce、consul、consul-template、nginx反向代理
  • 后端client:192.168.5.129/24,安装软件:docker-ce、registrator,构建nginx容器作为web
3.3、实验目的
  • server端的nginx作为反向代理;
  • 通过配置consul+nginx群集,能够实时监控后端的nginx容器,动态的自动添加或删除后端web到server端的nginx反向代理配置中。
3.4、Server端部署
  • 关闭防火墙,编译安装nginx作为反向代理调度器
[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
  • 配置nginx服务
[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   ##创建日志文件目录

在这里插入图片描述

  • 创建consul工作目录,上传consul软件包并解压
[root@consul ~]# mkdir /root/consul
[root@consul ~]# unzip consul_0.9.2_linux_amd64.zip 
[root@consul ~]# mv consul /usr/bin                      ##移动工具到/usr/bin让系统能识别

  • 配置consul agent
[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 & 
  • 查看当前群集信息,查看consul8500端口是否开启
[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  
  • 配置template nginx模板文件
[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;
  }
}
3.5、Client端部署
  • 安装Gliderlabs/Registrator
[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

3.6、群集功能验证
3.6.1、验证自动发现和更新
  • 在server端配置并启动template
[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

  • 此时在本地的vhost目录中会自动生成ng.conf配置文件,此文件能够根据后端的容器情况,进行动态的更新
[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;
  }
}

在这里插入图片描述

  • 在后端的client中创建2个nginx容器作为测试,发现nginx容器能够自动加入
[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

  • 再次查看vhost中的ng.conf文件

在这里插入图片描述

3.6.2、验证nginx反向代理
  • 网页带1111端口访问server端nginx调度器地址192.168.5.129:1111,自动跳转到后端web

在这里插入图片描述

  • 进入client端查看nginx容器的访问日志,发现源地址是server端的nginx调度器地址
[root@client ~]# docker logs -f test-01

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker+Consul+Nginx+keepalived是一种常用的云原生架构方案,它结合了容器化、服务发现、负载均衡和高可用等多种技术手段,可用于构建高可用、弹性、可扩展的应用系统。 该方案的主要组件包括: 1. Docker:用于容器化应用程序和服务,提供了高效、轻量、可移植的应用打包和部署方式。 2. Consul:用于服务发现和配置管理,支持多数据中心、跨平台、高度可扩展的分布式系统。 3. Nginx:用于负载均衡和反向代理,支持高并发、高可用的流量分发。 4. keepalived:用于实现高可用的服务和节点,提供了基于 VRRP 协议的故障转移和自动切换功能。 在该方案中,Docker 容器作为应用程序和服务的运行环境,使用 Consul 进行服务注册和发现,并通过 Nginx 进行流量分发和负载均衡。同时,使用 keepalived 实现高可用的服务和节点,确保系统的稳定性和可用性。 项目描述可以按照以下步骤进行撰写: 1. 项目背景和目的:简要介绍本项目的背景和目的,说明为什么选择 Docker+Consul+Nginx+keepalived 方案。 2. 技术架构:详细介绍该方案的技术架构和组件,包括 DockerConsulNginx 和 keepalived 的作用和使用方式。 3. 系统功能:描述系统的主要功能和特点,包括服务发现、负载均衡、高可用等方面。 4. 实现方式:介绍系统的具体实现方式和实现步骤,包括 Docker 镜像的构建、应用程序的容器化、Consul 的配置和使用、Nginx 的配置和使用、keepalived 的配置和使用等。 5. 测试和验证:对系统进行测试和验证,验证系统的功能和性能是否符合预期,是否满足高可用和弹性的要求。 6. 总结和展望:对本项目进行总结和展望,分析该方案的优缺点和适用范围,展望未来的发展方向和趋势。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值