The upstream object represents a virtual hostname and can be used to loadbalance incoming requests over multiple services (targets). So for example an upstream named service.v1.xyz for a Service object whose host is service.v1.xyz. Requests for this Service would be proxied to the targets defined within the upstream.
Upstream(上游)模块可以理解为服务请求处理器,他可以模拟成一个虚拟域名,然后将进入的请求进行负载均衡处理,最后将命令分发到不同的微服务(Target)。所以配置的时候,先创建upstream对象,然后再在upstream对象里增加具体的target模块以对应不同的微服务地址和端口,最后在已创建的服务里配置对应的upstream名字,这样当请求到达时,kong就知道将指令分发到哪个具体的微服务里。
通过KONGA配置UPSTREAM
1,打开KONGA界面,在左侧见面选择UPSTREAMS菜单,然后点击“CREATE UPSTREAM”
2,在弹出菜单里一般只需要填写NAME,此名字后面可用在SERVICES里的Host字段
注意:weighted-round-robin (加权轮询)算法使用
1,hash_on: 用什么字段作为输入的hash值,默认none就是没有,不增加额外权重
2,hash_fallback: 当hash_on没有设置的时候用什么字段作为hash值的输入
3,hash_on_header: 在header里用什么字段作为hash值的输入
4,hash_fallback_header: 如果hash_on_header没有设置,在header里用什么字段作为hash值的输入 ……
3,创建完UPSTREAM后,在UPSTREAM的列表里点“DETAILS”即可看见TARGET选项
4,点击Targets后进入Target编辑页面,并且可以通过右上角进入增加target页面
5,填写IP:PORT地址在Target位置,Weight是可以标识不同的服务的权重,可以进行调节
6,最后在SEVICE编辑里将host换成对应的UPSTREAM的名字
通过KONG ADMIN API配置UPSTREAM
https://docs.konghq.com/gateway-oss/0.14.x/admin-api/#upstream-objects
1,查看UPSTREAMS
curl -i http://10.10.30.70:8001/upstreams
{"next":null,"data":[{"client_certificate":null,"created_at":1629787443,"id":"17881723-daa5-439a-8456-dab11d821dc5","tags":null,"name":"testupstream","algorithm":"round-robin","hash_on_header":null,"hash_fallback_header":null,"host_header":null,"hash_on_cookie":null,"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_cookie_path":"/","hash_on":"none","hash_fallback":"none","slots":1000}]}
2,添加UPSTREAM
curl -X POST http://10.10.30.70:8001/upstreams --data "name=testapiupstream"
{"client_certificate":null,"created_at":1629876094,"id":"f2533583-a22d-4ce0-8c57-9a3e3c08ba49","tags":null,"name ":"testapiupstream","algorithm":"round-robin","hash_on_header":null,"hash_fallback_header":null,"host_header":nu ll,"hash_on_cookie":null,"healthchecks":{"threshold":0,"active":{"unhealthy":{"http_statuses":[429,404,500,501,5 02,503,504,505],"tcp_failures":0,"timeouts":0,"http_failures":0,"interval":0},"type":"http","http_path":"/","tim eout":1,"healthy":{"successes":0,"interval":0,"http_statuses":[200,302]},"https_sni":null,"https_verify_certific ate":true,"concurrency":10},"passive":{"unhealthy":{"http_failures":0,"http_statuses":[429,500,503],"tcp_failure s":0,"timeouts":0},"healthy":{"http_statuses"[200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,3 06,307,308],"successes":0},"type":"http"}},"hash_on_cookie_path":"/","hash_on":"none","hash_fallback":"none","sl ots":10000}
3,为UPSTREAM配置TARGET
curl -X POST http://10.10.30.70:8001/upstreams/testapiupstream/targets --data "target=10.10.30.62:8686" --data "weight=100"
curl -X POST http://10.10.30.70:8001/upstreams/testapiupstream/targets --data "target=10.10.30.70:8686" --data "weight=100"
4,SERVICE配置UPSTREAM
curl -X PATCH http://10.10.30.70:8001/services/e599e00a-d288-412b-adc9-fa14ac8661eb --data "host=testapiupstream"
{"host":"testapiupstream","id":"e599e00a-d288-412b-adc9-fa14ac8661eb","protocol":"http","read_timeout":60000,"tl s_verify_depth":null,"port":80,"updated_at":1629876777,"ca_certificates":null,"created_at":1629794428,"connect_t imeout":60000,"write_timeout":60000,"name":"hechongservice","retries":5,"path":"/","tls_verify":null,"tags":null ,"client_certificate":null}