DEV Community

Cover image for How to setup a Keycloak Gatekeeper to secure the services in your Kubernetes cluster
Techworld with Nana
Techworld with Nana

Posted on • Updated on

How to setup a Keycloak Gatekeeper to secure the services in your Kubernetes cluster

1. Prerequisite for configuring gatekeeper for a specific application:

  • create a client, called my-app in Keycloak. In the creation step select access-type: confidential

  • once created you will see a credentials tab for the newly created client with the secret. You will need that for the gatekeeper.yaml config file

  • create Realm role for that application my-app-role

  • create a group my-group and add the role my-app-role in role-mappings of the group

  • go to your user and add it to the my-group. This user will be authorized to access the secured application, once we configure it

2. Create a gatekeeper configuration file, gatekeeper.yaml, as a secret

client-id: my-app

client-secret: my-app-secret-from-credentials


skip-upstream-tls-verify: false

skip-openid-provider-tls-verify: true

encryption-key: random-secret-value-of-16-or-32-characters


secure-cookie: false

enable-logging: true

enable-json-logging: true

enable-default-deny: true

enable-refresh-tokens: true

enable-session-cookies: true

debug: true

ingress.enabled: true


  - uri: /favicon

  white-listed: true
Enter fullscreen mode Exit fullscreen mode

Values to be replaced and set in the above config file:

  • client-secret set from the client credentials tab

  • encryption-key random hash value 16 or 32 characters

  • discovery-url keycloak endpoint

Some of the important values explained:

  • discovery-url is keycloak's realm url that the my-app client resides in

  • skip-openid-provider-tls-verify ignores Keycloak's self-signed certificate warning for gatekeeper client request

  • enable-default-deny - all resources are by default secured unless explicitly allowed in resources

  • listen - port where the gatekeeper will start and listen on

  • redirection-url - where the successful login will be redirected to

  • upstream-url - where gatekeeper will forward the request after successful authorization

  • resources configures which resources can be accessed (white-listed) without authentication.

The configuration values are needed by the gatekeeper to communicate with Keycloak. So with the endpoint, we tell gatekeeper where to find the Keycloak server to talk to, with the client-id and client-secret gatekeeper can authenticate itself with Keycloak to make requests to it.

kubectl create secret generic gatekeeper --from-file= ./gatekeeper.yaml -n my-namespace
Enter fullscreen mode Exit fullscreen mode

Now that you have set up all the necessary configuration, you need to deploy gatekeeper container inside the pod where the application you want to secure is running. SO gatekeeper will be a side-car container to your actual app, acting as a... well, gatekeeper to all incoming requests.

So to deploy both containers inside one pod, you need to configure it like this:

apiVersion: apps/v1

kind: Deployment


  name: my-app






      - image: keycloak/keycloak-gatekeeper:7.0.0

        name: gatekeeper


        - containerPort: 3000


        - --config=/etc/secrets/gatekeeper.yaml

        - --redirection-url=https://my-app-server-url

        - --upstream-url=

        - --resources=uri=/*|roles=my-app-role


        - name: gatekeeper-secrets

          mountPath: /etc/secrets

      - image: my-app-image

        name: my-app


        - containerPort: 8080


      - name: gatekeeper-secrets


          secretName: gatekeeper
Enter fullscreen mode Exit fullscreen mode

So with the above configuration, a pod will be created with the application and gatekeeper containers and the gatekeeper.yaml file that we created above will be mounted inside the gatekeeper container.

As you see in the deployment config, gatekeeper container has args configured.

  • redirection-url is where the successful login will be redirected to. This points to the url of the my-app, which will also land with the gatekeeper

  • upstream-url - after the redirect-url is called for the authenticated user, gatekeeper will allow access to my-app, by forwarding the request to it on

Why because both my-app and gatekeeper run in the same pod, they can communicate via localhost.

So, now we need to configure the service for my-app to forward the incoming requests NOT to my-app, but to the gatekeeper container. As you see from above my-app and gatekeeper are running on different ports (8080 and 3000), so we just need to tell the service to forward the request to gatekeeper's port 3000.

apiVersion: v1

kind: Service


  name: my-service



    service: my-service

  type: ClusterIP


  - port: 32111

    targetPort: 3000
Enter fullscreen mode Exit fullscreen mode

► Get 30% off - with this code: UDEMY_NANA_OCT2020: Udemy course here
Kubernetes 101: Compact and easy-to-read ebook bundle 🚀
It's a handy way to quickly look something up or refresh your knowledge at work and use it as your cheatsheet 😎

Like, share and follow me 😍 for more content:

Discussion (2)

sharpedavid profile image

I'm no expert, but I'm wondering if there are some typos in the last code block. What is "my-service"? It's not referenced elsewhere in the article. Same for port "32111", what's running there? Anyhow, I'm very unfamiliar with Kubernetes so perhaps this is all correct.

Thanks for sharing this. I'm trying to use Gatekeeper and this cleared up a few things.

brunobck profile image

There is no typo, "my-service" is a k8s object that abstracts a set of pods. Port "32111" is where this k8s objects is receiving traffic. Every application which was calling the pod directly, now call the service on that port, and the service routes the traffic for one of its pods, like a load-balance. As you said, those are k8s specific matters, and I suggest you reading