Kong

云原生 API 网关。(The Cloud-Native API Gateway)

Github stars Tracking Chart

Kong 是一个云原生的、快速的、可伸缩的分布式微服务抽象层(也称为 API 网关、API 中间件或某些情况下的服务网格)。作为一个自2015年开始开源的项目,它的核心价值是高性能和可扩展性。

由于积极维护,Kong 被广泛用于从初创公司到全球5000强以及政府机构的生产中。

安装 | 文档 | 论坛 | 博客 | IRC(freenode): #kong

为什么选择 Kong?

如果您正在为 web、移动或物联网(Internet of Things)构建应用程序,那么您很可能最终需要公共功能来运行实际的软件。Kong 可以通过充当微服务请求的网关(或sidecar)来提供帮助,同时通过插件提供负载平衡、日志记录、身份验证、速率限制、转换等功能。

特性

  • 云原生:与平台无关,Kong 可以在裸机或 Kubernetes 运行。
  • 动态负载平衡:跨多个上游服务的负载平衡流量。
  • 基于散列的负载平衡:具有一致的散列粘性会话的负载平衡。
  • 断路器:智能跟踪不健康的上游业务。
  • 健康检查:对上游服务的主动和被动监控。
  • 服务发现:在第三方DNS解析器(如 Consul)中解析SRV记录。
  • 无服务器:直接从 Kong 调用和保护 AWS Lambda 或 OpenWhisk 函数。
  • WebSockets :通过 WebSockets 与您的上游服务通信。
  • OAuth2.0 :轻松将 OAuth2.0 认证添加到您的 API 中。
  • 日志记录:通过 HTTP、TCP、UDP 或磁盘记录系统的请求和响应。
  • 安全性:ACL,Bot 检测,白名单/黑名单 IP 等。
  • Syslog :将日志记录到系统日志。
  • SSL :为底层服务或 API 设置特定的 SSL 证书。
  • 监控:实时监视提供关键的负载和性能服务器指标。
  • 身份验证:HMAC,JWT,Basic 等。
  • 限速:基于多个变量阻塞和节流请求。
  • 转换:添加、删除或操作 HTTP 请求和响应。
  • 缓存:在代理层缓存和服务响应。
  • CLI :从命令行控制 Kong 集群。
  • REST API :Kong 可以使用它的 RESTful API 来操作,以获得最大的灵活性。
  • 地理复制:配置总是跨不同区域的最新配置。
  • 故障检测与恢复:如果您的 Cassandra 节点之一发生故障,Kong 不受影响。
  • 集群:所有的 Kong 节点自动加入集群,保持它们的配置跨节点更新。
  • 可伸缩性:按性质分布,Kong 通过简单地添加节点进行水平扩展。
  • 性能:Kong 通过扩展和在核心使用 NGINX 轻松处理负载。
  • 插件:用于向 Kong 和 api 添加功能的可扩展体系结构。

有关插件的更多信息,您可以查看Kong Hub

基准

我们已经在 AWS 上测试了 Kong 和 Cassandra:您可以查看我们的基准报告

发布

Kong 有多种形式。虽然这个存储库包含其核心源代码,但其他repos也在积极开发中:

开发

如果您计划在 Kong 上进行开发,则需要进行开发安装。next 分支包含最新未发布的源代码。

(First edition: vz revised at 2019.08.11)

您可以在插件开发指南中阅读有关编写自己的插件的更多信息,或者在插件开发工具包(PDK)参考中浏览Kong的源代码文档的在线版本

Docker

您可以使用 Docker/Docker-compose 和一个挂载卷,按照关于 Kong/kong-build-tools 的说明来开发 Kong。

Vagrant

你可以使用运行 Kong 和 Postgres 的 Vagrant box,你可以在这里找到它 Mashape/kong-vagrant

源代码安装

Kong 主要是由 Lua 源文件构成的 OpenResty 应用程序,但也需要一些额外的第三方依赖项。 我们建议按照 https://docs.konghq.com/install/source/ 上的源安装说明安装它们。

而不是遵循第二步(安装 Kong),克隆此存储库并安装最新的 Lua 源代替当前发布的源代码:

$ git clone https://github.com/Kong/kong
$ cd kong/
#你可能想切换到开发分支。请参阅 CONTRIBUTING.md
$ git checkout next
#安装 Lua 资源
$ luarocks make

