美文网首页
使用docker环境开发kong插件流程

使用docker环境开发kong插件流程

作者: 酥苏落叶 | 来源:发表于2019-11-01 11:38 被阅读0次

使用docker环境开发kong插件流程

说明:本教程适合使用docker环境开发kong插件,本教程使用的是 postgres数据库进行开发,使用本教程前请提前安装docker

1.安装postgres数据库

1)使用docker命令拉取postgres数据库镜像

docker pull  postgres:9.6

2)使用docker命令拉取kong镜像

docker pull  kong:latest

3)使用docker运行postgres数据库实例,指定实例名为kong-database,用户名为kong,DB名为kong

docker run -d --name kong-database -p 5432:5432 -e "POSTGRES_USER=kong" -e "POSTGRES_PASSWORD=kong" -e "POSTGRES_DB=kong" postgres:9.6 

4)使用docker对postgres数据库进行初始化

docker run --rm --link kong-database:kong-database -e "KONG_DATABASE=postgres"  -e "KONG_PG_PASSWORD=kong" -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong kong migrations bootstrap

5)使用docker运行kong实例

docker run -d --name kong --link kong-database:kong-database -e "KONG_DATABASE=postgres" -e "KONG_PG_PASSWORD=kong"  -e "KONG_PG_HOST=kong-database" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" -e "KONG_PROXY_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" -p 8000:8000 -p 8443:8443 -p 8001:8001  -p 8444:8444 kong  

6)进入kong实例sh

docker exec -it kong /bin/sh

7)安装必须工具

//编译工具
apk add gcc
//安装库
apk add libc-dev
//lua支持
apk add lua
//lua包管理器
apk add luarocks
//安装lua库  
luarocks install lua-cjson

8)安装kong插件教程

//创建插件目录
mkdir -p /root/test-authentication/kong/plugins/test-authentication
//进入/root/test-authentication/kong/plugins/test-authentication目录
cd /root/test-authentication/kong/plugins/test-authentication
//将项目文件拷入目录下

//进入/root/test-authentication目录

//创建kong-plugin-test-authentication-0.1.0-1.rockspec(kong-plugin-<项目名>-<版本号>.rockspec)

//在/root/test-authentication执行
luarocks make
luarocks pack <项目名> <版本号>
luarocks install kong-plugin-<项目名>-<版本号>.all.rock

kong-plugin-test-authentication-0.1.0-1.rockspec文件格式

package = "kong-plugin-test-authentication"  -- TODO: rename, must match the info in the filename of this rockspec!
                                    -- as a convention; stick to the prefix: `kong-plugin-`
version = "0.1.0-1"               -- TODO: renumber, must match the info in the filename of this rockspec!
-- The version '0.1.0' is the source code version, the trailing '1' is the version of this rockspec.
-- whenever the source version changes, the rockspec should be reset to 1. The rockspec version is only
-- updated (incremented) when this file changes, but the source remains the same.

-- TODO: This is the name to set in the Kong configuration `plugins` setting.
-- Here we extract it from the package name.
local pluginName = package:match("^kong%-plugin%-(.+)$")  -- "test-authentication"
supported_platforms = {"linux", "macosx"}
source = {
    url = "..."
}

description = { 
    summary = "Kong is a scalable and customizable API Management Layer built on top of Nginx.",
    homepage = "http://getkong.org",
    license = "Apache 2.0"
}

dependencies = {
}

build = {
    type = "builtin",
    modules = {
        -- TODO: add any additional files that the plugin consists of
        ["kong.plugins."..pluginName..".handler"] = "kong/plugins/"..pluginName.."/handler.lua",
        ["kong.plugins."..pluginName..".schema"] = "kong/plugins/"..pluginName.."/schema.lua",
        ["kong.plugins."..pluginName..".redis"] = "kong/plugins/"..pluginName.."/redis.lua",
        ["kong.plugins."..pluginName..".response"] = "kong/plugins/"..pluginName.."/response.lua",
    }
}

安装完成后可以执行命令查看是否安装成功

luarocks list

接下来配置kong的配置文件

//进入/etc/kong目录
cd /etc/kong
//拷贝默认文件到默认配置位置
cp kong.conf.default kong.conf
//修改配置文件
vi kong.conf
//找到plugins = bundled配置项,按I键进入编辑模式,在其后面加上新增的插件名,并用逗号分隔
plugins = bundled,test-authentication
//按Esc键退出编辑模式,输入:wq保存并退出
//使用下面命令使kong不用重启加载插件
kong prepare
kong reload

9)常用指令

//查看指定实例日志
docker logs -f <实例名>
//从容器到主机-拷贝文件
docker cp kong:/root/test-authentication/kong/plugins/test-authentication/handler.lua E:/handler.lua
//从主机到容器-拷贝文件
docker cp E:\project\Center\PrivilegeCenter\kong-test-plugin\test-authentication\kong\plugins\test-authentication\handler.lua kong:/root/test-authentication/kong/plugins/test-authentication/handler.lua
//注册kong的service
curl -i -X POST --url http://localhost:8001/services/ --data 'name=tdt-service' --data 'url=http://www.baidu.com/'
//设置路由
curl -i -X POST --url http://localhost:8001/services/tdt-service/routes  --data 'paths[]=/tdt'
//绑定插件
curl -i -X POST --url http://localhost:8001/services/tdt-service/plugins/ --data 'name=test-authentication'
//绑定插件并设置参数
curl -i -X POST --url http://localhost:8001/services/tdt-service/plugins/ --data 'name=test-authentication' -d "config.redis.host=r-2zejucm1vlozmv0mekpd.redis.rds.aliyuncs.com" -d "config.redis.port=6379" -d "config.redis.password=wBPhHiUScYO6
68TV" -d "config.redis.database=9"

10)相关链接
https://docs.konghq.com/1.2.x/plugin-development/distribution/

https://github.com/Kong/kong-plugin

https://github.com/luarocks/luarocks/wiki/Creating-a-rock

https://github.com/Kong/kong-plugin/blob/master/kong-plugin-myplugin-0.1.0-1.rockspec

相关文章

网友评论

      本文标题:使用docker环境开发kong插件流程

      本文链接:https://www.haomeiwen.com/subject/sxsfbctx.html