项目作者: projecteru2

项目描述 :
Eru load balancer
高级语言: Lua
项目地址: git://github.com/projecteru2/elb.git
创建时间: 2017-09-25T09:21:03Z
项目社区:https://github.com/projecteru2/elb

开源协议:MIT License

下载


Eru load balance

Build Status
Pypi Status

ELB (Eru load balance) is based on openresty. In Eru architecture, we use multiple filters to determine which upstream to forward. And by using ngx_http_dyups_module, citadel can update upstream dynamically.

Features

  • update upstream dynamically
  • run by eru
  • custom strategies of network flow

Storage

ELB will load data from etcd when starting.

Rule

In this version, we provide two types of filter. One is for user-agent, another for path. So we design a simple protocol for describing it.

This data will store in etcd with key /$ELBNAME/rules/$domain.

For example:

  1. {
  2. "init": "r1",
  3. "rules": {
  4. "r1": {"type": "ua", "args": {"fail": "r3", "pattern": "httpie(\\S+)$", "succ": "r4"}},
  5. "r2": {"type": "backend", "args": {"servername": "upstream1"}},
  6. "r3": {"type": "backend", "args": {"servername": "upstream2"}},
  7. "r4": {"type": "path", "args": {"regex": true, "pattern": "^\\/blog\\/(\\S+)$", "succ": "r2", "fail": "r3", "rewrite": false}}
  8. },
  9. }

You can build a complex filter by multiple rules.

Upstream

Upstream data also store in etcd with key /$ELBNAME/upstreams/$backend/$server.

For example

  1. etcdctl set /ELB/upstreams/up1/127.0.0.1:8088 ""
  2. etcdctl set /ELB/upstreams/up1/127.0.0.1:8089 ""
  3. etcdctl set /ELB/upstreams/up2/127.0.0.1:8089 "max_fails=2 weight=10"

Use dir to store server data will help you to asynchronous active server in upstream.

API

ELB have two APIs for managing.

Domain API /__erulb__/domain

When GET this url, it will response a json which contains domain and it’s rules.

For example:

  1. GET /__erulb__/domain HTTP/1.1
  2. Accept: */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Host: 127.0.0.1:8080
  6. User-Agent: HTTPie/0.9.9
  7. HTTP/1.1 200 OK
  8. Connection: keep-alive
  9. Content-Type: application/json
  10. Date: Mon, 25 Sep 2017 09:06:32 GMT
  11. Server: openresty/1.11.2.5
  12. Transfer-Encoding: chunked
  13. {
  14. "127.0.0.1": {
  15. "init": "r1",
  16. "rules": {
  17. "r1": {
  18. "args": {
  19. "fail": "r3",
  20. "pattern": "httpie(\\S+)$",
  21. "succ": "r4"
  22. },
  23. "type": "ua"
  24. },
  25. "r2": {
  26. "args": {
  27. "servername": "up1"
  28. },
  29. "type": "backend"
  30. },
  31. "r3": {
  32. "args": {
  33. "servername": "up2"
  34. },
  35. "type": "backend"
  36. },
  37. "r4": {
  38. "args": {
  39. "fail": "r3",
  40. "pattern": "^\\/blog\\/(\\S+)$",
  41. "regex": true,
  42. "rewrite": false,
  43. "succ": "r2"
  44. },
  45. "type": "path"
  46. }
  47. }
  48. }
  49. }

If you PUT this url, you have to upload a json with domains and it’s rule, then ELB will update itself with their rules. For example:

  1. PUT /__erulb__/domain HTTP/1.1
  2. Accept: application/json
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Content-Length: 305
  6. Content-Type: application/json
  7. Host: localhost:8080
  8. User-Agent: HTTPie/0.9.4
  9. {
  10. "localhost": {
  11. "init": "r1",
  12. "rules": {
  13. "r1": {
  14. "args": {
  15. "fail": "r3",
  16. "pattern": "^\\/blog\\/(\\S+)$",
  17. "regex": true,
  18. "rewrite": false,
  19. "succ": "r2"
  20. },
  21. "type": "path"
  22. },
  23. "r2": {
  24. "args": {
  25. "servername": "upstream1"
  26. },
  27. "type": "backend"
  28. },
  29. "r3": {
  30. "args": {
  31. "servername": "upstream2"
  32. },
  33. "type": "backend"
  34. }
  35. }
  36. }
  37. }
  38. HTTP/1.1 200 OK
  39. Connection: keep-alive
  40. Content-Type: application/json
  41. Date: Tue, 26 Sep 2017 15:03:03 GMT
  42. Server: openresty/1.11.2.5
  43. Transfer-Encoding: chunked
  44. {
  45. "msg": "OK"
  46. }

Then domain localhost was added. However, if somebody restart ELB, it will lose. Don’t forget to store rules in etcd.

If you use DELETE method, you can upload a json with domains, then ELB will delete those domains.

  1. DELETE /__erulb__/domain HTTP/1.1
  2. Accept: */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Content-Length: 0
  6. Host: localhost:8080
  7. User-Agent: HTTPie/0.9.9
  8. [
  9. "localhost",
  10. "127.0.0.1"
  11. ]
  12. HTTP/1.1 200 OK
  13. Connection: keep-alive
  14. Content-Type: application/json
  15. Date: Wed, 27 Sep 2017 06:36:30 GMT
  16. Server: openresty/1.11.2.5
  17. Transfer-Encoding: chunked
  18. {
  19. "msg": "OK"
  20. }

