• This repository has been archived on 26/Aug/2022
  • Stars
    star
    494
  • Rank 89,130 (Top 2 %)
  • Language
    Lua
  • Created about 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Updating your upstream list and run lua scripts without reloading Nginx.

Slardar

Updating your upstream list and run lua scripts without reloading Nginx.

Table of Contents

Description

Slardar is a HTTP load balancer based on Nginx, lua-nginx-module and stream-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.

Installation

Install from source

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

Back to TOC

Build Docker Image

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

Back to TOC

Configuration

Lua configuration

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,
    
    -- 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

Consul configuration

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.

Nginx configuration

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.

Back to TOC

Interface

Get Slardar status

GET 127.0.0.1:1995/status

Slardar will return its status in json format.

{
	-- checkups heartbeat timer is alive.
	"checkup_timer_alive": true,
	
	-- last heartbeat time
	"last_check_time": "2016-08-12 13:09:40",
	
	-- slardar version
	"slardar_version": "1.0.0",
	
	-- start or reload time.
	"start_time": "2016-08-12 13:09:40",
	
	-- lua config file version, you can set 'conf_hash = "your-version"' in your lua config file.
	"conf_hash": null,
	
	-- every time you update upstream, this value will increase.
	"shd_config_version": 0,
	
	-- status for consul cluster
	"cls:consul": [
		[
			{
				"server": "consul:10.0.5.108:8500",
				"weight": 1,
				"status": "unchecked"
			}
		]
	],
	
	-- status for node-dev cluster
	"cls:node-dev": [
		[
			{
				"server": "node-dev:10.0.5.108:8001",
				"weight": 1,
				"fail_timeout": 30,
				"status": "ok",
				"max_fails": 6
			}
		]
	]
}

Get scripts status

GET 127.0.0.1:1995/lua

Slardar will return loaded lua scripts and modules in json format.

{
	-- every time you update lua scripts, this value will increase.
	"version": 0,
	"modules": [
		{
			-- module load time
			"time": "2016-08-12 13:09:40",
			
			-- md5 value of module file
			"version": "aed4a968ef14f8db732e3602c34dc37a",
			
			-- module name
			"name": "modules.test"
		},
		{
			"time": "2016-08-12 13:09:40",
			"version": "302f9bf40fcd3734cab120b97f18edf3",
			"name": "script.test"
		}
	]
}

Update upstream

POST 127.0.0.1:1995/upstream/name

The request body is your new upstream list in json format. For example,

curl 127.0.0.1:1995/upstream/node-dev.example.com -d \
{"servers":[{"host":"192.168.1.1", "port": 8080}, {"host":"192.168.1.2", "port": 8080}]}

The example above will add two servers into upstream named node-dev.example.com.

Delete upstream

DELETE 127.0.0.1:1995/upstream/name

For example,

curl -XDELETE 127.0.0.1:1995/upstream/node-dev.example.com

The example above will delete the upstream named node-dev.example.com.

Post lua scripts or modules

POST 127.0.0.1:1995/lua/scripts.name

or post a lua module,

POST 127.0.0.1:1995/lua/modules.name

The request body is the lua code of your script or module. For example,

curl 127.0.0.1:1995/lua/scripts.test -d 'return slardar.exit(errno.EXIT_TRY_CODE)'
curl 127.0.0.1:1995/lua/modules.test -d 'local f = {version=10} return f'

Load lua scripts or modules

PUT 127.0.0.1:1995/lua/scripts.name

or load a lua module,

PUT 127.0.0.1:1995/lua/modules.name

Before loading lua, you must post the lua script or module to Slardar.

For example,

curl -XPUT 127.0.0.1:1995/lua/scripts.test
curl -XPUT 127.0.0.1:1995/lua/modules.test

Back to TOC

Example

Get from upstream which does not exist will result in 502.

$ curl 127.0.0.1:8080/ -H "Host: node-dev.example.com"
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>slardar/1.0</center>
</body>
</html>

Add one server to node-dev.example.com

$ curl 127.0.0.1:1995/upstream/node-dev.example.com -d '{"servers":[{"host":"127.0.0.1", "port": 4000}]}'
{"status":200}

Now, we can get the correct result.

$ curl 127.0.0.1:8080/ -H "Host: node-dev.example.com"
hello world

Load a lua script

$ curl 127.0.0.1:1995/lua/script.node-dev.example.com -d 'if ngx.req.get_method() == "DELETE" then return ngx.exit(403) end'
"ok"
$ curl -XPUT 127.0.0.1:1995/lua/script.node-dev.example.com

The script is taking effect.

$ curl -XDELETE 127.0.0.1:8080/ -H "Host: node-dev.example.com"
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>slardar/1.0</center>
</body>
</html>

Back to TOC

Run Test

This bundle contains only tests for Slardar, the bundled components are tested in their own project.

You can run tests for Slardar by the following commands.

docker run -d --net=host consul agent -dev -bind=127.0.0.1
make dev
make test

Back to TOC

Contribution

You're very welcome to report issues on GitHub.

