Kong的基本使用

xiaohai 2021-06-07 09:36:53 1207人围观 标签: Kong 
简介Kong是一个在Nginx中运行的Lua应用程序,可以通过lua-nginx模块实现,Kong不是用这个模块编译Nginx,而是与OpenRestry一起发布,OpenRestry已经包含了lua-nginx-module,OpenRestry是Nginx的一组扩展功能模块。

Nginx的负载均衡配置

upstream my_stream {
    server localhost:3000 weight=100;
}

server {
    listen 80;
    location /hello {
        proxy_pass http://my_steam;
    }
}

上面就是nginx最简单的nginx配置,可以转回http请求。

Kong配置

我们将上面nginx的配置转换成Kong的配置,操作如下:

1、配置upstream

[root@localhost ~]# curl -X POST http://localhost:8001/upstreams --data "name=my_stream"
{"created_at":1591599502,"hash_on":"none","id":"3524f785-9373-4020-be5f-bac876a08354","algorithm":"round-robin","name":"my_stream","tags":null,"hash_fallback_header":null,"hash_fallback":"none","hash_on_cookie":null,"host_header":null,"hash_on_cookie_path":"\/","healthchecks":{"threshold":0,"active":{"unhealthy":{"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0,"timeouts":0,"http_failures":0,"interval":0},"type":"http","http_path":"\/","timeout":1,"healthy":{"successes":0,"interval":0,"http_statuses":[200,302]},"https_sni":null,"https_verify_certificate":true,"concurrency":10},"passive":{"unhealthy":{"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0,"timeouts":0},"healthy":{"http_statuses":[200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,306,307,308],"successes":0},"type":"http"}},"hash_on_header":null,"slots":10000}

2、配置target

[root@localhost ~]# curl -X POST http://localhost:8001/upstreams/my_stream/targets --data "target=localhost:3000" --data "weight=100"
{"created_at":1591599638.753,"upstream":{"id":"3524f785-9373-4020-be5f-bac876a08354"},"id":"d3c6d09b-5779-480b-9573-a986f4c96083","target":"localhost:3000","weight":100}

3、配置service

[root@localhost ~]# curl -X POST http://localhost:8001/services --data "name=hello" --data "host=my_stream"
{"host":"my_stream","created_at":1591599716,"connect_timeout":60000,"id":"8a05e22b-8b88-4e86-b510-1943b4a5d8db","protocol":"http","name":"hello","read_timeout":60000,"port":80,"path":null,"updated_at":1591599716,"retries":5,"write_timeout":60000,"tags":null,"client_certificate":null}

这里会产生一个ID号:8a05e22b-8b88-4e86-b510-1943b4a5d8db

4、配置路由

[root@localhost ~]# curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id=8a05e22b-8b88-4e86-b510-1943b4a5d8db"
{"id":"341028c7-d2b4-4f0c-af67-3ee9b3be422c","path_handling":"v0","paths":["\/hello"],"destinations":null,"headers":null,"protocols":["http","https"],"methods":null,"snis":null,"service":{"id":"8a05e22b-8b88-4e86-b510-1943b4a5d8db"},"name":null,"strip_path":true,"preserve_host":false,"regex_priority":0,"updated_at":1591599797,"sources":null,"hosts":null,"https_redirect_status_code":426,"tags":null,"created_at":1591599797}

这里一切都是动态配置的,无需手动去配置。

为Kong新增路由信息需要操作upstream、target、service、route等,这边便是Kong最核心的四个对象。但是Kong不只是这些功能,如:

为hello服务添加50次/秒的限流:

curl -X POST http://localhost:8001/services/hello/plugins --data "name=rate-limiting" --data "config.second=50"

为hello服务jwt:

curl -X POST http://localhost:8001/services/hello/plugins --data "name=jwt"

插件也可以用在route上:

curl -X POST http://localhost:8001/services/{routeId}/plugins --data "name=rate-limiting" --data "config.second=50"

curl -X POST http://localhost:8001/services/{routeId}/plugins --data "name=jwt"