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.
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: falseFields that map to Keycloak options
| Resource field | Keycloak option |
|---|---|
spec.db.vendor, host, port, database | db, db-url-host, db-url-port, db-url-database |
spec.db.usernameSecret, passwordSecret | db-username, db-password, read from a Secret |
spec.http.httpEnabled | http-enabled |
spec.http.tlsSecret | The certificate Keycloak serves HTTPS with |
spec.hostname.hostname | hostname |
spec.hostname.admin | hostname-admin |
spec.proxy.headers | proxy-headers |
spec.additionalOptions | Any 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
Keycloakresource with name, namespace, instances, image, database, HTTPS, hostname, optional admin hostname, proxy headers, the Ingress switch, memory and an optionalmetrics-enabledoption. - It writes a
Secretwithusernameandpasswordkeys 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
- Install the Keycloak Operator and its CRDs in the cluster, following the operator’s own instructions for your version.
- Create or choose a PostgreSQL database reachable from the namespace. Note its service name and database name.
- Fill in the resource name, namespace and number of instances.
- 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.
- Enter the public hostname as a full URL, and an admin hostname if the console has its own address.
- Choose whether HTTPS ends in front of Keycloak or Keycloak serves it from a TLS Secret.
- Copy the Secret, put the real password in, and apply it. Then apply the Keycloak resource.
- Watch the pods with
kubectl get pods -n <namespace>until they are ready.
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 -dHTTPS: 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
additionalOptionsas plain text. - Read the
-initial-adminSecret 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.