目的

使用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脚本的功能。

三、测试

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

  1. k8s运维之pod排错

    k8s运维之pod排错 K8S是一个开源的,用于管理云平台中多个主机上的容器化应用,Kubernetes的目标是让部署容器化变得简单并且高效 K8S的核心优势: 1,基于yaml文件实现容器的自动创建 ...

  2. 自动化运维工具Ansible详细部署 (转载)

    自动化运维工具Ansible详细部署 标签:ansible 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog. ...

  3. 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客

    自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客 自动化运维工具Ansible详细部署

  4. 运维自动化之系统部署 PXE(二)

    PXE介绍 Preboot Excution Environment 预启动执行环境 Intel公司研发 基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持 ...

  5. 自动化运维工具SaltStack详细部署【转】

    ==========================================================================================一.基础介绍==== ...

  6. k8s运维处理

    k8s运维处理 驱逐节点容器,进行docker,等重要组件的重启时,打驱逐标记 kubectl drain [option --node ip] 进行重启docker或kubelet等其他操作,操作完 ...

  7. nginx反向代理部署springboot项目报404无法加载静态资源

    问题:nginx反向代理部署springboot项目报404无法加载静态资源(css,js,jpg,png...) springboot默认启动端口为8080,如果需要通过域名(不加端口号)直接访问s ...

  8. 自动化运维工具Ansible详细部署

    本文来源:http://sofar.blog.51cto.com/353572/1579894/ 前言 一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方 ...

  9. 自动化运维工具Ansible的部署步骤详解

    本文来源于http://sofar.blog.51cto.com/353572/1579894,主要是看到这样一篇好文章,想留下来供各位同僚一起分享. 一.基础介绍 ================= ...

随机推荐

  1. Bytes类型

    Bytes类型 一.定义 bytes类型是指一堆字节的集合,在python中以b开头的字符串都是bytes类型. b'\xe5\xb0\x8f\xe7\x8c\xbf\xe5\x9c\x88' ''' ...

  2. Go组件学习——手写连接池并没有那么简单

    1.背景 前段时间在看gorm,发现gorm是复用database/sql的连接池. 于是翻了下database/sql的数据库连接池的代码实现,看完代码,好像也不是很复杂,但是总觉得理解不够深刻,于 ...

  3. 【Offer】[38] 【字符串的排列】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一个字符串,打印出该字符串中字符的所有排列.例如,输入字符串abc,则打印出由字符a.b.c所能排列出来的所有字符串abc.acb. ...

  4. 设置普通用户输入sudo,免密进入root账户

    满足给开发用户开权限,赋予sudo权限.又不让其输入密码的方式: 方式一: 开始系统内部的wheel用户组, 在/etc/suoers 中编辑配置文件如下: %wheel ALL=(ALL) NOPA ...

  5. 033 模块4-PyInstaller库的使用

    目录 一.PyInstaller库基本介绍 1.1 PyInstaller库概述 1.2 pip的使用 1.3 pip install pyinstaller (cmd命令行) 二.PyInstall ...

  6. sql 多行、一行 互转

    原始数据: 期望数据: IF OBJECT_ID('temp_20170701','u') IS NOT NULL DROP TABLE temp_20170701 CREATE TABLE temp ...

  7. 【深入浅出-JVM】(75):class 装载

    过程 装载 条件 主动使用 class时 创建一个类的实例 (new .反射.克隆.反序列化) 调用类的静态方法(invokestatic) 使用类或接口的静态字段(getstatic.putstat ...

  8. yolo进化史之yolov3

    yolov3的论文写的比较简略,不看yolov1,yolov2很难直接看懂. 建议先看v1,v2论文. yolov3主要做了几点改进 改进了特征提取部分的网络结构 多尺度预测 分类由softmax改为 ...

  9. Python网络爬虫实战(四)模拟登录

    对于一个网站的首页来说,它可能需要你进行登录,比如知乎,同一个URL下,你登录与未登录当然在右上角个人信息那里是不一样的. (登录过) (未登录) 那么你在用爬虫爬取的时候获得的页面究竟是哪个呢? 肯 ...

  10. 32 (OC)* keyChain的本质

    1:它是一个sqlite数据库,其保存的所有数据都是加密过的. 2:Keychain是加密规则(key)的集合.每个规则必须含有以下三个要素:认证算法.认证密钥(加密字符串).规则的时间. 3:key ...