Skip to content

Keycloak Operator custom resource generator

Generate a Keycloak custom resource for the Keycloak Operator on Kubernetes, with the database, hostname, HTTPS and proxy settings, plus the database Secret it reads.

Runs in your browser. Nothing you paste is sent to us.

Settings

26.8.1 is the version we run. Edit it to your version; it should match the operator’s.

The public address, as a full URL. A made-up one is filled in.

Resources

keycloak.yaml
apiVersion: k8s.keycloak.org/v2alpha1
kind: Keycloak
metadata:
  name: keycloak
  namespace: keycloak
spec:
  instances: 2
  image: quay.io/keycloak/keycloak:26.8.1
  db:
    vendor: postgres
    host: postgres-rw
    port: 5432
    database: keycloak
    usernameSecret:
      name: keycloak-db
      key: username
    passwordSecret:
      name: keycloak-db
      key: password
  http:
    httpEnabled: true
  hostname:
    hostname: 'https://auth.example.com'
  proxy:
    headers: xforwarded
  ingress:
    enabled: false
  additionalOptions:
    - name: metrics-enabled
      value: 'true'
  resources:
    requests:
      memory: 2Gi
    limits:
      memory: 2Gi
Database secret (fill in the password)
apiVersion: v1
kind: Secret
metadata:
  name: keycloak-db
  namespace: keycloak
type: Opaque
stringData:
  username: keycloak
  password: change-me
Before you apply
  • Install the Keycloak Operator and its CRDs in the cluster first.
  • Bring your own PostgreSQL: the operator does not create a database. The host above is its Kubernetes service name.
  • The operator sets up the health probes on the management port (9000) itself, and creates a temporary admin in the Secret keycloak-initial-admin.
  • With HTTPS ended in front, the gateway must overwrite X-Forwarded headers, since Keycloak trusts them.

How to use it

  1. Enter the name, namespace and number of instances.
  2. Set the image to your Keycloak version and the public hostname.
  3. Say where HTTPS ends and where your PostgreSQL runs.
  4. Copy both resources and apply them with kubectl apply -f.

What to know

The Keycloak Operator turns one Keycloak resource into a StatefulSet, services and an Ingress, unless you turn the Ingress off. It also sets up the health probes and a temporary admin Secret.

The operator does not run a database for you. Point db at your own PostgreSQL, for example one managed by a PostgreSQL operator, and keep the credentials in a Secret.

Hostname takes a full URL. With HTTPS ended at an ingress or gateway, turn on httpEnabled and proxy headers, and make sure the gateway overwrites the X-Forwarded headers.

What the Keycloak Operator does

The Keycloak Operator is the Kubernetes way to run Keycloak. You describe the server you want in one custom resource of kind Keycloak, and the operator creates and maintains what it takes to run it: a StatefulSet for the pods, services, the configuration passed to each pod, and an Ingress unless you turn that off.

The value is consistency. Instead of hand-writing a Deployment, environment variables, probes and services, and keeping them in step across upgrades, you change a few fields in one resource. The operator turns those fields into the right Keycloak options and rolls the pods.

This generator writes that resource, plus the Secret it reads the database credentials from, from a short form.

Why it matters in production

Keycloak holds the keys to every application that signs in through it. In production you want more than one instance, health probes that Kubernetes acts on, a database that survives pod restarts, and a hostname that never changes by accident.

The operator gives you the first two. The resource sets the number of instances, and the operator configures startup, readiness and liveness probes on the management port. The last two remain your job: you bring the PostgreSQL database and you set the hostname. Getting those fields right in the resource is what this tool is for.

The resource in Keycloak 26

The current resource uses apiVersion: k8s.keycloak.org/v2alpha1 and kind: Keycloak. A minimal production resource behind an ingress or gateway that ends TLS looks like this. The hostname is a made-up sample.

keycloak.yaml
apiVersion: k8s.keycloak.org/v2alpha1
kind: Keycloak
metadata:
  name: keycloak
  namespace: keycloak
spec:
  instances: 2
  image: quay.io/keycloak/keycloak:26.8.1
  db:
    vendor: postgres
    host: postgres-rw
    port: 5432
    database: keycloak
    usernameSecret:
      name: keycloak-db
      key: username
    passwordSecret:
      name: keycloak-db
      key: password
  http:
    httpEnabled: true
  hostname:
    hostname: 'https://auth.example.com'
  proxy:
    headers: xforwarded
  ingress:
    enabled: false

Fields that map to Keycloak options

Resource fieldKeycloak option
spec.db.vendor, host, port, databasedb, db-url-host, db-url-port, db-url-database
spec.db.usernameSecret, passwordSecretdb-username, db-password, read from a Secret
spec.http.httpEnabledhttp-enabled
spec.http.tlsSecretThe certificate Keycloak serves HTTPS with
spec.hostname.hostnamehostname
spec.hostname.adminhostname-admin
spec.proxy.headersproxy-headers
spec.additionalOptionsAny option without a field of its own

Options without a field

Anything the resource has no field for goes in additionalOptions as a name and value, using the option name without the KC_ prefix, such as metrics-enabled. For a secret value, reference a Secret instead of writing the value in the resource.

What this tool does and does not do

  • It writes the Keycloak resource with name, namespace, instances, image, database, HTTPS, hostname, optional admin hostname, proxy headers, the Ingress switch, memory and an optional metrics-enabled option.
  • It writes a Secret with username and password keys for the database, with a placeholder password for you to replace.
  • It checks that names are valid Kubernetes names and that hostnames are full URLs.
  • It does not install the operator or its CRDs, create a database, or write Ingress or Gateway resources for you.
  • It does not contact your cluster. Everything is built in your browser.

