Kong API Gateway Load Balance

Submitted by Lizhe on Fri, 06/01/2018 - 12:15

第一步你需要创建一个upstream

这里命名为 helloworld_upstream

[admin@localhost ~]$ curl -i -X POST --url http://localhost:8001/upstreams/ -d "name=helloworld_upstream"
HTTP/1.1 201 Created
Date: Fri, 01 Jun 2018 04:17:49 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{"created_at":1527826669625,"hash_on":"none","id":"406af012-2da3-44b3-932e-f9950bcf24a4","healthchecks":{"active":{"unhealthy":{"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0,"timeouts":0,"http_failures":0,"interval":0},"http_path":"\/","healthy":{"http_statuses":[200,302],"interval":0,"successes":0},"timeout":1,"concurrency":10},"passive":{"unhealthy":{"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0,"timeouts":0},"healthy":{"successes":0,"http_statuses":[200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,306,307,308]}}},"name":"helloworld_upstream","hash_fallback":"none","slots":10000}
[admin@localhost ~]$ 
 

 

810

811

然后给这个helloworld_upstream添加target

这里我添加3个

分别指向

www.baidu.com

www.google.com

www.163.com

这里需要注意的是, target是需要指定端口的, 不指定的话默认会变成8000端口

[admin@localhost ~]$ curl -i -X POST --url http://localhost:8001/upstreams/helloworld_upstream/targets -d "target=www.163.com:80" -d "weight=100"
HTTP/1.1 201 Created
Date: Fri, 01 Jun 2018 05:52:14 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{"created_at":1527832334473,"weight":100,"upstream_id":"406af012-2da3-44b3-932e-f9950bcf24a4","target":"www.163.com:80","id":"55fea23f-5753-4dce-8817-8f0b66e4091b"}
[admin@localhost ~]$

817

 

关联upstream组至API

curl -i -X POST --url http://localhost:8001/apis/ -d "name=helloworld_lb" -d "hosts=lizhe.helloworld.com" -d "upstream_url=http://helloworld_upstream"

这里指定的hosts名为lizhe.helloworld.com

所以需要指定一下hosts文件

[admin@localhost ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   lizhe.helloworld.com
 

然后直接访问 lizhe.helloworld.com:8000就可以随机负载均衡给上面的3个地址了

818

 

819