运行开发

查看默认配置文件的开发部分,了解要调整的属性,以便简化 Kong 的开发过程。

修改 lua_package_path lua_package_cpath 指令将允许Kong在系统中找到定制插件的源代码。

测试

安装开发依赖项(bustedluacheck):

$ make dev

Kong 依赖于使用busted 测试库的三个测试套件:

  • 单元测试
  • 集成测试需要 Postgres 和 Cassandra 启动并运行
  • 需要 Postgres 运行的插件测试

第一个可以简单地运行后安装 busted 并运行:

  $ make test

但是,集成和插件测试将生成一个 Kong 实例并对其执行测试。因此,根据您的需要,可以参考 spec/kong_tests.conf 配置文件,使您的测试实例指向 Postgres/Cassandra 服务器。

您可以使用以下工具运行集成测试(假设 Postgres 和 Cassandra 都在运行并根据 spec/kong_tests.conf 配置):

  $ make test-integration

插件测试:

  $ make test-plugins

最后,只需使用以下命令即可立即运行所有套件:

  $ make test-all

请参阅 run_tests.sh 脚本以获取测试套件和 Makefile 的更高级示例用法。

最后,Lua 开发中的一个非常有用的工具(与许多其他动态语言一样)是执行代码的静态 linting。 您可以使用 luacheck (与 make dev 一起安装):

  $ make lint

Makefile

开发时,可以使用 Makefile 来完成以下操作:

名字 描述
install 全局安装 Kong luarock
dev 安装开发依赖项
lint Lint Lua files in kong/ and spec/
test 运行单元测试套件
test-integration 运行集成测试套件
test-plugins 运行插件测试套件
test-all 立即运行所有单元+集成+插件测试

企业支援及演示

如果您在大型组织工作,您应该了解有关 Kong 企业版 的更多信息。

许可证

Copyright 2016-2019 Kong Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
   http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

(First edition: vz revised at 2019.08.11)

Overview

Name With OwnerKong/kong
Primary LanguageLua
Program languageShell (Language Count: 8)
PlatformAmazon Elastic Compute Cloud (EC2), Docker, Heroku, Kubernetes, Linux, Mac, Microsoft Azure, Vagrant
License:Apache License 2.0
Release Count163
Last Release Name3.6.1 (Posted on )
First Release Name0.1.0beta-3 (Posted on )
Created At2014-11-17 23:56:08
Pushed At2024-04-29 16:47:00
Last Commit At2024-04-29 15:15:50
Stargazers Count37.5k
Watchers Count1k
Fork Count4.7k
Commits Count10.4k
Has Issues Enabled
Issues Count4303
Issue Open Count34
Pull Requests Count5573
Pull Requests Open Count81
Pull Requests Close Count1918
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Build Status
License
Twitter

Kong is a cloud-native, fast, scalable, and distributed Microservice
Abstraction Layer (also known as an API Gateway or API Middleware).
Made available as an open-source project in 2015, its core values are
high performance and extensibility.

Actively maintained, Kong is widely used in production at companies ranging
from startups to Global 5000 as well as government organizations.

Installation, Documentation, Forum, Blog, IRC (freenode): #kong, Nightly Builds

Summary

Why Kong?

If you are building for the web, mobile, or IoT (Internet of Things) you will
likely end up needing common functionality to run your actual software. Kong
can help by acting as a gateway (or a sidecar) for microservices requests while
providing load balancing, logging, authentication, rate-limiting,
transformations, and more through plugins.

