基于 nginx、openresty 的开源网关服务 kong 搭建

本贴最后更新于 2179 天前,其中的信息可能已经物是人非

介绍

Kong 是一款基于 Nginx_Lua 模块写的高可用,易扩展由 Mashape 公司开源的 API Gateway 项目。由于 Kong 是基于 Nginx 的,所以可以水平扩展多个 Kong 服务器,通过前置的负载均衡配置把请求均匀地分发到各个 Server,来应对大批量的网络请求。

安装

kong 依赖 postgresql 或 cassandra 存储数据,cassandra 没玩过。我们装一下 pg 吧,这个还是比较常见的。

pg 安装

https://www.postgresql.org/download/linux/redhat/
按步骤执行即可

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm    
yum install postgresql10    
yum install postgresql10-server
/usr/pgsql-10/bin/postgresql-10-setup initdb  
systemctl enable postgresql-10  
systemctl start postgresql-10

修改配置

允许外部访问

1、 修改 postgresql.conf

listen_addresses = 'localhost' 改为 listen_addresses = '*'

2、 修改 pg_hba.conf

在文件末尾加入:host all all 0.0.0.0/0 md5

修改密码
[root@localhost /]# su postgres
bash-4.2$ cd /usr/pgsql-10/bin
bash-4.2$ ./psql
postgres=# \password postgres
Enter new password: 
Enter it again: 
postgres=# 
创建 kong 用户
postgres=# CREATE USER kong WITH LOGIN PASSWORD 'kong';
CREATE ROLE
创建数据库
postgres=# CREATE USER kong WITH LOGIN PASSWORD 'kong';
CREATE ROLE

kong 安装

选择对应的系统下载,https://konghq.com/install/。我们使用 centos 系统的 rpm 进行安装。

因当前 kong-dashboard 不支持 0.13.x 以上版本,下载历史 rpm 版本https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong-community-edition-0.12.3.el7.noarch.rpm

yum -y install kong-community-edition.rpm

kong 配置

cp /etc/kong/kong.conf.default /etc/kong/kong.conf

修改 kong.conf

1、 #proxy_listen = 127.0.0.1:8000 改为 proxy_listen = 0.0.0.0:80

2、 #admin_listen = 127.0.0.1:8001 改为 admin_listen = 0.0.0.0:8001

3、 #pg_password = 改为 pg_password = kong 【postgresql 中创建的用户密码】

kong 启动

[root@localhost kong]# kong start

2018/05/08 03:28:19 [warn] ulimit is currently set to "1024". For better performance set it to at least "4096" using "ulimit -n"
Kong started

kong-dashboard

访问 127.0.0.1:8001 可以管理我们的 kong 服务, 通过 dashboard 管理 kong 更直观。

安装

1、环境装有 nodejs/npm

[root@localhost kong]# node -v
v8.11.1
[root@localhost kong]# npm -v
5.6.0

2、npm install -g kong-dashboard

3、kong-dashboard start --kong-url http://127.0.0.1:8001


管理

访问 127.0.0.1:8080 可以通过 kong-dashboard 进行管理
1、 api 接口创建

apipng

2、 创建 api 的处理插件

pluginpng

pluginsettingpng

kong 支持的插件有很多, 有一小部分插件是收费的。
https://konghq.com/plugins/

  • Gateway
    5 引用 • 2 回帖
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 34 关注
  • kong
    2 引用 • 2 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...