1 Kong網(wǎng)關(guān)介紹
Kong是在Nginx的基礎(chǔ)上構(gòu)建的,更確切的說(shuō)Kong是在Nginx中運(yùn)行的Lua應(yīng)用程序,Kong包含了OpenResty。Kong 提供了大量的插件和動(dòng)態(tài)管理API功能雷客。
Kong 大致功能特性
- 動(dòng)態(tài)路由: Kong 的背后是 OpenResty+Lua,所以從 OpenResty 繼承了動(dòng)態(tài)路由的特性
- 熔斷
- 健康檢查
- 日志: 可以記錄通過(guò) Kong 的 HTTP滓鸠,TCP猜敢,UDP 請(qǐng)求和響應(yīng)。
- 鑒權(quán): 權(quán)限控制耐量,IP 黑白名單飞蚓,同樣是 OpenResty 的特性
- 監(jiān)控: Kong 提供了實(shí)時(shí)監(jiān)控插件
- 認(rèn)證: 如數(shù)支持 HMAC, JWT, Basic, OAuth2.0 等常用協(xié)議
- 限流
- REST API: 通過(guò) Rest API 進(jìn)行配置管理,從繁瑣的配置文件中解放
- 可用性: 天然支持分布式
- 高性能: 背靠非阻塞通信的 nginx廊蜒,性能自不用說(shuō)
- 插件機(jī)制: 提供眾多開(kāi)箱即用的插件趴拧,且有易于擴(kuò)展的自定義插件接口,用戶可以使用 Lua 自行開(kāi)發(fā)插件
2 安裝包準(zhǔn)備
Kong的一些配置存儲(chǔ)可以是單機(jī)文件山叮,也可以是postgre著榴,cassandra。本文使用cassandra做Kong的數(shù)據(jù)庫(kù)屁倔。
3 安裝單機(jī)版
3.1 安裝Cassandra
Cassandra cqlsh 依賴了python2.7 我使用的Centos7自帶脑又。假設(shè)已經(jīng)安裝了JDK1.8.
Cassandra目前也是單機(jī)部署。
tar -xvf apache-cassandra-3.11.6-bin.tar.gz
cd apache-cassandra-3.11.6
Cassandra 不推薦使用root用戶啟動(dòng)服務(wù)锐借,我們創(chuàng)建一個(gè)cassandra組和用戶
groupadd cassandra
useradd cassandra -g cassandra
su - cassandra
chown -R cassandra:cassandra apache-cassandra-3.11.6
需要修改Cassandra配置文件问麸,找到以下屬性進(jìn)行修改
[root@sybj-int-26 apache-cassandra-3.11.6]# cd conf/
[root@sybj-int-26 conf]# vim cassandra.yaml
# cassandra 節(jié)點(diǎn)之間的監(jiān)聽(tīng)地址
listen_address: 10.0.3.26
# rpc 監(jiān)聽(tīng)地址
rpc_address: 10.0.3.26
seed_provider:
# Addresses of hosts that are deemed contact points.
# Cassandra nodes use this list of hosts to find each other and learn
# the topology of the ring. You must change this if you are running
# multiple nodes!
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "10.0.3.26"
修改conf/jvm.options配置文件,設(shè)置一下內(nèi)存
-Xms6G
-Xmx6G
啟動(dòng)Cassandra
[cassandra@sybj-int-26 apache-cassandra-3.11.6]$ cd bin
[cassandra@sybj-int-26 bin]$ ./cassandra
添加到開(kāi)機(jī)啟動(dòng)
[Unit]
Description=Cassandra Server Service
After=network.service
[Service]
Type=simple
Environment=JAVA_HOME=/usr/local/jdk1.8.0_231
PIDFile=/opt/ylbzj/apache-cassandra-3.11.6/bin/cassandra.pid
User=cassandra
Group=cassandra
ExecStart=/opt/ylbzj/apache-cassandra-3.11.6/bin/cassandra -p /opt/ylbzj/apache-cassandra-3.11.6/bin/cassandra.pid
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
3.1 安裝Kong
Cassandra單機(jī)版安裝到了10.0.3.26钞翔,另找一臺(tái)機(jī)器10.0.3.25安裝Kong Gateway
yum install kong-2.0.2.*.noarch.rpm --nogpgcheck
執(zhí)行安裝命令后严卖,安裝成功,但是會(huì)報(bào)一個(gè)腳本錯(cuò)誤布轿,暫時(shí)不處理哮笆。
kong安裝成功后配置文件在/etc/kong/目錄
修改Kong配置文件
cd /etc/hong
cp kong.conf.default kong.conf
vim kong.conf
數(shù)據(jù)庫(kù)使用cassandra
database = cassandra
# cassandra 服務(wù)地址
cassandra_contact_points = 10.0.3.26
cassandra_port = 9042
# Kong poll 數(shù)據(jù)庫(kù)的周期配置。
db_update_frequency = 5
# 使用Cassandra數(shù)據(jù)庫(kù)需要指定此配置驮捍,數(shù)據(jù)變更傳播到 kong 集群的最大時(shí)間是 db_update_frequency + db_update_propagation 秒疟呐。(cassandra的數(shù)據(jù)一致性延遲)。
db_update_propagation = 5
# 該配置項(xiàng)的時(shí)間(單位秒)是 kong 緩存數(shù)據(jù)庫(kù)實(shí)體的時(shí)間(包括緩存命中或者穿透)东且,該存活時(shí)間是一種保護(hù)措施启具,以防 kong 節(jié)點(diǎn)漏掉處理緩存無(wú)效事件,避免舊數(shù)據(jù)長(zhǎng)時(shí)間沒(méi)有被清理珊泳。當(dāng)緩存生存時(shí)間到了鲁冯,緩存值將會(huì)被清理掉拷沸,下一次將會(huì)從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)并再次緩存起來(lái)
db_cache_ttl = 3600
啟動(dòng)
# 初始化數(shù)據(jù)庫(kù)
kong migrations bootstrap
# 啟動(dòng)
kong start
測(cè)試Admin 服務(wù),能否訪問(wèn)
[root@sybj-int-25 kong]# curl -i http://localhost:8001
HTTP/1.1 200 OK
Date: Thu, 26 Mar 2020 09:14:00 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.2
Content-Length: 8959
X-Kong-Admin-Latency: 160
- 添加服務(wù)
[root@sybj-int-25 ~]# curl -i -X POST --url http://localhost:8001/services/ --data 'name=test_baidu' --data 'url=http://baidu.com'
HTTP/1.1 201 Created
Date: Thu, 26 Mar 2020 10:08:55 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.2
Content-Length: 289
X-Kong-Admin-Latency: 11
{"host":"baidu.com","created_at":1585217335,"connect_timeout":60000,"id":"49f4423b-87b9-45f1-a57b-c7f6f3fd4b40","protocol":"http","name":"test_baidu","read_timeout":60000,"port":80,"path":null,"updated_at":1585217335,"retries":5,"write_timeout":60000,"tags":null,"client_certificate":null}
- 添加服務(wù)路由
[root@sybj-int-25 ~]# curl -i -X POST --url http://localhost:8001/services/test_baidu/routes --data 'hosts[]=baidu.com'
HTTP/1.1 201 Created
Date: Thu, 26 Mar 2020 10:13:09 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.2
Content-Length: 427
X-Kong-Admin-Latency: 24