Kong是一个在Nginx中运行的Lua应用程序,并且通过lua-nginx模块实现。Kong不是用这个模块编译Nginx,而是与OpenResty一起发布,OpenResty已经包含了lua-nginx-module
OpenResty 也不是 Nginx的分支,而是一组扩展其功能的模块。
Kong是一个可扩展的开源API网关,运作在RESTfull API之前,提供统一的入口,并且通过插件的形式进行扩展,插件提供了平台核心功能意外的功能和服务,例如鉴权、流控等等。
Kong 是 Mashape 开源的高性能高可用 API 网关和 API 服务管理层,一款基于 Nginx_Lua 模块写的高可用服务网关,由于 Kong 是基于 Nginx 的,所以它是一个可扩展的服务,只需要添加多台服务器就可以配置成一个集群。通过前置的负载均衡配置把请求均匀地分发到各个 Server,来应对大批量的网络请求。
环境准备
Ubuntu 18.04
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
安装postgresql 10
sudo echo 'deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main' > /etc/apt/sources.list.d/postgresql.list
sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-10
配置postgresql,允许远程访问
vim /etc/postgresql/10/main/postgresql.conf
#listen_addresses = 'localhost' 的注释去掉并改为 listen_addresses = '*'
重启postgresql
sudo service postgresql restart
#重启postgresql
sudo service postgresql status
#查看状态
配置Kong用户
在Ubuntu中安装Postgresql之后,会自动添加一个postgresql的操作系统的用户,密码是随机的。并且会自动生成一个名字为postgresql的数据库,用户名也是postgresql,密码也是随机的
sudo -u postgres psql
postgres=# ALTER USER postgres WITH PASSWORD '123456';
#创建kong用户
postgres=# create user kong with password 'kong';
postgres=# create database kong owner kong;
postgres=# \q
#退出
安装Kong
curl -Lo kong.2.5.0.amd64.deb "https://download.konghq.com/gateway-2.x-ubuntu-$(lsb_release -cs)/pool/all/k/kong/kong_2.5.0_amd64.deb"
sudo dpkg -i kong.2.5.0.amd64.deb
配置Kong
sudo vim /usr/local/share/lua/5.1/kong/templates/kong_defaults.lua
#把pg_password = NONE 改成 pg_password = kong
#修改admin_listen = 0.0.0.0:8001 reuseport backlog=16384, 0.0.0.0:8444 http2 ssl reuseport backlog=16384 支持远程管理
初始化Kong启动
sudo kong migrations bootstrap
#初始化
sudo kong start
查看状态
curl -i http://localhost:8001/
HTTP/1.1 200 OK
Date: Mon, 26 Apr 2021 03:53:09 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Content-Length: 10910
X-Kong-Admin-Latency: 349
Server: kong/2.4.0
GitHub上开源且还在更新维护的dashboard有两个:
- kong-dashboard:Kong Dashboard 3.3.0 is only partially compatible with Kong 0.13. It does not support the new Service and Route objects introduced in Kong 0.13.
- konga:From 0.14.0 onwards, Konga is ONLY compatible with Kong >= 1.0.0
我的版本是kong2.0 需要安装konga
npm环境
sudo apt install libssl1.0-dev
sudo apt install nodejs-dev
sudo apt install node-gyp
sudo apt install npm
安装konga
cd /opt/
git clone https://github.com/pantsel/konga.git
cd konga
git checkout -b v0.14.9-1 0.14.9
git checkout v0.14.9-1
sudo npm install konga
sudo npm install pm2 -g
sudo npm install dotenv
sudo npm install dotenv-extended
sudo npm install angular
sudo npm install minimist
sudo npm install lodash
sudo npm install sails -g
sudo cp .env_example .env
sudo vim .env
根据数据库配置修改
PORT=1337
NODE_ENV=production
KONGA_HOOK_TIMEOUT=120000
#DB_HOST=127.0.0.1
#DB_PORT=5432
#DB_USER=kong
#DB_PASSWORD=kong
#DB_DATABASE=kong
DB_ADAPTER=postgres
DB_URI=postgresql://kong:kong@localhost:5432/kong
KONGA_LOG_LEVEL=warn
TOKEN_SECRET=some_secret_token
启动报错
sudo npm run production &
[1] 31645
> kongadmin@0.14.9 production /opt/konga
> node --harmony app.js --prod
To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.
To do that, run `npm install sails`
Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.
When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,
but if it doesn't, the app will run with the global sails instead!
解决方法
sudo npm install -g sails
/usr/local/bin/sails -> /usr/local/lib/node_modules/sails/bin/sails.js
/usr/local/lib
└── sails@1.4.2
sudo vim package.json
#修改 "sails": "~1.4.2",
sudo npm install sails
数据初始化
node ./bin/konga.js prepare [--adapter postgres --uri postgresql://localhost:5432/konga]