• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

rrfeng/lua-resty-upstream-etcd: An OpenResty lua module that can use upstream co ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

rrfeng/lua-resty-upstream-etcd

开源软件地址(OpenSource Url):

https://github.com/rrfeng/lua-resty-upstream-etcd

开源编程语言(OpenSource Language):

Lua 100.0%

开源软件介绍(OpenSource Introduction):

lua-resty-upstream-etcd

A lua module for OpenResty, can dynamically update the upstreams from etcd and kubernetes.

  • Upstream realtime change from etcd or kubernetes apiserver, without reload.
  • Weighted round robin load balancing (not for kubernetes because of no weight settings of pod).
  • Healthcheck
  • Request statistics

REQUIREMENTS

  • openresty-1.9.11.1 and newer
  • balancer_by_lua
  • ngx.worker.id
  • lua-resty-http
  • cjson

USAGE

Prepare Etcd:

etcdctl set /v1/testing/services/${service_name}/${host}:${port} ${value}

The service_name is upstream's name, should be string. The host is upstream's ip address, domain name not supported. The port is upstream's port. The value should be a json, if json parse error, will use the default value below:

{
    "weight": 100,
    "check_url": "/health",
    "slow_start": 30,
    "status": "up"
}
  • weight: upstream peer weight as number, int or float.
  • check_url: for healthcheck, if enabled, checker will do http request to http://${host}:${port}${check_url}.
  • slow_start: number of seconds duration that the upstream peer's weight slowly increased from 0 to ${weight}
  • status: indicates the peer is up or down, must be up if you want the peer work, any other value means peer down.

Init the module:

lua_socket_log_errors off;   # recommend
lua_package_path "/path/of/your/environment...";

lua_shared_dict lreu_shm 1m;     # for global storage
lua_shared_dict lreu_shm_k8s 1m; # for global storage

init_worker_by_lua_block {
    local syncer = require "lreu.syncer"
    syncer.init({
        etcd_host = "127.0.0.1",
        etcd_port = 2379,
        etcd_path = "/v1/testing/services/",
        storage = ngx.shared.lreu_shm
    })

    -- if you want to use k8s
    local syncer_k8s = require "lreu.syncer_k8s"
    sycner_k8s.init({
        apiserver_host = "127.0.0.1",
        apiserver_port = "6443",
        namespace = "default",
        token = "the token",
        storage = ngx.shared.lreu_shm_k8s
    })

    -- init the picker before using it in balancer_by_lua
    local picker = require "lreu.picker"

    -- you can use both etcd and k8s
    picker.init(ngx.shared.lreu_shm, ngx.shared.lreu_shm_k8s, true, ngx.shared.hc)

    -- if you want to use health check
    local health = require "lreu.health"
    health.init({
        storage = ngx.shared.lreu_upstream,
        healthcheck = {
            enable = true,
            timeout = 3,
            interval = 5,
            max_fails = 3,
            ok_status = {200, 204, 301, 302, 401, 403, 404}
        },
        logcheck = {
            enable = true,
            interval = 5,
            recover = 60
        }
    })
}

Use picker in upstream:

upstream test {
    server 127.0.0.1:2222; # fake server

    balancer_by_lua_block {
        local balancer = require "ngx.balancer"
        local picker = require "lreu.picker"
        local s, err = u.rr("my_test_service")
        if not s then
            ngx.log(ngx.ERR, err)
            return ngx.exit(500)
        end
        local ok, err = balancer.set_current_peer(s.host, s.port)
        if not ok then
            ngx.log(ngx.ERR, "failed to set current peer: " .. err)
            return ngx.exit(500)
        end
    }
}

Functions

picker.init(first_shm, second_shm?, merge?, hcshm?)

first_shm and second_shm should be the storage of syncer. One or two. If the second_shm provided, both used. but the merge controls the behavior:

  • merge==true: all peers in both shm merged together for load balancing.
  • merge==false: picker try to find peers in the first_shm, if found, use it; if no peers in first_shm, picker try the second_shm.

hcshm is the storeage of health.lua checker, if not set, will use the first_shm.

Todo

  • More docs to be added.
  • Etcd cluster support.
  • Add more load-balance-alg.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap