k8s运维记 - 如何让部署到k8s的kong网关托管自定义静态资源?

元气小坏坏 提交于 2019-12-01 09:40:22

目的

使用kong作为目录/data/reports的静态资源服务器,为了测试,已于目录/data/reports下创建文件report.html,如下:

<html>
<head></head>
<body><h1>测试报告</h1></body>
</html>

一、编写nginx自定义模板

  1. 获取kong自定义的nginx配置
[root@justmine ~]# kubectl -n [kong所在的命名空间] exec -it [kong pod完全限定名] sh 
[root@justmine ~]# cat /usr/local/kong/nginx.conf

nginx.conf内容复制出来,新的nginx模板将在此内容上进行修改,避免修改后无法启动kong容器。

  1. 添加托管静态资源配置
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;
    }
}
  1. 将上面的文件保存到k8s
[root@justmine ~]# kubectl -n [kong所在的命名空间] create configmap kong-nginx-custom-config --from-file=/data/kong/custom_nginx.template

二、配置kong

  1. 挂载自定义配置
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
  1. 挂载静态资源目录
- mountPath: /kong/nginx/www
          name: staging-test-reports
---中间省略---
- hostPath:
          path: /data/reports
          type: DirectoryOrCreate
        name: staging-test-reports
  1. 添加服务和路由
#!/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脚本的功能。

三、测试

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!