docker-compose+nginx(openresty)+lua实现灰度发布(三),


灰度发布 (一):  https://www.jianshu.com/p/4b5da484d339

灰度发布 (二):  https://www.jianshu.com/p/0d2e808ce8b4

最终用 nginx+lua实现 灰度发布篇:


copy docker-compose文件内容如果格式不对: 就去 https://www.bejson.com/validators/yaml/

这个 校验一下格式. 废话不多说 直接贴内容:


nginx.conf 内容:

#user nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include      mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush    on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    lua_package_path "/home/?.lua;;";

    #gzip  on;

    server {

        listen      80;

        server_name  localhost;

default_type  text/html;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        add_header Myip $remote_addr; #加个自定义头,看效果

        #location / {

        #    root  html;

        #    index  index.html index.htm;

        #}

location / {

content_by_lua_file /home/hello.lua;

}

location @old{

proxy_pass http://myjava1:8080;

}

location @new{

proxy_pass http://myjava2:8080;

}

#location / {

        #    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_buffering off;

        #    proxy_pass http://$weburl;

        #}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

    }

}


hello.lua 内容:

local function close_redis(red)

    if not red then 

        return 

    end 

    local pool_max_idle_time = 10000 --毫秒 

    local pool_size = 100 --连接池大小 

    local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) 

    if not ok then 

        ngx.say("set keepalive error : ", err) 

    end 

end

local redis = require("resty.redis")

local red = redis:new()

red:set_timeout(1000) 

local ip = "192.138.0.2"

local port = 6379

local ok, err = red:connect(ip, port) 

if not ok then 

    ngx.say("connect to redis error : ", err) 

    return close_redis(red) 

end

local headers=ngx.req.get_headers()

local cip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"

local resp, err = red:get(cip)

if not resp then 

    ngx.say("get msg error : ", err) 

    return close_redis(red) 

end 

if resp == ngx.null then 

    resp = '' 

end

close_redis(red)

if "1" == resp then

ngx.log(ngx.ERR, "resp:", 1)

ngx.exec("@new")

return

end

ngx.log(ngx.ERR, "resp !!!:", 1)

ngx.exec("@old")


docker-compose 内容:

version: '3'

services:

  redis:

  image: redis:3.2

  container_name: redis

  ports:

    - 6379:6379

  networks:

    myjavanet:

        ipv4_address: 192.138.0.2

  myjava1:

  image: registry.cn-hangzhou.aliyuncs.com/wuy2009123/tomcat8-1

  container_name: myjava1

  ports:

    - 8081:8080

  volumes:

    - /root/tom/java-docker-compose/tomcat-users.xml:/usr/local/tomcat8/conf/tomcat-users.xml

    - /root/tom/java-docker-compose/manager.xml:/usr/local/tomcat8/conf/Catalina/localhost/manager.xml

    - /root/tom/java-docker-compose/index1.html:/usr/local/tomcat8/webapps/ROOT/index.html

  command: ["catalina.sh","run"]

  networks:

    myjavanet:


  myjava2:

  image: registry.cn-hangzhou.aliyuncs.com/wuy2009123/tomcat8-1

  container_name: myjava2

  ports:

    - 8082:8080

  volumes:

    - /root/tom/java-docker-compose/tomcat-users.xml:/usr/local/tomcat8/conf/tomcat-users.xml

    - /root/tom/java-docker-compose/manager.xml:/usr/local/tomcat8/conf/Catalina/localhost/manager.xml

    - /root/tom/java-docker-compose/index2.html:/usr/local/tomcat8/webapps/ROOT/index.html

  command: ["catalina.sh","run"]

  networks:

    myjavanet:

  openresty:

  image: registry.cn-hangzhou.aliyuncs.com/wuy2009123/openresty-1

  ports:

    - 88:80

  volumes:

    - /root/tom/java-docker-compose/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf

    - /root/tom/java-docker-compose/hello.lua:/home/hello.lua

  networks:

    myjavanet:

networks:

  myjavanet:

    driver: bridge

    ipam:

      config:

        - subnet: 192.138.0.0/16

相关内容

    暂无相关文章