美文网首页
Kong网关API的使用

Kong网关API的使用

作者: 君致 | 来源:发表于2019-06-13 11:57 被阅读0次

简介

Kong在v0.13.x版本之前通过接口来管理用户接入的API,但是在v0.13.x版本之后,官方不建议使用API来管理用户接口,而是用Service和Route模块来替代,管理的更精细.

Service操作

接口说明

请求接口: /services
参数说明:

  • name: 服务名称
  • protocol: 通讯协议,可以是http(默认)或https
  • host: upstream服务器主机(upstream的name属性值)
  • port: upstream服务器端口,默认为80
  • retries: 代理失败情况下执行重试的次数,默认为5次
  • path: 向upstream服务器请求中使用的路径,默认为空
  • connect_timeout: 建立到upstream服务器连接超时时间,默认为60000
  • write_timeout: 请求发送到upstream服务器两个连续读写操作之间的超时时间,默认为60000
  • read_timeout: 请求发送到upstream服务器的两个连续读取操作之间的超时时间,默认为60000
添加service

请求方法:POST
请求实例:

# curl命令请求
curl -i -X POST --url http://KongUrl/services
    --data "name=test_1" 
    --data "protocol=http" 
    --data "host=test_1"
    --data "port=80"
    --data "path=/test_1"