Features

  • Cloud-Native: Platform agnostic, Kong can run from bare metal to
    Kubernetes.
  • Dynamic Load Balancing: Load balance traffic across multiple upstream
    services.
  • Hash-based Load Balancing: Load balance with consistent hashing/sticky
    sessions.
  • Circuit-Breaker: Intelligent tracking of unhealthy upstream services.
  • Health Checks: Active and passive monitoring of your upstream services.
  • Service Discovery: Resolve SRV records in third-party DNS resolvers like
    Consul.
  • Serverless: Invoke and secure AWS Lambda or OpenWhisk functions directly
    from Kong.
  • WebSockets: Communicate to your upstream services via WebSockets.
  • gRPC: Communicate to your gRPC services and observe your traffic with logging
    and observability plugins
  • OAuth2.0: Easily add OAuth2.0 authentication to your APIs.
  • Logging: Log requests and responses to your system over HTTP, TCP, UDP,
    or to disk.
  • Security: ACL, Bot detection, whitelist/blacklist IPs, etc...
  • Syslog: Logging to System log.
  • SSL: Setup a Specific SSL Certificate for an underlying service or API.
  • Monitoring: Live monitoring provides key load and performance server
    metrics.
  • Forward Proxy: Make Kong connect to intermediary transparent HTTP proxies.
  • Authentications: HMAC, JWT, Basic, and more.
  • Rate-limiting: Block and throttle requests based on many variables.
  • Transformations: Add, remove, or manipulate HTTP requests and responses.
  • Caching: Cache and serve responses at the proxy layer.
  • CLI: Control your Kong cluster from the command line.
  • REST API: Kong can be operated with its RESTful API for maximum
    flexibility.
  • Geo-Replicated: Configs are always up-to-date across different regions.
  • Failure Detection & Recovery: Kong is unaffected if one of your Cassandra
    nodes goes down.
  • Clustering: All Kong nodes auto-join the cluster keeping their config
    updated across nodes.
  • Scalability: Distributed by nature, Kong scales horizontally by simply
    adding nodes.
  • Performance: Kong handles load with ease by scaling and using NGINX at
    the core.
  • Plugins: Extendable architecture for adding functionality to Kong and
    APIs.

For more info about plugins and integrations, you can check out the Kong
Hub
.

Distributions

Kong comes in many shapes. While this repository contains its core's source
code, other repos are also under active development:

Development

If you are planning on developing on Kong, you'll need a development
installation. The next branch holds the latest unreleased source code.

You can read more about writing your own plugins in the Plugin Development
Guide
, or browse an
online version of Kong's source code documentation in the Plugin Development
Kit (PDK) Reference
.

Docker

You can use Docker / docker-compose and a mounted volume to develop Kong by
following the instructions on Kong/kong-build-tools.

Vagrant

You can use a Vagrant box running Kong and Postgres that you can find at
Kong/kong-vagrant.

Source Install

Kong mostly is an OpenResty application made of Lua source files, but also
requires some additional third-party dependencies. We recommend installing
those by following the source install instructions at
https://docs.konghq.com/install/source/.

Instead of following the second step (Install Kong), clone this repository
and install the latest Lua sources instead of the currently released ones:

$ git clone https://github.com/Kong/kong
$ cd kong/

# you might want to switch to the development branch. See CONTRIBUTING.md
$ git checkout next

# install the Lua sources
$ luarocks make

Running for development

Check out the development section
of the default configuration file for properties to tweak in order to ease
the development process for Kong.

Modifying the lua_package_path
and lua_package_cpath
directives will allow Kong to find your custom plugin's source code wherever it
might be in your system.

Tests

Install the development dependencies (busted, luacheck) with:

$ make dev

Kong relies on three test suites using the busted testing library:

  • Unit tests
  • Integration tests, which require Postgres and Cassandra to be up and running
  • Plugins tests, which require Postgres to be running

The first can simply be run after installing busted and running:

$ make test

However, the integration and plugins tests will spawn a Kong instance and
perform their tests against it. As so, consult/edit the spec/kong_tests.conf
configuration file to make your test instance point to your Postgres/Cassandra
servers, depending on your needs.

You can run the integration tests (assuming both Postgres and Cassandra are
running and configured according to spec/kong_tests.conf) with:

$ make test-integration

And the plugins tests with:

$ make test-plugins

Finally, all suites can be run at once by simply using:

$ make test-all

Consult the run_tests.sh script for a more advanced example
usage of the tests suites and the Makefile.

Finally, a very useful tool in Lua development (as with many other dynamic
languages) is performing static linting of your code. You can use luacheck
(installed with make dev) for this:

$ make lint

Makefile

When developing, you can use the Makefile for doing the following operations:, Name, Description, ------------------:, -------------------------------------------------------, install, Install the Kong luarock globally, dev, Install development dependencies, lint, Lint Lua files in kong/ and spec/, test, Run the unit tests suite, test-integration, Run the integration tests suite, test-plugins, Run the plugins test suite, test-all, Run all unit + integration + plugins tests at once, ## Enterprise Support & Demo

If you are working in a large organization you should learn more about Kong
Enterprise
.

License

Copyright 2016-2020 Kong Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To the top