Don’t forget delete domains data in etcd.

Upstream API /__erulb__/upstream

If you GET this url, elb will response a json which contains upstreams and it’s backends like this:

  1. GET /__erulb__/upstream HTTP/1.1
  2. Accept: */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Host: 127.0.0.1:8080
  6. User-Agent: HTTPie/0.9.9
  7. HTTP/1.1 200 OK
  8. Connection: keep-alive
  9. Content-Type: application/json
  10. Date: Mon, 25 Sep 2017 09:08:59 GMT
  11. Server: openresty/1.11.2.5
  12. Transfer-Encoding: chunked
  13. {
  14. "upstream1": [
  15. {
  16. "addr": "127.0.0.1:8089",
  17. "fail_timeout": 10,
  18. "max_fails": 1,
  19. "name": "127.0.0.1:8089",
  20. "weight": 1
  21. },
  22. {
  23. "addr": "127.0.0.1:8088",
  24. "fail_timeout": 10,
  25. "max_fails": 1,
  26. "name": "127.0.0.1:8088",
  27. "weight": 1
  28. }
  29. ],
  30. "upstream2": [
  31. {
  32. "addr": "127.0.0.1:8089",
  33. "fail_timeout": 10,
  34. "max_fails": 2,
  35. "name": "127.0.0.1:8089",
  36. "weight": 10
  37. }
  38. ]
  39. }

If you use PUT method, you can upload a json with upstreams and it’s backends, then ELB will update itself with their upstreams. For example:

  1. PUT /__erulb__/upstream HTTP/1.1
  2. Accept: application/json, */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Content-Length: 115
  6. Content-Type: application/json
  7. Host: localhost:8080
  8. User-Agent: HTTPie/0.9.9
  9. {
  10. "up1": {
  11. "127.0.0.1:8088": "",
  12. "127.0.0.1:8089": ""
  13. },
  14. "up2": {
  15. "localhost:8088": "",
  16. "localhost:8089": ""
  17. }
  18. }
  19. HTTP/1.1 200 OK
  20. Connection: keep-alive
  21. Content-Type: application/json
  22. Date: Mon, 25 Sep 2017 09:10:58 GMT
  23. Server: openresty/1.11.2.5
  24. Transfer-Encoding: chunked
  25. {
  26. "msg": "OK"
  27. }

If you use DELETE method, you can upload a json with upstreams’ name, then ELB will delete those upstreams.

  1. DELETE /__erulb__/upstream HTTP/1.1
  2. Accept: application/json, */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Content-Length: 8
  6. Content-Type: application/json
  7. Host: localhost:8080
  8. User-Agent: HTTPie/0.9.9
  9. [
  10. "upstream1",
  11. "upstream2"
  12. ]
  13. HTTP/1.1 200 OK
  14. Connection: keep-alive
  15. Content-Type: application/json
  16. Date: Wed, 27 Sep 2017 06:34:51 GMT
  17. Server: openresty/1.11.2.5
  18. Transfer-Encoding: chunked
  19. {
  20. "upstream1": true,
  21. "upstream2": true
  22. }

Don’t forget delete upstreams data in etcd.

Dump API /__erulb__/dump

PUT this url, in-memory data will be dumped into etcd. and next elb will load it automatically from etcd.

  1. PUT /__erulb__/dump HTTP/1.1
  2. Accept: application/json, */*
  3. Accept-Encoding: gzip, deflate
  4. Connection: keep-alive
  5. Content-Length: 115
  6. Content-Type: application/json
  7. Host: localhost:8080
  8. User-Agent: HTTPie/0.9.9
  9. HTTP/1.1 200 OK
  10. Connection: keep-alive
  11. Content-Type: application/json
  12. Date: Mon, 25 Sep 2017 09:10:58 GMT
  13. Server: openresty/1.11.2.5
  14. Transfer-Encoding: chunked
  15. {
  16. "msg": "OK"
  17. }

Env

ELB will read ETCD, ELBNAME and STATSD from environment.

If etcd and elbname not set, elb will use 127.0.0.1:2379 and ELB as default.

But if STATSD not set, elb will not calcuate domain status.

Dockerized ELB manually

We suggest you to run elb by ERU, however this image can standalone running.

  1. docker run -d --privileged \
  2. --name eru_elb_$HOSTNAME \
  3. --net host \
  4. --restart always \
  5. -e "ETCD=<IP:PORT>" \
  6. -e "ELBNAME=<ELBNAME>" \
  7. -e "STATSD=<IP:PORT>" \
  8. projecteru2/elb

Build and Deploy by Eru itself

After we implemented bootstrap in eru2, now you can build and deploy elb with cli tool.

  1. Test source code and build image
  1. <cli_execute_path> --name <image_name> https://goo.gl/WTGT9E

Make sure you can clone code by ssh protocol because libgit2 ask for it. So you need configure core with github certs. After the fresh image was named and tagged, it will be auto pushed to the remote registry which was defined in core.

  1. Deploy elb by eru with specific resource.
  1. <cli_execute_path> --pod <pod_name> --entry elb --network <network_name> --image <projecteru2/elb>|<your_own_image> [--node <specify_node>] [--cpu 0.3 | --mem 1024000000] https://goo.gl/WTGT9E

Now you will find elb was started.

Warning

Because overlayfs with CentOS 7 has some issue, do not compile Dockerfile on CentOS 7 with overlayfs if you use early docker befor this.