Kong網關部署

  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

四、大功告成

clipboard.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章