kong 配置负载均衡

56次阅读

共计 2474 个字符,预计需要花费 7 分钟才能阅读完成。

负载均衡
route 根据 paths 转发给相应的 service 根据 host(upstream 的 name)转发给 upstream 负载均衡至 targets,这就是 kong 的负载均衡执行流程,下面通过 restApi 分别配置 upstream,service,route。
upstreams
创建名为 upstream.api 的 upstream
curl -X POST localhost:8001/upstreams
-d “name=upstream.api”
// reponse
{
“created_at”: 1553661443,
“hash_on”: “none”,
“id”: “04c9c36c-eea8-4d58-8668-3bfa117c34fd”,
“name”: “upstream.api”,

}
为 upstream.api 添加后端服务器
curl -X POST localhost:8001/upstreams/upstream.api/targets \
-d “target=192.168.20.6:8888” \
-d “weight=100”
// reponse
{
“created_at”: 1553663185.86,
“upstream”: {
“id”: “04c9c36c-eea8-4d58-8668-3bfa117c34fd”
},
“id”: “3386af25-8643-4c9c-aff5-bd30451ae24b”,
“target”: “192.168.20.6:8888”,
“weight”: 100
}

curl -X POST localhost:8001/upstreams/upstream.api/targets \
-d “target=192.168.20.6:9999” \
-d “weight=100”
// reponse
{
“created_at”: 1553663185.86,
“upstream”: {
“id”: “04c9c36c-eea8-4d58-8668-3bfa117c34fd”
},
“id”: “3386af25-8643-4c9c-aff5-bd30451ae24b”,
“target”: “192.168.20.6:9999”,
“weight”: 100
}
等同于创建了如下配置:
upstream upstream.api {
server 192.168.20.6:8888 weight=100;
server 192.168.20.6:9999 weight=100;
}
services
创建名为 service.api 的服务,并通过 host 绑定相应的后端服务 upstream.api。
curl -X POST localhost:8001/services/service.api
-d “name=service.api”
-d “host=upstream.api”
//
{
“host”: “upstream.api”,// 绑定的 upstream
“created_at”: 1553663485,
“connect_timeout”: 60000,
“id”: “5b93eda7-7ba5-4acc-a536-cf12f58a1144”,//service.id
“protocol”: “http”,
“name”: “service.api”,
“read_timeout”: 60000,
“port”: 80,
“path”: “/api/v1”,
“updated_at”: 1553663485,
“retries”: 5,
“write_timeout”: 60000
}
等同于
http {
server {
listen 8000;
location waiting-for-define {
proxy_pass http://upstream.api;
}
}
}
routes
为服务 service.api 绑定路由。需要理解,route 并非一条 url,它是 kong 的路由服务,可以为某个 kong 服务管理管理一套路由集合,route 就相当于 http > server 中的 location 规则集合。
#为 service.api 添加路由集合
curl -X POST localhost:8001/routes \
-d “name=route.api” \
-d “paths[]=/api/v1” \
-d “paths[]=/api/v2” \
-d “paths[]=/api/v3” \
-d “hosts[]=api.service.com” \
-d “hosts[]=service.com” \
-d “service.id=5b93eda7-7ba5-4acc-a536-cf12f58a1144”

# 或者通过 services 的接口

curl -X POST localhost:8001/services/service.api/routes \
-d “name=route.api” \
-d “paths[]=/api/v1” \
-d “paths[]=/api/v2” \
-d “paths[]=/api/v3” \
-d “hosts[]=localhost” \
-d “hosts[]=api.service.com” \
-d “hosts[]=service.com” \
我们还同时指定了 hosts,相当于 server_name,顺手把虚拟主机也做了。大致等同于如下配置:
http {
server {
listen 8000;
server_name localhost api.service.com service.com;
location /api/v1 {
proxy_pass http://upstream.api;
}
location /api/v2 {
proxy_pass http://upstream.api;
}
location /api/v3 {
proxy_pass http://upstream.api;
}
}
}
这样我们可以通过
localhost:8000/api/v1
api.service.com:8000/api/v2
service.com:8000/api/v3
来访问 service.api 服务,此服务后端有两台服务器做 LB。

正文完
 0