Kong Gateway 是一个轻量级、快速、灵活的云原生 API 网关。API 网关是一种反向代理,可让您管理、配置和将请求路由到您的 API。
官网
Kong官方文档:Kong Docs
Kong下载地址:Installations - KongHQ
RPM包下载: https://docs.konghq.com/install/centos/
参考文档:php实战kong做微服务架构一(kong简介与安装)_小猴子喝牛奶的博客-CSDN博客_kong php
部署Kong
通过Docker部署
(1)创建网桥
docker network create kong-net
(2)安装postgres(你可以使用源码包安装,这里用docker安装)
- docker run -d --name kong-database \
- --network=kong-net \
- -p 5432:5432 \
- -e "POSTGRES_USER=kong" \
- -e "POSTGRES_PASSWORD=Knight8888" \
- -e "POSTGRES_DB=kong" \
- -v kong_data:/var/lib/postgresql/data \
- postgres:9.6
(3) 查看版本
# 查看版本
kong version
(4) 编辑配置文件
- cp /etc/kong/kong.conf.default /etc/kong/kong.conf
- vim /etc/kong/kong.conf,用于配置数据库的地址
-
- -- 省略
- database = postgres # Determines which of PostgreSQL or Cassandra
- # this node will use as its datastore.
- # Accepted values are `postgres`,
- # `cassandra`, and `off`.
-
- pg_host = 172.19.193.161 # Host of the Postgres server.
- pg_port = 5432 # Port of the Postgres server.
- #pg_timeout = 5000 # Defines the timeout (in ms), for connecting,
- # reading and writing.
-
- pg_user = kong # Postgres user.
- pg_password = kong@8888 # Postgres user's password.
- pg_database = kong # The database name to connect to.
-
- #pg_schema = # The database schema to use. If unspecified,
- # Kong will respect the `search_path` value of
- # your PostgreSQL instance.
-
- pg_ssl = off # Toggles client-server TLS connections
- # between Kong and PostgreSQL.
- # Because PostgreSQL uses the same port for TLS
- # and non-TLS, this is only a hint. If the
- # server does not support TLS, the established
- # connection will be a plain one.
-
- pg_ssl_verify = off # Toggles server certificate verification if
- # `pg_ssl` is enabled.
- # See the `lua_ssl_trusted_certificate`
- # setting to specify a certificate authority.
-- 省略
通过yum安装部署PostGreSQL
(1)安装软件
- $ yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- $ yum install -y postgresql96-server
- $ systemctl enable postgresql-9.6
(2)修改环境变量
- $ vim /etc/profile
- $ export /usr/pgsql-9.6/bin:$PATH
- $ source /etc/profile
(3)初始化数据
- $ su - postgres
- $ initdb -D /home/data/postgres/pgsql/data
- $ psql
#创建kong数据库
- $ CREATE USER kong; CREATE DATABASE kong OWNER kong;
- $ ALTER USER kong PASSWORD 'qq@123';
#创建konga数据库
- $ CREATE USER konga; CREATE DATABASE konga OWNER konga;
- $ ALTER USER konga PASSWORD 'qq@123';
(4) 修改数据目录
- $ sed -i "s#/var/lib/pgsql/9.6/data/#/home/data/postgres/pgsql/data/#g" /usr/lib/systemd/system/postgresql-9.6.service
- $ systemctl reload postgresql-9.6
修改配置文件:
- $cat /etc/kong/kong.conf |grep "^pg_"
- pg_host = 172.19.193.161 # Host of the Postgres server.
- pg_port = 5432 # Port of the Postgres server.
- pg_user = kong # Postgres user.
- pg_password = Knight8888 # Postgres user's password.
- pg_database = kong # The database name to connect to.
- pg_ssl = off # Toggles client-server TLS connections
- pg_ssl_verify = off # Toggles server certificate verification if
初始化数据
- $sudo kong migrations bootstrap
- Bootstrapping database...
- migrating core on database 'kong'...
- core migrated up to: 000_base (executed)
- core migrated up to: 003_100_to_110 (executed)
- core migrated up to: 004_110_to_120 (executed)
- core migrated up to: 005_120_to_130 (executed)
- core migrated up to: 006_130_to_140 (executed)
- core migrated up to: 007_140_to_150 (executed)
- core migrated up to: 008_150_to_200 (executed)
- core migrated up to: 009_200_to_210 (executed)
- core migrated up to: 010_210_to_211 (executed)
- core migrated up to: 011_212_to_213 (executed)
- core migrated up to: 012_213_to_220 (executed)
- core migrated up to: 013_220_to_230 (executed)
- migrating acl on database 'kong'...
- acl migrated up to: 000_base_acl (executed)
- acl migrated up to: 002_130_to_140 (executed)
- acl migrated up to: 003_200_to_210 (executed)
- acl migrated up to: 004_212_to_213 (executed)
- migrating acme on database 'kong'...
- acme migrated up to: 000_base_acme (executed)
- migrating basic-auth on database 'kong'...
- basic-auth migrated up to: 000_base_basic_auth (executed)
- basic-auth migrated up to: 002_130_to_140 (executed)
- basic-auth migrated up to: 003_200_to_210 (executed)
- migrating bot-detection on database 'kong'...
- bot-detection migrated up to: 001_200_to_210 (executed)
- migrating hmac-auth on database 'kong'...
- hmac-auth migrated up to: 000_base_hmac_auth (executed)
- hmac-auth migrated up to: 002_130_to_140 (executed)
- hmac-auth migrated up to: 003_200_to_210 (executed)
- migrating ip-restriction on database 'kong'...
- ip-restriction migrated up to: 001_200_to_210 (executed)
- migrating jwt on database 'kong'...
- jwt migrated up to: 000_base_jwt (executed)
- jwt migrated up to: 002_130_to_140 (executed)
- jwt migrated up to: 003_200_to_210 (executed)
- migrating key-auth on database 'kong'...
- key-auth migrated up to: 000_base_key_auth (executed)
- key-auth migrated up to: 002_130_to_140 (executed)
- key-auth migrated up to: 003_200_to_210 (executed)
- migrating oauth2 on database 'kong'...
- oauth2 migrated up to: 000_base_oauth2 (executed)
- oauth2 migrated up to: 003_130_to_140 (executed)
- oauth2 migrated up to: 004_200_to_210 (executed)
- oauth2 migrated up to: 005_210_to_211 (executed)
- migrating rate-limiting on database 'kong'...
- rate-limiting migrated up to: 000_base_rate_limiting (executed)
- rate-limiting migrated up to: 003_10_to_112 (executed)
- rate-limiting migrated up to: 004_200_to_210 (executed)
- migrating response-ratelimiting on database 'kong'...
- response-ratelimiting migrated up to: 000_base_response_rate_limiting (executed)
- migrating session on database 'kong'...
- session migrated up to: 000_base_session (executed)
- session migrated up to: 001_add_ttl_index (executed)
- 41 migrations processed
- 41 executed
- Database is up-to-date
- 启动:
-
- shell>kong start
- Kong started
-
- shell>ps -ef |grep nginx
- root 8212 1 0 16:34 ? 00:00:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -p /usr/local/kong -c nginx.conf
- kong 8213 8212 0 16:34 ? 00:00:00 nginx: worker process
- kong 8214 8212 0 16:34 ? 00:00:00 nginx: worker process
- kong 8215 8212 0 16:34 ? 00:00:00 nginx: worker process
- kong 8216 8212 0 16:34 ? 00:00:00 nginx: worker process
- root 8260 1732 0 16:34 pts/0 00:00:00 grep --color=auto nginx
安装konga
(1)安装node.js环境
安装konga之前我们先安装一下node.js环境,
- $ cd /opt/
- $ wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz
- $ tar xf node-v10.16.0-linux-x64.tar.xz
- $ mv node-v10.16.0-linux-x64 /usr/local/node
# 验证安装有效性
- $ node -v
- $ npm -v
#安装pm2
npm install -g pm2
环境变量:
- ## set node env
- export NODE=/usr/local/node
- export PATH=$PATH:$NODE/bin
(2)开始安装
- $ cd /home/data
- $ git clone https://github.com/pantsel/konga.git --depth=1
- #替换源
- $ echo "registry = https://registry.npm.taobao.org" > ~/.npmrc
- $ cd konga
- # 安装npm 依赖
- $ npm i