# Custom plugin installation kubernetes

**URL:** <https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200>\
**Category:** General\
**Tags:** kubernetes, kong-gateway\
**Created:** [December 18, 2023, 3:37pm UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200 "2023-12-18T15:37:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cactus](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@cactus](https://discuss.konghq.com/u/cactus)\
**Post date:** [December 18, 2023, 3:37pm UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200/1 "2023-12-18T15:37:18Z")

</div>

Hi!

While adding a custom plugin to Kong in Kubernetes it fails to start with the next error:

```auto
2023/12/18 11:41:51 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/tools/utils.lua:786: error loading module 'kong.plugins.custom-rate-limit.handler':
93
/opt/kong/plugins/custom-rate-limit/handler.lua:2: module 'lunajson' not found:No LuaRocks module found for lunajson
92
	no field package.preload['lunajson']

```

As I understand, the default Kong image doesn’t have `lunajson` module and that’s why it complains about it.  
How can I add a missed module - updating the default Kong image?

Thank you.

---

<div class="post-metadata">

**Author:** ![JohnWilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/johnwilliams/32/4230_2.png) [@JohnWilliams](https://discuss.konghq.com/u/JohnWilliams)\
**Post date:** [December 20, 2023, 8:35am UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200/2 "2023-12-20T08:35:12Z")

</div>

@cactus You need to create a custom image with the dependencies added. This will increase the size of the image - if you find any other alternatives to bring back the size, please do respond here (I am looking for a solution)

```auto
FROM kong/kong-gateway:3.4
USER root
RUN apt-get update
RUN apt-get install -y lua5.1 luarocks
#RUN apk add gcc expat expat-dev expat-static musl-dev
RUN luarocks install lua-cjson 2.1.0.6-1 # add your dependency here
RUN luarocks install xml2lua 1.5-2
USER kong
WORKDIR /usr/local/share/lua/5.1/kong/plugins
COPY kong/plugins/ ./

```

---

<div class="post-metadata">

**Author:** ![cactus](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@cactus](https://discuss.konghq.com/u/cactus)\
**Post date:** [December 20, 2023, 5:28pm UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200/3 "2023-12-20T17:28:12Z")

</div>

Hey!

Thanks for the reply. We have fixed this problem by installing the plugin in the docker image:

```auto
FROM ubuntu:jammy

COPY kong.deb /tmp/kong.deb

RUN set -ex; \
   apt-get update \
   && apt-get install --yes /tmp/kong.deb wget unzip \
   && rm -rf /var/lib/apt/lists/* \
   && rm -rf /tmp/kong.deb \
   && chown kong:0 /usr/local/bin/kong \
   && chown -R kong:0 /usr/local/kong \
   && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/luajit \
   && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/lua \
   && ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/bin/nginx \
   && kong version \
   && wget https://url/custom-rate-limit-1.all.rock \
   && luarocks install custom-rate-limit-1.all.rock

```

In kubernetes kong-gateway config added next:

```auto
  env:
    plugins: "bundled,custom-rate-limit"

```

Also, a plugin was added to Kubernetes using `Kind: KongPlugin`, according to this [manual](https://docs.konghq.com/kubernetes-ingress-controller/3.0.x/get-started/rate-limiting/#create-a-rate-limiting-kongplugin).  
Plugin is available, but when we tried to use it(add annotation to service or route), we got the error:

```auto
proxy 2023/12/20 17:20:52 [debug] 1262#0: *311 [lua] init.lua:23: poll(): worker-events: emulate poll method │
│ proxy 2023/12/20 17:20:52 [error] 1262#0: *311 [lua] api_helpers.lua:540: handle_error(): /usr/local/share/lua/5.1/lapis/application.lua:439: /usr/local/share/lua/5.1/kong/db/schema/init.lua:1204: attempt to index a nil │
│ value │
│ proxy stack traceback: │
│ proxy /usr/local/share/lua/5.1/kong/db/schema/init.lua: in function 'run_entity_check' │
│ proxy /usr/local/share/lua/5.1/kong/db/schema/init.lua:1321: in function 'run_checks' │
│ proxy /usr/local/share/lua/5.1/kong/db/schema/init.lua:1355: in function 'run_entity_checks' │
│ proxy /usr/local/share/lua/5.1/kong/db/schema/init.lua:1986: in function 'validate_upsert' │
│ proxy /usr/local/share/lua/5.1/kong/db/dao/init.lua:596: in function 'check_upsert' │
│ proxy /usr/local/share/lua/5.1/kong/db/dao/init.lua:1211: in function 'upsert_entity' │
│ proxy /usr/local/share/lua/5.1/kong/api/endpoints.lua:499: in function 'fn' │
│ proxy /usr/local/share/lua/5.1/kong/api/api_helpers.lua:311: in function </usr/local/share/lua/5.1/kong/api/api_helpers.lua:289> │
│ proxy │
│ proxy stack traceback: │
│ proxy [C]: in function 'error' │
│ proxy /usr/local/share/lua/5.1/lapis/application.lua:439: in function 'handler' │
│ proxy /usr/local/share/lua/5.1/lapis/application.lua:185: in function 'resolve' │
│ proxy /usr/local/share/lua/5.1/lapis/application.lua:216: in function </usr/local/share/lua/5.1/lapis/application.lua:214> │
│ proxy [C]: in function 'xpcall' │
│ proxy /usr/local/share/lua/5.1/lapis/application.lua:214: in function 'dispatch' │
│ proxy /usr/local/share/lua/5.1/lapis/nginx.lua:231: in function 'serve' │
│ proxy /usr/local/share/lua/5.1/kong/init.lua:1626: in function 'admin_content' │
│ proxy content_by_lua(nginx-kong.conf:330):2: in main chunk, client: 10.15.64.85, server: kong_admin, request: "PUT /plugins/470bfd5f-6718-497b-a638-67da79a0ee2d HTTP/2.0", host: "10.15.67.7:8444" │
│ proxy 2023/12/20 17:20:52 [debug] 1262#0: *311 [lua] init.lua:23: poll(): worker-events: emulate poll method

```

This custom plugin was written for kong-2.1(also postgres was used as db for kong), we are trying to use it on kong-3.4 in kubernetes in db-less mode.

What is the best way to import plugins from old kong version into new one?

Thanks.

---

<div class="post-metadata">

**Author:** ![cactus](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@cactus](https://discuss.konghq.com/u/cactus)\
**Post date:** [December 21, 2023, 8:23am UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200/4 "2023-12-21T08:23:08Z")

</div>

Hey, @JohnWilliams  
We tried to add a plugin in this way:

> **[(un)Installing your plugin - v3.5.x | Kong Docs](https://docs.konghq.com/gateway/latest/plugin-development/distribution/#via-a-dockerfile-or-docker-run-install-and-load)**
>
> Documentation for Kong, the Cloud Connectivity Company for APIs and Microservices.

During Kong startup, we see that the plugin is loaded:

```auto
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: custom-rate-limit │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:242: loader_fn(): Loading custom plugin entity: 'custom-rate-limit.ratelimiting_metrics' │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: jwt │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:242: loader_fn(): Loading custom plugin entity: 'jwt.jwt_secrets' │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: acl   
..........                        

```

But after trying to annotate a route or service with this plugin got the same error above.

All the source code in the directory with plugin name

```auto
tree custom-rate-limit/
custom-rate-limit/
├── README.md
├── daos.lua
├── expiration.lua
├── handler.lua
├── policies
│ └── init.lua
├── schema.lua
└── custom-rate-limit-1.rockspec

1 directory, 7 files

```

---

<div class="post-metadata">

**Author:** ![JohnWilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/johnwilliams/32/4230_2.png) [@JohnWilliams](https://discuss.konghq.com/u/JohnWilliams)\
**Post date:** [December 21, 2023, 8:54am UTC](https://discuss.konghq.com/t/custom-plugin-installation-kubernetes/12200/5 "2023-12-21T08:54:48Z")

</div>

I am not familiar with the error.

Try posting this in [Kong Slack Channel](https://join.slack.com/t/kongcommunity/shared_invite/zt-1s4nb74vo-jLdMEk8MoTm~uMWYMMLPWg) - it might get you a quick answer.
