k8s operation and maintenance in mind - how to make kong gateway deployed to k8s managed custom static resources?
purpose
Use kong directory as /data/reports
static resource server, in order to test directory was /data/reports
created under the file report.html
, as follows:
<html>
<head></head> <body><h1>测试报告</h1></body> </html>
First, write a custom template nginx
- Get kong custom configuration nginx
[root@justmine ~]# kubectl -n [kong所在的命名空间] exec -it [kong pod完全限定名] sh
[root@justmine ~]# cat /usr/local/kong/nginx.conf
The nginx.conf copied out the content, the new nginx
template will be modified in this content, avoiding the revised unable to start kong container.
- Add hosting static resource allocation
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;
}
}
- Save the file above to k8s
[root@justmine ~]# kubectl -n [kong所在的命名空间] create configmap kong-nginx-custom-config --from-file=/data/kong/custom_nginx.template
Second, the configuration kong
- Mount Custom Configuration
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
- Mount static resource directory
- mountPath: /kong/nginx/www
name: staging-test-reports
---中间省略--- - hostPath: path: /data/reports type: DirectoryOrCreate name: staging-test-reports
- Add services and routing
#!/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 !!!"
Note: You can also use kong dashboad completion of the above shell script.
Third, the test
purpose
Use kong directory as /data/reports
static resource server, in order to test directory was /data/reports
created under the file report.html
, as follows:
<html>
<head></head> <body><h1>测试报告</h1></body> </html>
First, write a custom template nginx
- Get kong custom configuration nginx
[root@justmine ~]# kubectl -n [kong所在的命名空间] exec -it [kong pod完全限定名] sh
[root@justmine ~]# cat /usr/local/kong/nginx.conf
The nginx.conf copied out the content, the new nginx
template will be modified in this content, avoiding the revised unable to start kong container.
- Add hosting static resource allocation
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;
}
}
- Save the file above to k8s
[root@justmine ~]# kubectl -n [kong所在的命名空间] create configmap kong-nginx-custom-config --from-file=/data/kong/custom_nginx.template
Second, the configuration kong
- Mount Custom Configuration
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
- Mount static resource directory
- mountPath: /kong/nginx/www
name: staging-test-reports
---中间省略--- - hostPath: path: /data/reports type: DirectoryOrCreate name: staging-test-reports
- Add services and routing
#!/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 !!!"
Note: You can also use kong dashboad completion of the above shell script.