How to use it

  1. Install the Keycloak Operator and its CRDs in the cluster, following the operator’s own instructions for your version.
  2. Create or choose a PostgreSQL database reachable from the namespace. Note its service name and database name.
  3. Fill in the resource name, namespace and number of instances.
  4. Set the image to the Keycloak version you want. The tool fills in 26.8.1, the version we run; keep it in line with the operator version.
  5. Enter the public hostname as a full URL, and an admin hostname if the console has its own address.
  6. Choose whether HTTPS ends in front of Keycloak or Keycloak serves it from a TLS Secret.
  7. Copy the Secret, put the real password in, and apply it. Then apply the Keycloak resource.
  8. Watch the pods with kubectl get pods -n <namespace> until they are ready.
Apply and read the first admin
kubectl apply -f keycloak-db-secret.yaml
kubectl apply -f keycloak.yaml
kubectl get keycloaks -n keycloak
kubectl get secret keycloak-initial-admin -n keycloak -o jsonpath='{.data.username}' | base64 -d

HTTPS: at the gateway or in Keycloak

There are two sound setups. Pick one and keep the other settings consistent with it.

TLS ends at an ingress or gateway

Set httpEnabled: true and proxy.headers: xforwarded. The ingress or gateway must overwrite the X-Forwarded headers on every request, since Keycloak trusts them.

Keycloak serves HTTPS

Put a certificate and key in a TLS Secret and name it in spec.http.tlsSecret. Use this when traffic must stay encrypted up to the pod, or when a load balancer passes TLS through untouched.

Where it pays off

Moving from a VM or Docker Compose to Kubernetes

Most of your existing settings carry over. The database options, hostname and proxy headers become fields in the resource, and the rest go into additionalOptions. The configuration converter helps read options out of a Compose file or keycloak.conf. Point the new resource at a copy of the database first and test sign-in before you switch traffic.

Running more than one instance

Set instances to 2 or more. Keycloak nodes share state through the database and their caches, and the operator runs them as a StatefulSet. Plan a PodDisruptionBudget and spread the pods across nodes so one node failure does not take every instance down.

Upgrading Keycloak

Change the image tag, or upgrade the operator if you let it choose the image. Back up the database first. Keycloak migrates the schema when it starts on a new version, so test the upgrade against a copy of production data.

Several environments

Keep one resource per environment, with the same structure and different hostnames, Secrets and instance counts. Reviewing a diff between them is easier than comparing hand-written Deployments.

Common errors and how to fix them

The resource is accepted but no pods appear

Read the resource’s status with kubectl describe keycloak <name>. The operator reports problems in the status conditions. Also check the operator’s own logs, and that the resource has either httpEnabled: true or a tlsSecret.

Pods restart and never become ready

Usually the database. Check the host is the service name in the same namespace, or a full DNS name for another one, and that the Secret keys match username and password. The pod logs name the cause.

Wrong URLs in tokens or redirects

Set hostname.hostname to the public URL, including https, and set proxy.headers when TLS ends in front. The hostname settings helper explains each option.

Image and operator versions disagree

The operator is built for a matching Keycloak version. Running a very different image can fail in ways that are hard to read. Keep them in step.

Security notes

  • Keep database credentials only in Secrets, never in the resource or in additionalOptions as plain text.
  • Read the -initial-admin Secret once, create a permanent administrator, then remove the temporary one.
  • Do not expose the management port 9000 outside the cluster. Scrape metrics from inside.
  • Use a NetworkPolicy so only the ingress or gateway reaches Keycloak’s HTTP port, and only Keycloak reaches the database.
  • If you let the operator create an Ingress, check which paths it exposes, and keep the admin console off the public address when you can.

Best practices

  • Pin the image by version, and by digest if your process allows it.
  • Run at least two instances for anything users depend on.
  • Set memory requests and limits from what you observe under load, not from guesses.
  • Turn on metrics and alert on readiness, not only on pods running.
  • Keep the resources in Git and change them through review.

If you would rather not run this yourself, we run upstream Keycloak on a dedicated cluster per customer. The migration page describes how realms move over.

Questions

Frequently asked questions

What is the apiVersion of the Keycloak custom resource?

The current Keycloak Operator uses `k8s.keycloak.org/v2alpha1` with `kind: Keycloak`. Check the operator documentation for your version if you run an older one.

Does the Keycloak Operator create a database?

No. You bring PostgreSQL or another supported database and point `spec.db` at it. Many teams run PostgreSQL with a PostgreSQL operator in the same cluster.

Where do I find the admin password created by the operator?

In a Secret named after the resource with `-initial-admin` at the end, such as `keycloak-initial-admin`. It is a temporary admin, so create a permanent one and remove it.

How do I set a Keycloak option the resource has no field for?

Add it under `spec.additionalOptions` as a name and value, with the option name in its plain form such as `metrics-enabled`. For a secret, reference a Secret key instead of a value.

Should TLS end at the ingress or at Keycloak?

Both work. Ending TLS at the ingress or gateway is common: set `httpEnabled: true` and `proxy.headers`. Use `tlsSecret` when traffic must be encrypted all the way to the pod.

How many Keycloak instances should I run?

At least two for anything users depend on, so one pod can restart without an outage. Add more based on load, and spread them across nodes and zones.

How do I upgrade Keycloak with the operator?

Back up the database, then change the image or upgrade the operator to the matching version. Keycloak migrates the schema on start, so test the upgrade on a copy of the data first.

Why does the operator create an Ingress I did not ask for?

The resource’s Ingress is on unless you turn it off with `ingress.enabled: false`. Turn it off when you route traffic with your own Ingress or Gateway API resources.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.