资讯专栏INFORMATION COLUMN

kong 配置负载均衡

idealcn / 1027人阅读

摘要:负载均衡根据转发给相应的根据的转发给负载均衡至,这就是的负载均衡执行流程,下面通过分别配置。创建名为的为添加后端服务器等同于创建了如下配置创建名为的服务,并通过绑定相应的后端服务。

负载均衡

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/34943.html

相关文章

  • 分布式系统的负载均衡 | 架构干货

    摘要:是的默认负载均衡策略。一致性哈希负载均衡。所以负载均衡是分布式系统架构设计中必须考虑的因素之一。考虑主要是如何让下游接收到的请求是均匀分布的第层客户端层反向代理层的负载均衡。通过轮询第层反向代理层层的负载均衡。 一、 什么是负载均衡? 什么是负载均衡? 记得第一次接触 Nginx 是在实验室,那时候在服务器部署网站需要用 Nginx 。Nginx 是一个服务组件,用来反向代理、负载平衡...

    twohappy 评论0 收藏0
  • Dubbo Cloud Native 之路的实践与思考

    摘要:可简单地认为它是的扩展,负载均衡自然成为不可或缺的特性。是基于开发的服务代理组件,在使用场景中,它与和整合,打造具备服务动态更新和负载均衡能力的服务网关。类似的特性在项目也有体现,它是另一种高性能代理的方案,提供服务发现健康和负载均衡。 摘要: Cloud Native 应用架构随着云技术的发展受到业界特别重视和关注,尤其是 CNCF(Cloud Native Computing Fo...

    niceforbear 评论0 收藏0
  • 大型网站演变中的负载均衡场景

    摘要:集群方案中利用哈希槽的方式,达到了缓存数据量的拆分,以及负载均衡。消息中间件是天生的集群化,保证消息中间件的高可用以及负载均衡,有这个是消息队列层的负载均衡场景。享学课堂特邀作者:老顾前言我们小伙伴们是不是经常看到网上一些集群、高可用、高并发、负载均衡等关键词,有很多种方案、以及应用场景中都有相关的介绍。今天老顾就带着大家一起看一下,一整套大型网站会有哪些负载均衡方案场景。创业阶段创业初期很...

    tianhang 评论0 收藏0
  • API网关Kong-简介

    摘要:为万开发者提供每月数十亿的请求支持。在请求和响应之间,将执行任何安装的插件,扩展的功能集。其有效的成为每个的请求入口。主要组件介绍基于服务器,用来接受请求的。总结就是一个针对管理系统,并提供了很多关于网关功能的扩展插件介绍插件使用脚本编写。 1、简介 Kong 是一个企业级服务网关,底层是使用lua语言,整合Nginx 实现了强大的服务转发,路由,验证功能, 1.2 官方描述 Kong...

    张宪坤 评论0 收藏0
  • Dubbo Cloud Native 实践与思考

    摘要:可简单地认为它是的扩展,负载均衡自然成为不可或缺的特性。类似的特性在项目也有体现,它是另一种高性能代理的方案,提供服务发现健康和负载均衡。 Dubbo Cloud Native 实践与思考 分享简介 Cloud Native 应用架构随着云技术的发展受到业界特别重视和关注,尤其是 CNCF(Cloud Native Computing Foundation)项目蓬勃发展之际。Dubbo...

    邱勇 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<