# 返回参数结果
{
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1', 
    'updated_at': 1560396796, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
获取所有service数据

请求方法: GET
请求实例:

# curl命令请求
curl http://KongUrl/services
# 返回参数结果
{
    'next': None, 
    'data': [
        {
            'host': 'test_2', 
            'created_at': 1560312006, 
            'connect_timeout': 60000, 
            'id': '51146ad9-6a73-4409-9395-43539cfe74f3', 
            'protocol': 'http', 
            'name': 'test_2', 
            'read_timeout': 60000, 
            'port': 80, 
            'path': None, 
            'updated_at': 1560324556, 
            'retries': 5, 
            'write_timeout': 60000, 
            'tags': None
        }, 
        {
            'host': 'test_1', 
            'created_at': 1560396796, 
            'connect_timeout': 60000, 
            'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
            'protocol': 'http', 
            'name': 'test_1', 
            'read_timeout': 60000, 
            'port': 80, 
            'path': '/test_1', 
            'updated_at': 1560396817, 
            'retries': 5, 
            'write_timeout': 60000, 
            'tags': None
        }
    ]
}
获取单个service数据

请求方法: GET
请求实例:

# curl命令请求(根据service名字或ID)
curl http://KongUrl/services/test_1
# 返回参数结果
{
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf99', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1', 
    'updated_at': 1560396796, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
更新service

请求方法: PATCH
请求实例:

# 例如通过service名字(ID)更新path
curl -i -X PATCH --url http://KongUrl/services/test_1 --data "path=test_1_path"
# 返回参数结果
{   
    'host': 'test_1', 
    'created_at': 1560396796, 
    'connect_timeout': 60000, 
    'id': 'be48404c-f396-465d-9010-a33a59a0tf9', 
    'protocol': 'http', 
    'name': 'test_1', 
    'read_timeout': 60000, 
    'port': 80, 
    'path': '/test_1_patch', 
    'updated_at': 1560405161, 
    'retries': 5, 
    'write_timeout': 60000, 
    'tags': None
}
注: 使用PUT方法,进行创建或更细service,不存在对应的service即创建,存在即更新
删除service数据

请求方法: DELETE
请求实例:

# 根据service名字或ID删除
curl -i -X DELETE --url http://KongUrl/services/test_1

Route操作

接口说明:
  • name: 路由名称
  • protocols: 路由允许的协议列表
  • methods: 路由相匹配的HTTP方法
  • hosts: 路由匹配的域名列表
  • paths: 路由匹配的路径列表
  • strip_path: 当通过一条路径匹配一条路径时,从upstream请求URL中剥离匹配的前缀,默认为true
  • preserve_host: 当通过一个主机域名匹配一条路由时,在upstream请求头中使用请求主机头.默认设置为false,upstream主机头将是服务主机的主机头
  • service: 路由代理通信的地方。用表单编码参数是service.id=<service_id>.如果是JSON,则使用"service":{"id":"<service_id>"}
添加某个Service的路由

请求方法: POST
请求实例:

# curl命令请求--创建test_1服务的路由
curl -i -X POST --url http://KongUrl/service/test_1/routes/
--data "protocols[]=http&protocols[]=https"
--data "hosts[]=test.cn"
--data "methods[]=GET&methods[]=POST"
# 返回参数结果
{   
    'updated_at': 1560409444, 
    'created_at': 1560409444, 
    'strip_path': True, 
    'snis': None, 
    'hosts': ['test.cn'], 
    'name': 'test_1', 
    'methods': ['GET', 'POST'], 
    'sources': None, 
    'preserve_host': False, 
    'regex_priority': 0, 
    'service': {'id': 'be48404c-f396-465d-9010-a33a59a00tf9'}, 
    'paths': ['/test_1'], 
    'destinations': None, 
    'id': '29310394-6342-48b8-a95e-533bdc5b26d9', 
    'protocols': ['http', 'https'], 
    'tags': None
}
Route其它操作
  • 更新:PATCH ,请求方法类似service的PATCH
# 更新route名字为test_1的paths
curl -i -X PATCH --url http://KongUrl/services/test_1 --data "paths[]=test_1_path"
  • 删除: DELETE,请求方法类似service的DELETE
# 根据routes的名字或ID删除routes
curl -i -X DELETE --url http://KongUrl/routes/test_1
  • 查询: GET, 请求方法类似service的GET
# 获取所有routes
curl http://KongUrl/routes
# 获取某个service的routes,通过service名字或ID获取
curl http://KongUrl/{service name or id}/routes
# 检索某个routes,通过route的名字或ID获取
curl http://KongUrl/routes/{name or id}

操作Consumer

  • 请求接口:/consumers
    消费对象表示服务的消费者或用户。可以依赖Kong作为主要数据存储,也可以将用户自己管理的列表映射到该数据库consumer表,以保持Kong与现有主数据存储的一致性。用作权限控制。

操作Plugins

  • 请求接口:/plugins
    插件表示将在HTTP请求/响应生命周期期间执行的插件配置。可以将功能添加到Kong后面运行的服务,例如身份验证或速率限制。可以通过访问插件库找到更多关于安装的信息和每个插件所需的值。当向服务添加插件配置时,客户端对该服务所做的每一个请求都将运行添加的插件

  • 一个插件对于一个请求只运行一次,但是它将运行的配置取决于它所配置的插件实体。可以为各种实体,实体组合,甚至全局配置插件。例如,当希望以某种方式为大多数请求配置插件,但与已认证的请求行为略有不同时,这很有用。因此,当插件应用于具有不同配置的不同实体时,存在运行插件的优先顺序。经验法则是:一个插件对于配置的实体更具体更详细,其优先级越高。

  • 当多次配置插件时,优先级的完整顺序是:
    1、在以下组合上配置插件:a Route, a Service, and a Consumer(消费者意味着请求必须被认证)
    2、在Route和Consumer组合上配置的插件(消费者意味着请求必须被认证)
    3、插件配置在Service和Consumer的组合上(消费者意味着请求必须被认证)
    4、插件配置在Route和Service的组合上
    5、在Consumer上配置的插件(消费者意味着请求必须被认证)
    6、在Route上配置的插件
    7、在Service上配置的插件
    8、插件配置为全局运行

操作Certificate

  • 请求接口: /certificate
  • 主要用于Kong的证书操作

操作Sni

  • 请求接口: /snis
  • SNI对象(Server Name Indication)表示主机名与证书的多对一映射。也就是说,证书对象可以有许多与之关联的主机名;当Kong收到SSL请求时,它使用Client Hello中的SNI字段根据与证书关联的SNI查找证书对象。

操作Upstream

  • 请求接口: /upstreams
  • 请求方法: POST
  • 请求实例
# curl方法请求
curl -i -X POST http://KongUrl/upstreams --data="name=test_1"
# 返回参数结果
{
    'created_at': 1560477909,
    'hash_on': 'none', # 加权循环方案,默认为none
    'id': 'a28a67a0-f13a-42e9-b003-6337c79f96ba',
    'tags': None,
    'name': 'test_1',
    'hash_fallback_header': None,
    'hash_on_cookie': None,
    'healthchecks':
        {
            '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': None,
                    '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_fallback': 'none',
    'hash_on_header': None,
    'slots': 10000
}

  • 查看所有的负载均衡数据-GET
curl http://KongUrl/upstreams
  • 根据负载的名字或ID获取负载均衡数据
curl http://KongUrl/upstreams/{name or id}
  • 更新负载均衡数据-PATCH
  • 负载均衡健康检查(根据名字或ID)-GET
curl http://KongUrl/{name or id}/health

操作Target

  • 请求方法: POST
  • 添加负载均衡的节点
  • 接口: /upstreams/{upstream host:port or id}/targets
  • 说明: id为upstream的ID,host:posrt为关联主机的host:port
# curl请求方法
curl -i -X POST http://KongUrl/upstreams/a28a67a0-f13a-42e9-b003-6337c79f96ba/
 --data="target=192.168.99.100:8080"
 -- data="weight=100"
 --data="tags=test"
# 返回参数结果
{
    'created_at': 1560483493.414,
    'upstream': {'id': 'a28a67a0-f13a-42e9-b003-6337c79f96ba'},
    'id': 'e58948fe-0040-4a5c-951c-fdc08398396b',
    'tags': ['test'],
    'target': '192.168.99.100:8080',
    'weight': 100
}

推荐操作流程:

  • 1.创建services
    1. 创建routes
    1. 创建upstreams
    1. 创建targets

相关文章

网友评论

      本文标题:Kong网关API的使用

      本文链接:https://www.haomeiwen.com/subject/pzbjfctx.html