Kong是一個基於Nginx的開源網關係統,參考官網來部署一套網關係統來使用。
機器準備
節點A:192.168.0.10
節點B:192.168.0.11
節點A,部署一個Kong Node和Postgresql
節點B, 部署一個Kong Node,數據庫指向節點A的postgresql
安裝
1.安裝數據庫postgresql
docker network create kong-net
docker run -d --name kong-database \\
--network=kong-net \\
-p 5432:5432 \\
-e "POSTGRES_USER=kong" \\
-e "POSTGRES_DB=kong" \\
postgres:9.6
docker run --rm \\
--network=kong-net \\
-e "KONG_DATABASE=postgres" \\
-e "KONG_PG_HOST=kong-database" \\
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \\
kong:latest kong migrations up
2.postgresql主備熱切換
TODO
為了高可用性,使用postgresql的流複製以及雙機熱備
3.kong集群
docker run -d --name kong \\
--network=kong-net \\
-e "KONG_DATABASE=postgres" \\
-e "KONG_PG_HOST=kong-database" \\
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \\
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \\
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \\
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \\
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \\
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \\
-e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \\
-p 8000:8000 \\
-p 8443:8443 \\
-p 8001:8001 \\
-p 8444:8444 \\
kong:latest
docker run -d --name kong \\
-e "KONG_DATABASE=postgres" \\
-e "KONG_PG_HOST=192.168.0.10" \\
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \\
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \\
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \\
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \\
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \\
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \\
-e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \\
-p 8000:8000 \\
-p 8443:8443 \\
-p 8001:8001 \\
-p 8444:8444
同步時間默認是5s,可以通過官網集群資料進行修改
節點A添加
curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'
節點B查詢
curl http://localhost:8001/apis/
>>>>>>
{
\t"total": 1,
\t"data": [{
\t\t"created_at": 1527732097194,
\t\t"strip_uri": true,
\t\t"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
\t\t"hosts": ["example.com"],
\t\t"name": "example-api",
\t\t"http_if_terminated": false,
\t\t"https_only": false,
\t\t"retries": 5,
\t\t"preserve_host": false,
\t\t"upstream_connect_timeout": 60000,
\t\t"upstream_read_timeout": 60000,
\t\t"upstream_send_timeout": 60000,
\t\t"upstream_url": "http:\\/\\/mockbin.org"
\t}]
}
節點B添加
curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api2' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'
節點A查詢
curl http://localhost:8001/apis/
>>>>>>
{
\t"total": 2,
\t"data": [{
\t\t"created_at": 1527732097194,
\t\t"strip_uri": true,
\t\t"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
\t\t"hosts": ["example.com"],
\t\t"name": "example-api",
\t\t"http_if_terminated": false,
\t\t"https_only": false,
\t\t"retries": 5,
\t\t"preserve_host": false,
\t\t"upstream_connect_timeout": 60000,
\t\t"upstream_read_timeout": 60000,
\t\t"upstream_send_timeout": 60000,
\t\t"upstream_url": "http:\\/\\/mockbin.org"
\t}, {
\t\t"created_at": 1527732578045,
\t\t"strip_uri": true,
\t\t"id": "29f12209-f45b-45dc-b6e1-f64e855d7387",
\t\t"hosts": ["example.com"],
\t\t"name": "example-api2",
\t\t"http_if_terminated": false,
\t\t"https_only": false,
\t\t"retries": 5,
\t\t"preserve_host": false,
\t\t"upstream_connect_timeout": 60000,
\t\t"upstream_read_timeout": 60000,
\t\t"upstream_send_timeout": 60000,
\t\t"upstream_url": "http:\\/\\/mockbin.org"
\t}]
}
配置nginx負載Kong集群
對外提供9000端口,通過nginx的負載均衡,根據自身的需求,配置不同的算法,反向代理到kong的集群中
upstream kong {
# 負載均衡算法,根據最少連接數
least_conn;
server 192.168.0.10:8000;
server 192.168.0.11:8000;
}
server {
# 這些端口要修改為實際情況
listen 9100;
#include snippets/ssl-api.example.com.conf;
#include snippets/ssl-params.conf;
# https 因為安全原因,需要禁用 gzip
# 但是在一些場景下,不需要禁用
# gzip off;
gzip on;
# 設置允許壓縮的頁面最小字節數
gzip_min_length 1000;
gzip_buffers 4 16k;
gzip_http_version 1.1;
# 1~9,默認為1,數值越大,壓縮率越高,CPU佔用越多,時間越久
gzip_comp_level 3;
gzip_vary on;
# 禁用對 IE 6 使用 gzip 壓縮
gzip_disable "MSIE [1-6]\\.";
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/javascript "application/javascript; charset=utf-8" application/xml application/xml+rss application/json "application/json; charset=utf-8" fo
nt/ttf font/otf image/svg+xml;
# 設置最大允許的POST數據量,如果提交的文件超過這個值,會出現413錯誤
client_max_body_size 20m;
keepalive_timeout 15;
# 不顯示 nginx 的版本號
server_tokens off;
## Individual nginx logs
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# 這裡不能用 ^~ 因為後面跟的是字符串不是正則表達式
# 只匹配符合規則的,其他都返回 404
location / {
# 這條命令會將 headers 中的 server 設置為 nginx 代理的後端網站所使用的
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://kong;
expires -1;
}
# 主頁返回 404
location = / {
return 404;
}
}
使用時,將所有額請求都轉發到http://192.168.0.10:9100上,然後後續Kong會根據配置進行相應的處理以及相關的插件過濾
閱讀更多 戰渣渣 的文章
關鍵字: PostgreSQL 開源 數據庫