目的
使用 kong 作为目录 / data/reports 的静态资源服务器, 为了测试, 已于目录 / data/reports 下创建文件 report.html, 如下:
- <HTML>
- <head>
- </head>
- <body>
- <h1>
- 测试报告
- </h1>
- </body>
- </HTML>
一, 编写 nginx 自定义模板
获取 kong 自定义的 nginx 配置
- [[email protected] ~]# kubectl -n [kong 所在的命名空间] exec -it [kong pod 完全限定名] sh
- [[email protected] ~]# 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
[[email protected] ~]# 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 脚本的功能.
三, 测试
来源: http://www.bubuko.com/infodetail-3459220.html