使用 KONG 和 PgSQL 进行 api 网关服务的搭建

1,016 阅读1分钟

参考文档

KONG 服务安装过程

# 更新yum库
1. yum update -y
# 安装wget包
2. yum install -y wget
# 下载kong安装源
3. wget https://bintray.com/kong/kong-rpm/rpm -O bintray-kong-kong-rpm.repo

4. export major_version=`grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d "." -f1`

5. sed -i -e 's/baseurl.*/&\/centos\/'$major_version''/ bintray-kong-kong-rpm.repo

6. sudo mv bintray-kong-kong-rpm.repo /etc/yum.repos.d/
# 更新yum库
7. sudo yum update -y
# yum安装kong包
8. sudo yum install -y kong

PgSQL 数据库安装过程

1. yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.

2. yum install postgresql95 -y

3. yum install postgresql95-server -y
# 初始化数据库
4. /usr/pgsql-9.5/bin/postgresql95-setup initdb

5. systemctl enable postgresql-9.5

6. 修改 /var/lib/pgsql/9.5/data/postgresql.conf
listen_addresses = '*'

7. 修改 /var/lib/pgsql/9.5/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

host    all             all             0.0.0.0/0               md5
host    all             all             ::/0                    md5

8. systemctl start postgresql-9.5

创建 PgSQL 中 KONG 需要的库

1. sudo -u postgres psql

# 创建用户 kong
2. CREATE USER kong;

# 创建用户 kong 的数据库
3. CREATE DATABASE kong OWNER kong;

# 修改用户 kong 和 postgres 的密码
4. ALTER USER kong WITH PASSWORD 'password'; ALTER USER postgres WITH PASSWORD 'password';

# 重启 postgresql 服务
5. systemctl restart postgresql-9.5

6. 重新配置 kong 中 pgsql (/etc/kong/kong.conf) 的相关配置
pg_host = 127.0.0.1             # Host of the Postgres server.
pg_port = 5432                  # Port of the Postgres server.
pg_user = kong                  # Postgres user.
pg_password = password          # Postgres user's password.
pg_database = kong              # The database name to connect to.

# 退出数据库后,进行启动
7. kong migrations bootstrap [-c /path/to/kong.conf]

8. kong start [-c /path/to/kong.conf]
  • 检测有无安装成功 curl -i http://127.0.0.1:8001

安装 konga (可视化操作界面)

# 进入 pgsql
1. sudo -u postgres psql

# 创建 konga 所需要的数据库
2. CREATE DATABASE konga OWNER kong;

下载地址 安装说明 使用说明