# Apply custom plugin via KongIngress

**URL:** <https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698>\
**Category:** Questions\
**Tags:** service-mesh, kubernetes\
**Created:** [January 25, 2019, 8:19pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698 "2019-01-25T20:19:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mountaineerduke](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mountaineerduke](https://discuss.konghq.com/u/mountaineerduke)\
**Post date:** [January 25, 2019, 8:19pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/1 "2019-01-25T20:19:11Z")

</div>

Is there a way to setup a custom plugin for kong (I’m trying to use the nokia-oidc plugin, [GitHub - nokia/kong-oidc: OIDC plugin for Kong](https://github.com/nokia/kong-oidc)) when an ingress is created for the Kong-Ingress-Controller? Currently my ingress yml file looks like this:

```
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echochamber-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /echochamber
        backend:
          serviceName: echochamber-service
          servicePort: 8080
```

I have a script that will set up the pluing on the service using curl commands like so:

```
curl -s -X POST --url "http://$KONG_HOSTNAME:$KONG_PORT/services/$SERVICE_NAME/plugins" \
 -d "name=oidc" \
 -d "config.discovery=$WELL_KNOWN_URL" \
 -d "config.introspection_endpoint=$INTROSPECTION_URL" \
 -d "config.client_id=$CLIENT_NAME" \
 -d "config.client_secret=$CLIENT_SECRET" \
 -d "config.realm=$REALM" &> /dev/null
```

After a little while though the plugin is removed from the service that is created in kong. Is there a way to setup the same plugin using something like this so it stays applied:

```
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: oidc
config:
  ???
```

---

<div class="post-metadata">

**Author:** ![traines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/traines/32/158_2.png) [@traines](https://discuss.konghq.com/u/traines)\
**Post date:** [January 30, 2019, 7:44pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/2 "2019-01-30T19:44:07Z")

</div>

Custom plugins can be added to the ingress controller via volume mounts and environment variable configuration. Note that these need to be added to both the [kong Deployment](https://github.com/Kong/kubernetes-ingress-controller/blob/0.3.0/deploy/single/all-in-one-postgres.yaml#L637-L641) and the ingress-kong Deployment, as the ingress controller uses a split deployment with separate Kong nodes for the admin API and proxy.

You’ll need to first create a [configMap](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/) to hold your plugin source with something like `kubectl create configmap custom-plugins --from-file=/path/to/kong-plugin-hello/ --namespace kong` .

After, the volume configuration in the Kong deployment will look something like:

```auto
        image: kong:1.0.2-centos
        volumeMounts:
          - name: custom-plugin-vol
            mountPath: /kong-plugins/kong/plugins/
        env:
          - name: KONG_LUA_PACKAGE_PATH
            value: "/kong-plugins/?.lua;;"
          - name: KONG_CUSTOM_PLUGINS
            value: hello-world

      volumes:
        - name: custom-plugin-vol
          configMap:
            name: custom-plugins
            items:
              - key: handler.lua
                path: hello-world/handler.lua
              - key: schema.lua
                path: hello-world/schema.lua

```

With that deployed, you can add Ingress objects normally, with annotations to load the plugin and specify configuration as you would with any standard plugin.

Note that there are some caveats to updating ConfigMaps that you’ll need to take into account pending Kubernetes feature requests:

> <https://github.com/kubernetes/kubernetes/issues/22368>

  

> <https://github.com/kubernetes/kubernetes/issues/24957>

You can alternately build a custom Docker image, starting from one of the standard Kong images and copying the plugin files over. The environment variables should still be added, but the volumeMounts/configMaps can be removed.

---

<div class="post-metadata">

**Author:** ![Wangchong\_Zhou](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/wangchong_zhou/32/899_2.png) [@Wangchong\_Zhou](https://discuss.konghq.com/u/Wangchong_Zhou)\
**Post date:** [January 30, 2019, 7:54pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/3 "2019-01-30T19:54:45Z")

</div>

Also a side node that when you applying a plugin resource in Ingress Controller, you will need to specify the `plugin` field as well. The `metadata.name` field is used to attach the plugin resource to a service/route/consumer resource, and it doesn’t equal to `name` in Kong Admin API. The `plugin` field specifies the actual plugin you want to create, in your case it will be `oidc`.

So the CRUD will be:

```
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: oidc-for-echochamber
config:
  ???
plugin: oidc

```

Then patch your service/route you want to apply that plugin to:

```
kubectl patch svc echochamber-service \
  -p '{"metadata":{"annotations":{"plugins.konghq.com": "oidc-for-echochamber\n"}}}'
```

---

<div class="post-metadata">

**Author:** ![mountaineerduke](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mountaineerduke](https://discuss.konghq.com/u/mountaineerduke)\
**Post date:** [January 30, 2019, 8:01pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/4 "2019-01-30T20:01:05Z")

</div>

I had already went down the path of building a custom docker image with the oidc plugin built in.  
`
FROM kong:0.14.1
RUN luarocks install kong-oidc
RUN sh -c “echo ‘plugins = bundled,oidc’ >> /etc/kong/kong.conf”
`

It looks like the main thing I was missing is the environment variables in the yaml file. Would I need the KONG\_LUA\_PACKAGE\_PATH value or is KONG\_CUSTOM\_PLUGINS sufficient?

---

<div class="post-metadata">

**Author:** ![Muhammad\_Yazid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/muhammad_yazid/32/1184_2.png) [@Muhammad\_Yazid](https://discuss.konghq.com/u/Muhammad_Yazid)\
**Post date:** [August 19, 2019, 6:43am UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/5 "2019-08-19T06:43:00Z")

</div>

Any update on your progress?

---

<div class="post-metadata">

**Author:** ![Michael\_Chatterton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/michael_chatterton/32/1636_2.png) [@Michael\_Chatterton](https://discuss.konghq.com/u/Michael_Chatterton)\
**Post date:** [February 6, 2020, 7:32pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/6 "2020-02-06T19:32:28Z")

</div>

> [@traines](#):
>
> plugin files over. The environment variables should still be a

Any update on your progress? I’m trying to do exactly this and still not quite there. I have a custom docker image but when trying to start the image it is looking for DB config and I want to run in db-less mode.

Any help will be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![mountaineerduke](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mountaineerduke](https://discuss.konghq.com/u/mountaineerduke)\
**Post date:** [February 6, 2020, 8:05pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/7 "2020-02-06T20:05:34Z")

</div>

sadly no, we ended switching to istio for ingress control so this issue became moot.

---

<div class="post-metadata">

**Author:** ![Michael\_Chatterton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/michael_chatterton/32/1636_2.png) [@Michael\_Chatterton](https://discuss.konghq.com/u/Michael_Chatterton)\
**Post date:** [February 6, 2020, 8:07pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/8 "2020-02-06T20:07:11Z")

</div>

Thanks for reply, I guess I’ll have to look for alternatives 🙂

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [February 6, 2020, 10:29pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/9 "2020-02-06T22:29:57Z")

</div>

You can easily setup Custom plugins with Kong Ingress Controller.  
Here is a handy guide to setup plugins via COnfigMaps:

> <https://github.com/Kong/kubernetes-ingress-controller/blob/master/docs/guides/setting-up-custom-plugins.md>

Kong and the Ingress Controller do not differentiate between a plugin that is bundled with Kong and a plugin that is installed by a user. They are exactly the same. The only challenge is to ensure that you have to install the plugin in the file-system and configure `plugins` property.

---

<div class="post-metadata">

**Author:** ![jaiswar.vipin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jaiswar.vipin/32/1873_2.png) [@jaiswar.vipin](https://discuss.konghq.com/u/jaiswar.vipin)\
**Post date:** [May 19, 2020, 7:24pm UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/10 "2020-05-19T19:24:17Z")

</div>

I have created the custom plugin “[https://medium.com/swlh/creating-and-installing-custom-lua-plugins-in-kong-ce7fd64d33bf](https://medium.com/swlh/creating-and-installing-custom-lua-plugins-in-kong-ce7fd64d33bf)” using this reference, and we are successfully able to build and deploy the plugin in KONG 2.0 using docker image.

Now we are trying to deploy the same plugin in the KONG as ingress controller (we are using AKS) but i am facing issue

what we had did till now

- Created the configMap and successfully able to describe it by following above steps

\*Now we are trying to deploy the same plugin but facing if we follow the above steps and deployment file looks like

# deployment.yaml

apiVersion: apps/v1  
kind: Deployment  
metadata:  
name: kong-ingress-custom-plugin  
namespace: konga-test  
spec:  
selector:  
matchLabels:  
app: konga-test  
template:  
metadata:  
name: kong-ingress-custom-plugin  
labels:  
app: kong-ingress-custom-plugin  
spec:  
containers:  
- name: proxy  
env:  
- name: KONG\_PLUGINS  
value: ‘bundled,cutom-auth’  
- name: KONG\_LUA\_PACKAGE\_PATH  
value: /opt/?.lua;;  
volumeMounts:  
- name: kong-plugin-cutom-auth  
mountPath: /opt/kong/plugins/cutom-auth  
volumes:  
- defaultMode: 755  
- name: kong-plugin-cutom-auth  
configMap:  
name: kong-plugin-cutom-auth

**we are executing the same below command to apply the change**

> kubectl apply -f miniorange-auth-deployment.yaml --validate=false

**and error we got**

> The Deployment “kong-plugin-miniorange-auth” is invalid: spec.template.spec.containers[0].image: Required value

Here i don’t understand, if we created the ConfigMap for same plugin then why its asking for docker image.

Kindly assist.

Thanks & Regards  
Jaiswar Vipin Kumar R.

---

<div class="post-metadata">

**Author:** ![jaiswar.vipin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jaiswar.vipin/32/1873_2.png) [@jaiswar.vipin](https://discuss.konghq.com/u/jaiswar.vipin)\
**Post date:** [May 21, 2020, 11:36am UTC](https://discuss.konghq.com/t/apply-custom-plugin-via-kongingress/2698/11 "2020-05-21T11:36:32Z")

</div>

After **updating** the YAML finally custom pluing get install but after that got next issue. Now same POD, Deployment and Replicaset throwing “ **CrashloopOff** ”. After checking the log of the pod we got following error.

> init\_by\_lua error: /usr/local/share/lua/5.1/kong/init.lua:389: [PostgreSQL error] failed to retrieve PostgreSQL server\_version\_num: connection refused stack traceback:

**However Updated YAML is**

> apiVersion: apps/v1  
> kind: Deployment  
> metadata:  
> name: kong-ingress-custom-plugin  
> namespace: konga-test  
> spec:  
> selector:  
> matchLabels:  
> app: konga-test  
> replicas: 1  
> template:  
> metadata:  
> name: kong-ingress-custom-plugin  
> labels:  
> app: kong-ingress-custom-plugin  
> spec:  
> containers:  
> name: proxy  
> **image: ‘kong:2.0’**  
> volumeMounts:  
> - name: kong-plugin-custom-auth  
> mountPath: /opt/kong/plugins/kong-plugin-custom-auth  
> - env:  
> - name: KONG\_PG\_DATABASE  
> value: kong  
> - name: KONG\_PG\_HOST  
> value: postgres  
> - name: KONG\_PG\_PORT  
> value: ‘5432’  
> - name: KONG\_PG\_PASSWORD  
> value: kong  
> - name: KONG\_PG\_USER  
> value: kong  
> - name: KONG\_LOG\_LEVEL  
> value: info  
> - name: KONG\_PLUGINS  
> value: ‘bundled,kong-plugin-custom-auth’  
> - name: KONG\_LUA\_PACKAGE\_PATH  
> value: /etc/?./opt/?.lua;;  
> volumes:  
> - defaultMode: 755  
> - name: kong-plugin-custom-auth  
> configMap:  
> name: kong-plugin-custom-auth

Thanks & Regards  
Jaiswar Vipin Kumar R.
