Svolence

kong introduce

Kong 初探(一)

Kong 安装

  • 可参照官网https://getkong.org进行有选择性的安装
1
2
3
4
5
wget https://github.com/Mashape/kong/releases/download/0.10.3/kong-0.10.3.trusty_all.deb
sudo apt-get update
sudo apt-get install openssl libpcre3 procps perl
sudo dpkg -i kong-0.10.3.trusty_all.deb
  • Kong 支持两种数据库postgrescassandra,默认postgres

安装postgresql

  • 添加postgresql源:
1
2
3
4
5
sudo touch /etc/apt/sources.list.d/pgdb.list
sudo vim /etc/apt/sources.list.d/pgdb.list
# 把下面这行数据添加到pgdb.list文件中:
deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
  • 执行下面的命令添加postgresql安装包的秘钥:
1
2
3
4
5
6
7
sudo wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
# 重启 postgresql
sudo service postgresql restart

Kong 数据库配置

  • 创建用户、数据库
1
2
3
4
5
6
7
8
9
10
11
12
➜ wenyue sudo -i -u postgres
postgres@vmlin0310:~$ CREATE USER kong; CREATE DATABASE kong OWNER kong;
CREATE: command not found
CREATE: command not found
postgres@vmlin0310:~$ psql
psql (9.6.3)
Type "help" for help.
postgres=# CREATE USER kong; CREATE DATABASE kong OWNER kong;
CREATE ROLE
CREATE DATABASE
postgres=#
  • 修改密码
1
2
3
4
5
6
7
8
9
10
11
12
➜ wenyue sudo su postgres
postgres@vmlin0310:/home/wenyue$ psql
psql (9.6.3)
Type "help" for help.
postgres=# \password kong
Enter new password:
Enter it again:
postgres=# exit
postgres-# \q
postgres@vmlin0310:/home/wenyue$ exit
exit
  • 验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
➜ wenyue psql -U kong -d kong -h 127.0.0.1
Password for user kong:
psql (9.6.3)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
kong=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
kong | kong | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
kong=>
  • 修改kong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
vim /etc/kong/kong.conf
181 #------------------------------------------------------------------------------
182 # DATASTORE
183 #------------------------------------------------------------------------------
184
185 # Kong will store all of its data (such as APIs, consumers and plugins) in
186 # either Cassandra or PostgreSQL.
187 #
188 # All Kong nodes belonging to the same cluster must connect themselves to the
189 # same database.
190
191 #database = postgres # Determines which of PostgreSQL or Cassandra
192 # this node will use as its datastore.
193 # Accepted values are `postgres` and
194 # `cassandra`.
195
196 pg_host = 127.0.0.1 # The PostgreSQL host to connect to.
197 pg_port = 5432 # The port to connect to.
198 pg_user = kong # The username to authenticate if required.
199 pg_password = 123456 # The password to authenticate if required.
200 pg_database = kong # The database name to connect to.
201
202 #pg_ssl = off # Toggles client-server TLS connections
203 # between Kong and PostgreSQL.
204
205 #pg_ssl_verify = off # Toggles server certificate verification if
206 # `pg_ssl` is enabled.
207 # See the `lua_ssl_trusted_certificate`
208 # setting to specify a certificate authority

启动Kong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ kong start
Usage: kong COMMAND [OPTIONS]
The available commands are:
check
cluster
compile
health
migrations
quit
reload
restart
roar
start
stop
version
Options:
--v verbose
--vv debug
# Kong is running
$ curl 127.0.0.1:8001

Kong-dashboard 安装

  • 源码安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Pull repository
git clone https://github.com/PGBI/kong-dashboard.git
cd kong-dashboard
git checkout 2.0
# Build Kong Dashboard
npm install
npm run build
# Start Kong Dashboard
npm start
# To start Kong Dashboard on a custom port or with basic auth
npm start -- [-p port] [-a user=password]

demo

kong-dashboard