前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >k8s运维记 - 如何让部署到k8s的kong网关托管自定义静态资源?

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

作者头像
justmine
发布2019-10-16 10:03:12
1.8K0
发布2019-10-16 10:03:12
举报
文章被收录于专栏:容器化容器化

目的

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

代码语言:javascript
复制
<html>
<head></head>
<body><h1>测试报告</h1></body>
</html>

一、编写nginx自定义模板

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

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

  1. 添加托管静态资源配置
代码语言:javascript
复制
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
代码语言:javascript
复制
[root@justmine ~]# kubectl -n [kong所在的命名空间] create configmap kong-nginx-custom-config --from-file=/data/kong/custom_nginx.template

二、配置kong

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

三、测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目的
  • 一、编写nginx自定义模板
  • 二、配置kong
  • 三、测试
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档