PRs are more than welcome. Just fork, create a feature branch, and open a PR. We love PRs. :)

Back to TOC

Copyright & License

The bundle itself is licensed under the 2-clause BSD license.

Copyright (c) 2016, UPYUN(又拍云) Inc.

This module is licensed under the terms of the BSD license.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

More Repositories

1

lua-resty-checkups

Manage Nginx upstreams in pure Lua.
Lua
259
star
2

upx

UPYUN Storage Command Tool
Go
193
star
3

php-sdk

UPYUN SDK for PHPer
PHP
176
star
4

upyun-resty

UPYUN's open source software for OpenResty development
175
star
5

Android-short-video

Android-short-video 是又拍云推出的一款适用于 Android 平台的短视频 SDK,它包含短视频拍摄、编辑、合成、上传,还包含短视频播放器,再结合又拍云存储和 CDN,您就可以开启您的短视频业务了。
Java
172
star
6

python-sdk

UPYUN Python SDK
Python
115
star
7

java-sdk

UPYUN Java SDK
Java
114
star
8

ios-live-sdk

UPYUN 直播 SDK。播放器、采集器、推流器统一集成,功能完备接口简练,可以快速安装使用。采集与音视频处理模块开源,灵活性强可以满足复杂定制需求。UPYUN 直播平台一站式服务:http://docs.upyun.com/live/
Objective-C
104
star
9

node-sdk

UPYUN SDK for JS(support browser and node.js)
JavaScript
95
star
10

android-player-sdk

UPYUN Android 流媒体播放器
Java
87
star
11

go-sdk

UPYUN Go SDK
Go
76
star
12

android-push-sdk

UPYUN Android 推流 SDK
Java
74
star
13

cafex

Cafex is a pure Elixir implementation of Kafka client
Elixir
69
star
14

wechat-sdk

又拍云微信小程序 SDK
JavaScript
69
star
15

lua-resty-limit-rate

Lua module for limiting request rate for OpenResty/ngx_lua, using the "token bucket" method.
Perl
68
star
16

docs

UPYUN Docs
CSS
66
star
17

android-sdk

UPYUN Android SDK
Java
51
star
18

ios-short-video

ios-short-video 是又拍云推出的一款适用于 iOS 平台的短视频 SDK,它包含短视频拍摄、编辑、合成、上传,还包含短视频播放器,再结合又拍云存储和 CDN,您就可以开启您的短视频业务了。https://www.upyun.com/products/short-video
Objective-C
51
star
19

node-upyun

UPYUN Node.js SDK !!!Deprecated
48
star
20

lua-resty-sync

Synchronizing data based on version changes
Lua
41
star
21

ios-form-sdk

UPYUN iOS SDK
Objective-C
37
star
22

ios-sdk

UPYUN iOS SDK
Objective-C
32
star
23

ruby-sdk

UPYUN Ruby SDK
Ruby
29
star
24

js-multipart-upload

HTML5 multipart-uploader demo
JavaScript
28
star
25

upyun.com

The next generation website
Vue
27
star
26

token-examples

Multilingual token generation example
JavaScript
19
star
27

upyun.github.io

UPYUN Engineering Blog
CSS
18
star
28

ueditor

Integrate UPYUN API to UEditor.
JavaScript
17
star
29

multipart-upload-android-sdk

multipart-upload-android-sdk
Java
15
star
30

discuz-plugin

UPYUN for discuz
PHP
13
star
31

node-av-pretreatment

音视频处理 SDK(Node.js)
JavaScript
11
star
32

c-sdk

UPYUN C SDK
C
10
star
33

js-form-api

表单 API,前端使用的简单 Demo
9
star
34

c-sharp-sdk

UPYUN C-Sharp SDK
C#
8
star
35

upyun-uss-script

Commonly used script for UPYUN Storage Service
Python
8
star
36

multipart-upload-php-sdk

又拍云分块上传 SDK !!! Deprecated
PHP
7
star
37

multipart-upload-ios-sdk

multipart-upload-ios-sdk
Objective-C
7
star
38

erlang-sdk

UPYUN Erlang SDK
Erlang
6
star
39

swift-sdk

又拍云 swift SDK
Swift
5
star
40

Android-QuickLogin

Java
4
star
41

ios-video-player

Objective-C
4
star
42

iOS-QuickLogin

Objective-C
4
star
43

umeditor-for-UPYUN

Integrated UPYUN API to umeditor
JavaScript
3
star
44

av-pretreatment-php-sdk

又拍云音视频预处理 PHP SDK !!! Deprecated
PHP
3
star
45

java-purge-sdk

JAVA version cache refresh for UPYUN
Java
2
star
46

ios-fusion-sdk

UPYUN 融合云 iOS SDK
Objective-C
2
star
47

utilgo

Some awesome go utils
Go
2
star
48

android-fusion-sdk

UPYUN 融合云 Android SDK
Java
1
star
49

carrot

UPYUN scoop packages
PowerShell
1
star
50

rust-sdk

1
star
51

gzjion

multi gz files merge to one
C
1
star