共计 3904 个字符,预计需要花费 10 分钟才能阅读完成。
Kong 是一个使用了 lua-nginx-module 运行在 Nginx 之上的 Lua 应用。Kong 是一个成熟的 API 网关解决方案。API 网关,即 API Gateway,是大型分布式系统中,为了保护内部服务而设计的一道屏障,可以提供高性能、高可用的 API 托管服务,从而帮助服务的开发者便捷地对外提供服务,而不用考虑安全控制、流量控制、审计日志等问题,统一在网关层将安全认证,流量控制,审计日志,黑白名单等实现。网关的下一层,是内部服务,内部服务只需开发和关注具体业务相关的实现。网关可以提供 API 发布、管理、维护等主要功能。开发者只需要简单的配置操作即可把自己开发的服务发布出去,同时置于网关的保护之下。
参考文档:
https://konghq.com/(kong 官网)
https://www.pocketdigi.com/bo…
https://www.postgresql.org/(postgresql 官网)
http://www.postgres.cn/index….
环境:
环境:Centos7
配置:2c4g
权限:root
一、安装 PostgreSQL
注意:请勿使用 "yum install kong-community-edition" 安装 Kong,必须指定版本号!"yum install kong-community-edition-0.14.1.*.noarch.rpm --nogpgcheck"
1、配置 yum 源
# 配置完 yum 库之后卸载之前安装的 Postgresql | |
yum erase postgresql* | |
# 删除遗留的数据 | |
rm -rf /var/lib/pgsql |
2、安装
下载 RPM(PostgreSQL YUM 源),找到对应的版本 CentOS 7 – x86_64
# 安装 yum 源 | |
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm | |
# 安装 PostgreSQL | |
yum install postgresql96-server postgresql96-contrib |
3、初始化数据库
# 初始化数据库 | |
/usr/pgsql-9.6/bin/postgresql96-setup initdb |
4、PostgreSQL 服务控制
# PostgreSQL 使用 systemctl 作为服务托管 | |
service postgresql-9.6 start/stop/restart/reload | |
# 或是 | |
systemctl start/stop/restart/status postgresql-9.6 | |
# 设置开机自启 | |
systemctl enable postgresql-9.6 |
5、卸载(顺便提供卸载 PostgreSQL 的命令)
# 卸载 PostgreSQL | |
yum erase postgresql96 |
6、修改密码
PostgreSQL 数据库默认会创建一个 Linux 系统用户 postgres,通过 passwd 命令可以设置密码。
# 创建 postgres 数据库账号 | |
su postgres | |
psql | |
ALTER USER postgres WITH PASSWORD '123456'; | |
\q | |
su root |
7、设置远程控制
7.1 修改 vi /var/lib/pgsql/9.6/data/postgresql.conf 文件,配置可以远程访问(正式环境按照白名单正确配置)
将 listen_addresses 前的 #去掉,并将 listen_addresses = ‘localhost’ 改成 listen_addresses = ‘*’;
#------------------------------------------------------------------------------ | |
# CONNECTIONS AND AUTHENTICATION | |
#------------------------------------------------------------------------------ | |
# - Connection Settings - | |
# 放开 IP 的限制 | |
listen_addresses = '*' # what IP address(es) to listen on; |
7.2 修改客户端认证配置文件 vi /var/lib/pgsql/9.6/data/pg_hba.conf
将 IPv4 区下的 127.0.0.1/32 修改为 0.0.0.0/0;将 ident 修改为 md5
# TYPE DATABASE USER ADDRESS METHOD | |
# "local" is for Unix domain socket connections only | |
local all all peer | |
local all all md5 | |
# IPv4 local connections: | |
### 假如 Kong 用户设置了密码,需要配置 MD5 认证 | |
host all all 127.0.0.1/32 md5 | |
### 容许远程向 Navicat 客户端访问 | |
host all all 0.0.0.0/0 md5 | |
# IPv6 local connections: | |
host all all ::1/128 ident |
7.3 重启使配置生效
7.4 注意:对应虚拟机或是在非本机的用户,需要注意防火墙端口开放。
8、添加 Kong 账号
# 为 postgres 用户增加 work 分组 | |
sudo usermod -a -G work postgres | |
# 添加 kong 数据库账户及数据库 | |
createuser -s -e kong | |
createdb -E UTF8 -O kong kong | |
# 添加 kong 系统用户名 | |
sudo adduser kong | |
# 可选 为 kong 系统用户设置密码 | |
sudo passwd kong |
9、新建 Kong 数据库
# 创建 postgres 数据库账号 | |
su postgres | |
psql | |
ALTER USER kong WITH PASSWORD '123456'; | |
\q | |
exit |
二、安装 Kong
官网文档:Kong Install
1、根据系统版本配置 yum 源
# 新建 Kong 的 yum reposit | |
vi /etc/yum.repos.d/kong-community-edition.repo | |
# 输入内容 | |
[kong-community-edition] | |
name=kong-community-edition | |
baseurl=https://kong.bintray.com/kong-community-edition-rpm/centos/7 | |
gpgcheck=0 | |
repo_gpgcheck=0 | |
enabled=1 |
2、安装
# 安装 epel | |
yum install epel-release | |
# 安装 Kong | |
yum localinstall https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong-community-edition-0.14.1.el7.noarch.rpm |
3、配置 Kong DataSource
# 创建配置 cp kong.conf.default kong.conf | |
vim /etc/kong/kong.conf | |
# 修改数据库配置 | |
database = postgres # Determines which of PostgreSQL or Cassandra | |
pg_host = 127.0.0.1 # The PostgreSQL host to connect to. | |
pg_port = 5432 # The port to connect to. | |
pg_user = kong # The username to authenticate if required. | |
pg_password = 123456 # The password to authenticate if required. | |
pg_database = kong # The database name to connect to. |
4、迁移 Kong(在数据库 Kong 中创建需要的表)
kong migrations up [-c /path/to/kong.conf]
5、启动 Kong
kong start [-c /path/to/kong.conf] | |
# 非 root 权限用户启动方式 | |
chmod -R 777 /usr/local/kong | |
chmod -R 777 /usr/local/share/lua/5.1 |
6、验证
curl -i http://localhost:8001/
三、安装 Kong Dashboard
官方文档:Kong Dashboard Install
1、安装 npm
# kong Dashboard 是 nodejs 写的 | |
sudo yum install nodejs |
2、安装
# Install Kong Dashboard | |
npm install -g kong-dashboard | |
# Start Kong Dashboard | |
kong-dashboard start --kong-url http://kong:8001 | |
# Start Kong Dashboard on a custom port | |
kong-dashboard start \ | |
--kong-url http://kong:8001 \ | |
--port [port] | |
# Start Kong Dashboard with basic auth | |
kong-dashboard start \ | |
--kong-url http://kong:8001 \ | |
--basic-auth user1=password1 user2=password2 | |
# See full list of start options | |
kong-dashboard start --help |
四、大功告成