Updating your upstream list and run lua scripts without reloading Nginx.
Slardar is a HTTP load balancer based on Nginx and lua-nginx-module, by which you can update your upstream list and run lua scripts without reloading Nginx.
This bundle is maintained by UPYUN(又拍云) Inc.
Because most of the nginx modules are developed by the bundle maintainers, it can ensure that all these modules are played well together.
The bundled software components are copyrighted by the respective copyright holders.
1. Clone the repository
git clone https://github.com/upyun/slardar.git
2. Set installation directory (optional)
By default, Slardar will be installed to /usr/local/slardar
, and you should ensure that you have write permission to the directory.
If you want to change to another location, you should export the PREFIX
environment variable to the path you want to install.
export PREFIX=/path/to/your/dir
3. Configure
cd slardar
make configure
4. Build and Install
make
make install
5. Run
/usr/local/slardar/nginx/sbin/nginx
or you have changed installation directory in step 2:
$PREFIX/nginx/sbin/nginx
1. Clone the repository
git clone https://github.com/upyun/slardar.git
2. Build docker image
cd slardar
docker build -t slardar .
3. Run
docker run -d --net=host --name slardar slardar
Contiguration file is in lua
format and located at /usr/local/slardar/nginx/app/etc/config.lua
or $PREFIX/nginx/app/etc/config.lua
if you changed your installation location.
Example configuration and the comments are listed as follows.
local _M = {}
_M.global = {
-- checkups send heartbeats to backend servers every 5s.
checkup_timer_interval = 5,
-- checkups timer key will expire in every 60s.
-- In most cases, you don't need to change this value.
checkup_timer_overtime = 60,
-- checkups will sent heartbeat to servers by default.
default_heartbeat_enable = true,
-- create upstream syncer for each worker.
-- If set to false, dynamic upstream will not work properly.
-- This switch is used for compatibility purpose only in checkups,
-- don't change this in slardar.
checkup_shd_sync_enable = true,
-- sync upstream list from shared memory every 1s
shd_config_timer_interval = 1,
}
_M.consul = {
-- connect to consul will timeout in 5s.
timeout = 5,
-- disable checkups heartbeat to consul.
enable = false,
-- consul k/v prefix.
-- Slardar will read upstream list from config/slardar/upstreams.
-- For more information, please refer to 'Consul configuration'.
config_key_prefix = "config/slardar/",
-- positive cache ttl(in seconds) for dynamic configurations from consul.
config_positive_ttl = 10,
-- negative cache ttl(in seconds) for dynamic configurations from consul.
config_negative_ttl = 5,
-- do not cache dynamic configurations from consul.
config_cache_enable = true,
cluster = {
{
servers = {
-- change these to your own consul http addresses
{ host = "10.0.5.108", port = 8500 },
{ host = "10.0.5.109", port = 8500 },
},
},
},
}
return _M
Slardar will read persisted configurations, upstream list and lua code from consul on startup. Consul configuration can be customized by setting k/v with the prefix config_key_prefix
(e.g.config/slardar/
) configured in config.lua
. You should ensure that all values behind config_key_prefix
are in valid json
format.
An example Consul keys and their corresponding values are listed as follows,
consul k/v key | value |
---|---|
lua/modules.abc | local f = {version=10} return f |
lua/script.test | local f = require("modules.abc") print(f.version) |
upstreams/node-dev.example | {"enable": true, "servers": [{"host": "127.0.0.1","port": 8001,"weight": 1,"max_fails": 6,"fail_timeout": 30}]} |
myargs | {"arg0": 0,"arg1": 1} |
For the above example, Slardar will load modules.abc
, script.test
as lua code and node-dev.example
as upstream on startup.
You can set "enable": false
(default is true
) in your upstream configuration to disable periodical heartbeats to servers by checkups.
When Slardar is running, you can use slardar.myargs.arg0
to get arg0
and slardar.myargs.arg1
to get arg1
. The config will be cached for config_positive_ttl
seconds. That is to say, when you change the value of myargs
in consul, it will take effect in config_positive_ttl
seconds.
Differs to configurations like myargs
, keys behind lua
and upstreams
will not be cached and you can only update them by Slardar's HTTP interfaces.
If you don't need any preload scripts or upstreams, just leave nothing behind config_key_prefix
or an empty value.
Slardar is 100% compatible with nginx, so you can change nginx configuration files in the same way you do for Nginx.
Configuration files for Nginx are located at /usr/local/slardar/nginx/conf
or $PREFIX/nginx/conf
if you changed your installation location.