kong中go插件开发配置并打包成docker

kong中go插件开发配置并打包成docker

本方法适用于kong 2.7版本以上

kong服务器安装时的配置

go语言开发插件

kong提供了go语言的插件开发工具包go-pdk

示例go-hello插件:

/*
	A "hello world" plugin in Go,
	which reads a request header and sets a response header.
*/

package main

import (
	"fmt"
	"log"

	"github.com/Kong/go-pdk"
	"github.com/Kong/go-pdk/server"
)

func main() {
	server.StartServer(New, Version, Priority)
}

var Version = "0.2"
var Priority = 1

type Config struct {
	Message string
}

func New() interface{} {
	return &Config{}
}

func (conf Config) Access(kong *pdk.PDK) {
	host, err := kong.Request.GetHeader("host")
	if err != nil {
		log.Printf("Error reading 'host' header: %s", err.Error())
	}

	message := conf.Message
	if message == "" {
		message = "hello"
	}
	kong.Response.SetHeader("x-hello-from-go", fmt.Sprintf("Go says %s to %s", message, host))
}

kong.conf.default配置修改

pluginserver_names = go-hello

pluginserver_go_socket = /usr/local/kong/go-hello.sock
pluginserver_go_start_cmd = /usr/local/bin/go-hello -kong-prefix /usr/local/kong
pluginserver_go_query_cmd = /usr/local/bin/go-hello -dump -kong-prefix /usr/local/kong

添加插件名到 plugins配置中

plugins = bundled, go-hello

修改constant.lua文件

文件位置 /usr/local/share/lua/5.1/kong/constants.lua

在local plugins中添加go-hello插件名。

local plugins = {
    "go-hello",
    }

到此kong已经安装完成了。修改连接postgre后,就可以执行kong migrations

docker打包集成

Dockerfile


FROM docker.io/library/golang:1.18.3-alpine3.15 AS builder


RUN mkdir -p /tmp/go/src/ \
    && go env -w GOPROXY=https://goproxy.cn,direct

COPY plugins-code/ /tmp/go/src
COPY kong.conf.default /tmp/go/src/kong.conf.default

RUN cd /tmp/go/src/go-hello  \
    && go mod tidy \
    && go build -o go-hello \
    && ls /tmp/go/src/go-hello

FROM kong:2.8.1-alpine
USER root
COPY --from=builder  /tmp/go/src/go-hello/go-hello /usr/local/bin/
COPY --from=builder  /tmp/go/src/kong.conf.default /etc/kong/kong.conf.default
RUN sed -i 's|local plugins = {|local plugins = {\n  "go-hello",|g' /usr/local/share/lua/5.1/kong/constants.lua

deploy

# 删除容器
docker rm -f `docker ps -a|grep kong:2.8.0-personalise|awk '{print $1}'`
#删除镜像
docker rmi -f `docker images --filter=reference='kong:2.8.0-personalise' | awk '{print $3}'`

# 进入容器
#  docker exec -it `docker ps |grep kong:2.8.0-personalise |awk '{print $1}'`  /bin/sh

chmod +x kong/install.sh

docker build -f Dockerfile . -t kong:2.8.0-personalise

run

docker run --rm  \
    -p 8000:8000 \
    -p 8001:8001 \
    -p 8443:8443 \
    -p 8444:8444 \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_PORT=5432" \
    -e "KONG_PG_HOST={IP}" \
    -e "KONG_PG_DATABASE=kong" \
    -e "KONG_PG_USER=postgres" \
    -e "KONG_PG_PASSWORD=kong" \
    -e "KONG_PLUGINS=bundled, go-hello" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
    kong:2.8.0-personalise kong migrations bootstrap -c /etc/kong/kong.conf.default

docker run -d --name kong --restart=no \
    -p 8000:8000 \
    -p 8001:8001 \
    -p 8443:8443 \
    -p 8444:8444 \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_PORT=5432" \
    -e "KONG_PG_HOST={IP}" \
    -e "KONG_PG_DATABASE=kong" \
    -e "KONG_PG_USER=postgres" \
    -e "KONG_PG_PASSWORD=kong" \
    -e "KONG_PLUGINS=bundled, go-hello" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
    kong:2.8.0-personalise kong start -c /etc/kong/kong.conf.default

参考资料

https://github.com/Kong/go-plugins

https://docs.konghq.com/gateway/latest/install-and-run/centos/

https://docs.konghq.com/gateway/latest/reference/external-plugins/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北方的孤夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值