nginx-consul-template
概述
Consul-template 是 HashiCorp 基于 Consul 所提供的可扩展的工具,通过监听 Consul中的数据变化,动态地修改一些配置文件中地模板。常用于在 Nginx、HAProxy上动态
配置健康状态下的客户端反向代理信息。
Consul-template 和 nginx 必须安装在同一台机器上,因为 Consul-template 需要动态修改 nginx 的配置文件 nginx.conf,然后执行 nginx -s reload命令进行路由更新,达到动态负载均衡的目的。
nginx-ingress是我自己命名的(实现原理跟k8s的ingress一样),因为此nginx主要是负责访问calico网络内的负载均衡,且calico不支持http协议的穿透,所以外部需要跟inress,通讯的话必须在中间再创建一个nginx-host作为转发,后面会介绍。
原理
通过 Nginx 自身实现负载均衡和请求转发;
通过 Consul-template 的 config 功能实时监控 Consul 集群节点的服务和数据的变化;
实时的用 Consul 节点的信息替换 Nginx 配置文件的模板,并重新加载配置文件;
下载nginx镜像
docker pull docker.io/nginx:latest
创建nginx脚本
官方及网上大部分的启动nginx-consul-template容器最后ENTRYPOINT都为nginx -sreload,但是因为在重制镜像的时候会将nginx镜像中ENTRYPOINT的nginx -g 'daemonoff'给覆盖掉,导致容器在启动的时候nginx没有启动,而nginx -s reload会去读/run/nginx.pid,如果没有则reload失败,所以这里新建了一个nginx启动及重启的脚本。
#!/bin/bash
if nginx -t>/dev/null; then
if [[ -s /var/run/nginx.pid ]]; then
nginx -s reload
if [[ $? != 0 ]]; then
rm -f /var/run/nginx.pid
nginx -c /etc/nginx/nginx.conf
fi
else
nginx -c /etc/nginx/nginx.conf
fi
fi
这里做了3层判断,先检查nginx配置是否正确,然后查看检查nginx.pid是否存在且不为空。容器如果退出,会导致nginx.pid里面的ID号不对,再次启动nginx的时候,nginx-s reload会报错,所以需要再判断nginx -s reload是否正确
创建nginx-consul-template的docker file
vim nginx-consul-template.df
FROM nginx
MAINTAINER Qingwen Zhang <qingwenzhang@quarkfinance.com>
RUN apt-get update && \
apt-get install --no-install-recommends --no-install-suggests -y unzip && \
rm -r /var/lib/apt/lists/*
ENV CONSUL_TEMPLATE_VERSION 0.19.4
ADD https://releases.hashicorp.com/consul-template/${CONSUL_TEMPLATE_VERSION}/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip /tmp/consul-template.zip
ADD nginx.sh /tmp/nginx.sh
RUN chmod +x /tmp/nginx.sh
RUN unzip /tmp/consul-template.zip -d /usr/bin && \
chmod +x /usr/bin/consul-template && \
rm /tmp/consul-template.zip
RUN mkdir /etc/ctmpl
WORKDIR /etc/ctmpl
ENTRYPOINT ["/usr/bin/consul-template"]
创建镜像
docker build -t 172.16.4.92/service/nginx-consul-template -f /opt/dockerfile/nginx-consul-template.df .
docker push 172.16.4.92/service/nginx-consul-template
创建ctmpl模板
mkdir -p /opt/platform/nginx-calico && cd /opt/platform/nginx-calico && mkdir -p conf modules html logs ctmpl
vim /opt/platform/nginx-calico/ctmpl/ctmpl
{{range services}}{{ if in .Tags "calico" }}{{$name := .Name}}{{$service := service .Name}}upstream {{$name}} {
zone upstream-{{$name}} 64k;
{{range $service}} server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
{{end}}}
server {
listen 80;
charset utf-8;
server_name {{$name|toLower|split "-"|join "."}}.test.com;
access_log /var/log/nginx/{{.Name}}.log;
location / {
proxy_pass http://{{$name}};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_send_timeout 150s;
proxy_read_timeout 150s;
proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;
}
}
{{end}}{{end}}
运行nginx-consul-template
docker pull 172.16.4.92/service/nginx-consul-template
docker run -d \
--restart=always \
--net=calico \
--ip=10.233.2.1 \
--label org.projectcalico.label.role=nginx \
-v /opt/platform/nginx-calico/conf:/etc/nginx \
-v /opt/platform/nginx-calico/modules:/usr/lib/nginx/modules \
-v /opt/platform/nginx-calico/html:/usr/share/nginx/html \
-v /opt/platform/nginx-calico/logs:/var/log/nginx \
-v /opt/platform/nginx-calico/ctmpl:/etc/ctmpl \
--name=calico-nginx1-consul-template \
172.16.4.92/service/nginx-consul-template \
-consul-addr=172.16.150.25:8500 -wait=5s \
-template="/etc/ctmpl/ctmpl:/etc/nginx/conf.d/app.conf:/tmp/nginx.sh"
创建calico policy
calico网络有2种控制流量的方式,profile跟policy。
profile设置思路是针对网络的,默认配置名称是跟创建网络名字一样。如果针对容器做策略,需要先创建策略,然后针对容器的网络workloadEndpoint来应用,而容器只要重启,则相应的workloadEndpoint也会变化,所以不建议用profile来做。
policy默认可以精细到具体容器,因为他是针对容器的label来做的,所以只需要在启动容器的时候添加--label org.projectcalico.label.role即容器的默认策略应用的是policy的
vim nginx-policy.yaml
apiVersion: v1
kind: policy
metadata:
name: allow-tcp-80
spec:
selector: role == 'nginx'
types:
- ingress
- egress
ingress:
- action: allow
protocol: tcp
source:
nets:
- 10.233.0.0/16
- 172.0.0.0/8
destination:
ports:
- 80
egress:
- action: allow
calicoctl apply -f nginx-policy.yaml
calicoctl get policy
ingress是入口,egress是出口,我们这里需要外部的nginx-host反向代理到calico网络的nginx-ingress,所以只需要ingress放行指定IP的tcp 80即可。
nginx-consul-template的更多相关文章
- 在Windows环境中使用Nginx, Consul, Consul Template搭建负载均衡和服务发现服务
搭建负载均衡和服务发现服务的目的 随着网站业务的不断提升,单个服务器的性能越来越难满足客户的业务需求,所以很多情况下,需要使用多服务器实例和负载均衡器来满足业务需要. Nginx 什么是Nginx N ...
- Consul Template的简单使用
Consul Template的使用 1安装 地址 https://github.com/hashicorp/consul-template/releases wget https://release ...
- 负载均衡之nginx+consul(自动更新路由)
前几篇先是记载了如何通过nginx配置服务负载均衡,后面记载了如何通过 ocelot 配置 服务负载均衡,分别介绍了用webapi注册服务以及配置文件注册服务,通过ocelot webapi + co ...
- 动态负载均衡(Nginx+Consul+UpSync)
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- Nginx(四) nginx+consul+upasync 在ubnutu18带桌面系统 实现动态负载均衡
1.1 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化 ...
- Nginx Consul nginx-upsync-module
nginx consul nginx-upsync-module 依赖包: yum -y install libpcre3 libpcre3-dev ruby zlib1g-dev patch 下载n ...
- Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架
安装所需软件 Docker Docker-compose 配置docker-compose.yml文件内容如下: #load balancer will automatically update th ...
- 动态负载均衡(Nginx+Consul+UpSync)环境搭建
首先 安装好 Consul upsync 然后: 1.配置安装Nginx 需要做配置,包括分组之类的,创建目录,有些插件是需要存放在这些目录的 groupadd nginx useradd -g ng ...
- Consul&Nginx&Registrator&ConsulTemplate部署高可用负载均衡
1. Consul Server 创建consul server虚拟主机 docker-machine create consul 出现如下内容即创建成功 Running pre-create che ...
- Consul+Nginx部署高可用
1. Consul Server 创建consul server虚拟主机 docker-machine create consul 出现如下内容即创建成功 Running pre-create che ...
随机推荐
- 16.合并两个排序的链表 Java
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解题思路 两种解法:递归和非递归 参考代码 /* public class ListNode { ...
- [Tex学习笔记]章节用罗马字母编号
微信扫描如上二维码关注跟锦数学微信公众账号. 详情请见那里.
- react 实现数据双向绑定
好久没有更新了 只是都写在有道笔记中 今天整理下 一些基础的 大神勿喷 一个基础的不能再基础的数据双向绑定 因为react不同于vue 没有v-model指令 所以怎么实现呢? import Reac ...
- buildscript和allprojects的作用和区别是什么?
在Android Studio的Project的build.gradle中, // Top-level build file where you can add configuration optio ...
- js复选框实现全选、全不选、反选
复选框为checkbox对象 通过input就可以将一个简单的复选框呈现在页面上 <input type="checkbox" /> 要实现的大概就是这样一个页面 思路 ...
- [Python]统计1个元素在列表中的出现次数
使用列表自带的count方法: list.count(element) 示例: 列表a,有4个元素,其中值1出现3次 In []: a=[,,,] In []: a Out[]: [, , , ] ...
- JVM学习笔记之认识JDK(一)
1. HotSpot VM: HotSpot VM是Sun JDK和OpenJDK中所带的虚拟机,也是目前使用范围最广的Java虚拟机. 什么是HotSpot VM & 深入理解Java虚拟机 ...
- [spring]@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...
- Requests API
Requests API http://docs.python-requests.org/en/latest/ requests的具体安装过程请看: http://docs.python-reques ...
- Python异步IO之协程(一):从yield from到async的使用
引言:协程(coroutine)是Python中一直较为难理解的知识,但其在多任务协作中体现的效率又极为的突出.众所周知,Python中执行多任务还可以通过多进程或一个进程中的多线程来执行,但两者之中 ...