kong 配置負載均衡

負載均衡

route根據paths轉發給相應的service根據hostupstreamname)轉發給 upstream負載均衡至targets,這就是kong的負載均衡執行流程,下面通過restApi分別配置upstream,service,route

upstreams

創建名爲upstream.apiupstream

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章