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即可。  

原文地址:https://www.cnblogs.com/cjsblogs/p/9173440.html