目的
使用kong作為目錄/data/reports
的靜態資源服務器,為了測試,已於目錄/data/reports
下創建文件report.html
,如下:
<html>
<head></head>
<body><h1>測試報告</h1></body>
</html>
一、編寫nginx自定義模板
- 獲取kong自定義的nginx配置
[root@justmine ~]# kubectl -n [kong所在的命名空間] exec -it [kong pod完全限定名] sh
[root@justmine ~]# cat /usr/local/kong/nginx.conf
將nginx.conf內容復制出來,新的nginx
模板將在此內容上進行修改,避免修改后無法啟動kong容器。
- 添加托管靜態資源配置
worker_processes auto;
daemon off;
pid pids/nginx.pid;
error_log /dev/stderr notice;
worker_rlimit_nofile 65536;
events {
worker_connections 16384;
multi_accept on;
}
http {
include 'nginx-kong.conf';
# 上面為kong本身配置,下面為提供靜態資源的配置。
server {
listen 8005 default_server;
index index.html;
root /kong/nginx/www;
}
}
- 將上面的文件保存到k8s
[root@justmine ~]# kubectl -n [kong所在的命名空間] create configmap kong-nginx-custom-config --from-file=/data/kong/custom_nginx.template
二、配置kong
- 掛載自定義配置
volumes:
- configMap:
defaultMode: 420
name: kong-nginx-custom-config
name: kong-nginx-custom-config
---中間省略---
- mountPath: /usr/local/kong/nginx.conf
name: kong-nginx-custom-config
subPath: custom_nginx.template
- 掛載靜態資源目錄
- mountPath: /kong/nginx/www
name: staging-test-reports
---中間省略---
- hostPath:
path: /data/reports
type: DirectoryOrCreate
name: staging-test-reports
- 添加服務和路由
#!/bin/bash
set -e
IFS=$'\n\n'
echo ""
echo "Start building the cnd-sure-route dynamically..."
declare svcName="自定義服務名稱"
declare kongServiceBaseUrl="http://[kong admin地址]/services"
declare sureRouteSvcUrl="http://localhost:8005"
# resilience handle
# Maximum time in seconds that you allow the whole operation to take.
declare maxTime=5
# Maximum time in seconds that you allow the connection to the server to take.
declare maxConnectTime=2
declare retryCount=5
## add services
function createService()
{
curl -X POST $1 \
--connect-timeout $2 \
--max-time $3 \
--retry $4 \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"name\": \"$5\", \"url\": \"$6\"}";
}
createService $kongServiceBaseUrl $maxConnectTime $maxTime $retryCount $svcName $sureRouteSvcUrl
## add routes
declare kongRouteBaseUrl="http://[kong admin地址]/routes"
declare kongRouteDomain="[路由綁定的域名]"
function createRoute()
{
declare svcResponse=$(curl -X GET $kongServiceBaseUrl/$5 --connect-timeout $2 --max-time $3 --retry $4)
declare JQ_EXEC=`which jq`
declare svcId=$(echo $svcResponse | ${JQ_EXEC} .id | sed 's/\"//g')
declare defMethods="[\"GET\"]"
set +e
if [ -n "$8" ]; then
defMethods=$8
fi
if [ -z "$svcId" ]; then
echo "Warnning, failed to get the service[$5] identifier, route cannot be created.";
else
curl -X POST $1 \
--connect-timeout $2 \
--max-time $3 \
--retry $4 \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"service\": "{\"id\":\"$svcId\"}",\"paths\": "[\"$6\"]",\"methods\": "$defMethods",\"strip_path\":$7,\"hosts\": "[\"$kongRouteDomain\"]"}";
fi
set -e
}
declare sureRouteRouteUrl="/"
createRoute $kongRouteBaseUrl $maxConnectTime $maxTime $retryCount $svcName $sureRouteRouteUrl true
echo ""
echo "Dynamicly building the cnd-sure-route successfully !!!"
備注:也可以使用kong的dashboad完成上面shell腳本的功能。