• openresty+etcd配置实现原理


    部署etcd

    解压etcd-v3.4.7-linux-amd64.tar.gz文件
    tar -zxvf etcd-v3.4.7-linux-amd64.tar.gz
    启动etcd服务
    ./etcd --logger=zap --data-dir=/etc/etcd --enable-v2=true
    upsync模块只支持v2版本的API,一定要加上--enable-v2=true,默认为禁用

    nginx配置

    [root@k8smaster01 conf]# more nginx.conf
    worker_processes  2;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
         log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_for
    warded_for" "$upstream_addr"  "$request_body"';
        sendfile        on;
        keepalive_timeout  65;

        upstream test {
            sticky name=test_1 path=/;
            upsync 127.0.0.1:2379/v2/keys/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=etcd strong_dependency=off;
    #        upsync 192.168.23.1:8000/encode/etcd/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=etcd strong_dependency=off;
     #        upsync 127.0.0.1:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
            upsync_dump_path /data/soft/openresty-1.13.6.1/nginx/conf/servers_test.conf;
            include /data/soft/openresty-1.13.6.1/nginx/conf/servers_test.conf;

            check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false;
            check_http_send "HEAD / HTTP/1.0\r\n\r\n";
            check_http_expect_alive http_2xx http_3xx;
        }
        server {
            listen       8888;
            location /test {
                 proxy_pass http://test;
            }
            location /upstream_show {
                upstream_show;
            }
            location /upstream_status {
                check_status;
                access_log off;
            }

            location / {
                default_type 'text/html';
                content_by_lua 'ngx.say("hello world")';
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    验证

    1、往etcd写入数据

    [root@k8smaster01 ~]# curl -X PUT http://127.0.0.1:2379/v2/keys/upstreams/test/192.168.23.101:80 -d value=""
    {"action":"set","node":{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":4,"createdIndex":4}}
    [root@k8smaster01 ~]# curl -X PUT http://127.0.0.1:2379/v2/keys/upstreams/test/192.168.23.101:80 -d value=""
    {"action":"set","node":{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":5,"createdIndex":5},"prevNode":{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":4,"createdIndex":4}}
    [root@k8smaster01 ~]#

    2、浏览器查看upstream是否动态更新

     抓包查看底层原理

     1、手工往etcd加入数据

    PUT /v2/keys/upstreams/test/192.168.23.101:80 HTTP/1.1
    User-Agent: curl/7.29.0
    Host: 127.0.0.1:2379
    Accept: */*
    Content-Length: 6
    Content-Type: application/x-www-form-urlencoded

    value=HTTP/1.1 200 OK
    Access-Control-Allow-Headers: accept, content-type, authorization
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Access-Control-Allow-Origin: *
    Content-Type: application/json
    X-Etcd-Cluster-Id: cdf818194e3a8c32
    X-Etcd-Index: 7
    X-Raft-Index: 9
    X-Raft-Term: 3
    Date: Fri, 29 Jul 2022 16:51:31 GMT
    Content-Length: 215

    {"action":"set","node":{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":7,"createdIndex":7},"prevNode":{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":5,"createdIndex":5}}

    2、nginx全量更新etcd的upstream配置

    GET /v2/keys/upstreams/test? HTTP/1.0
    Host: 127.0.0.1
    Accept: */*

    HTTP/1.0 200 OK
    Access-Control-Allow-Headers: accept, content-type, authorization
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Access-Control-Allow-Origin: *
    Content-Type: application/json
    X-Etcd-Cluster-Id: cdf818194e3a8c32
    X-Etcd-Index: 7
    X-Raft-Index: 9
    X-Raft-Term: 3
    Date: Fri, 29 Jul 2022 16:51:31 GMT
    Content-Length: 196

    {"action":"get","node":{"key":"/upstreams/test","dir":true,"nodes":[{"key":"/upstreams/test/192.168.23.101:80","value":"","modifiedIndex":7,"createdIndex":7}],"modifiedIndex":4,"createdIndex":4}}

    3、nginx监听etcd节点变化

    GET /v2/keys/upstreams/test?wait=true&recursive=true&waitIndex=8 HTTP/1.0
    Host: 127.0.0.1
    Accept: */*

    HTTP/1.0 200 OK
    Access-Control-Allow-Headers: accept, content-type, authorization
    Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
    Access-Control-Allow-Origin: *
    Content-Type: application/json
    X-Etcd-Cluster-Id: cdf818194e3a8c32
    X-Etcd-Index: 7
    X-Raft-Index: 9
    X-Raft-Term: 3
    Date: Fri, 29 Jul 2022 16:51:32 GMT

    {"action":"set","node":{"key":"/upstreams/test/192.168.23.102:80","value":"","modifiedIndex":8,"createdIndex":8}}

    4、总结:nginx启动后,一直监听etcd上的upstream节点的变化,如果节点发送变化,触发全量更新nginx对应的upstream的值到内存中,这抓包可以看出,etcd的更新与监听是两个不一样的url,比consul复杂。

     

     

     

  • 相关阅读:
    515. 在每个树行中找最大值
    如何利用三极管实现电平转换
    《FPGA原理与结构》读书笔记(0)-基础知识
    MySQL 教程(三)函数
    【Python百日进阶-WEB开发】Day179 - Django案例:11短信验证码
    Llama2-Chinese项目:3.1-全量参数微调
    TreeMap匿名内部类使用Comparator方法(比较器)被替换
    Fragment转场动画的那些坑(仅分析v4包下的fragment)
    微服务实战微服务网关Zuul入门与实战
    使用paddleX体验
  • 原文地址:https://blog.csdn.net/zhaikaiyun/article/details/126059227