For a Kong Gateway node to be able to use the custom plugin, the custom plugin’s Lua sources must be installed on your host’s file system.
You can do this with LuaRocks, Docker, or manually.
Regardless of which method you are using to install your plugin’s sources, you must install it for each node in your Kong Gateway cluster.
The .rock
file is a self contained package that can be installed locally or from a remote server.
If the luarocks
utility is installed in your system, you can install the rock in your LuaRocks tree:
luarocks install {rock-filename}
The filename can be a local name, or any of the supported methods, for example http://myrepository.lan/rocks/my-plugin-0.1.0-1.all.rock
.
If the luarocks
utility is installed in your system, you can install the Lua sources in your LuaRocks tree:
cd {plugin-name}
luarocks make
This will install the Lua sources in kong/plugins/{plugin-name}
in your system’s LuaRocks tree, where all the Kong Gateway sources are already present.
If you are running Kong Gateway on Docker or Kubernetes, the plugin needs to be installed inside the Kong Gateway container.
Copy or mount the plugin’s source code into the container.
Note: Official Kong Gateway images are configured to run as the nobody
user.
When building a custom image, you must temporarily set the user to root
to copy files into the Kong Gateway image.
Here’s an example Dockerfile that shows how to mount your plugin in the Kong Gateway image:
FROM kong/kong-gateway:latest
# Ensure any patching steps are executed as root user
USER root
# Add custom plugin to the image
COPY example-plugin/kong/plugins/example-plugin /usr/local/share/lua/5.1/kong/plugins/example-plugin
ENV KONG_PLUGINS=bundled,example-plugin
# Ensure kong user is selected for image execution
USER kong
# Run kong
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000 8443 8001 8444
STOPSIGNAL SIGQUIT
HEALTHCHECK --interval=10s --timeout=10s --retries=10 CMD kong health
CMD ["kong", "docker-start"]
You can also include the following in your docker run
command:
-v "{custom_plugin_folder}:/tmp/custom_plugins/kong"
-e "KONG_LUA_PACKAGE_PATH=/tmp/custom_plugins/?.lua;;"
-e "KONG_PLUGINS=bundled,example-plugin"
To avoid polluting the LuaRocks tree, you can point Kong Gateway to the directory containing the plugins.
You can do that with the lua_package_path
property of your Kong Gateway configuration.
Those properties contain a semicolon-separated list of directories in which to search for Lua sources:
lua_package_path = /{path-to-plugin-location}/?.lua;;
In this example:
-
/{path-to-plugin-location}
is the path to the directory containing the extracted archive.
Replace it with the location of the kong
directory from the archive.
-
?
is a placeholder that will be replaced by kong.plugins.{plugin-name}
when Kong Gateway tries to load your plugin.
Do not change it.
-
;;
a placeholder for the default Lua path. Do not change it.
For example, if the plugin something
is located on the file system and the handler file is located at /usr/local/custom/kong/plugins/something/handler.lua
, the path setup is:
lua_package_path = /usr/local/custom/?.lua;;
If you want to install two or more custom plugins this way, you can set the variable to something like:
lua_package_path = /path/to/plugin1/?.lua;/path/to/plugin2/?.lua;;
In this example:
-
;
is the separator between directories.
-
;;
still means refers to the default Lua path.
You can also set this property via its environment variable equivalent: KONG_LUA_PACKAGE_PATH
.