Skip to content

Instantly share code, notes, and snippets.

@dasniko
Last active December 24, 2024 18:02
Show Gist options
  • Save dasniko/3a57913047af3ca1b6b0a83b294dc1a1 to your computer and use it in GitHub Desktop.
Save dasniko/3a57913047af3ca1b6b0a83b294dc1a1 to your computer and use it in GitHub Desktop.

Revisions

  1. dasniko revised this gist Feb 12, 2024. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions _keycloak-cluster-config.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,7 @@

    This is a short and simple example on how to build a proper Keycloak cluster, using `DNS_PING` as discovery protocol and an NGINX server as reverse proxy.

    To get it working properly, just enable Docker Swarm mode with `docker swarm init` and just run it as it were a regular Docker Compose deployment.
    So, just `docker compose up` is enough, don't deploy a swarm `stack`! (This would cause trouble and extra complexity with networking)
    Afterwards, you can disable Swarm again with `docker swarm leave -f`, if needed.
    If you prefer to use JDBC_PING, see @xgp's example gist here: https://gist.github.com/xgp/768eea11f92806b9c83f95902f7f8f80

    ---

  2. dasniko revised this gist Oct 9, 2023. 2 changed files with 6 additions and 7 deletions.
    4 changes: 2 additions & 2 deletions docker-compose-cluster.yml
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,8 @@ services:
    KC_DB_USERNAME: keycloak
    KC_DB_PASSWORD: passw0rd
    KC_PROXY: edge
    KC_HOSTNAME_STRICT: "false"
    KC_SPI_EVENTS_LISTENER_JBOSS_LOGGING_SUCCESS_LEVEL: info
    KC_HOSTNAME: localhost
    KC_HOSTNAME_PORT: "8000"
    KEYCLOAK_ADMIN: admin
    KEYCLOAK_ADMIN_PASSWORD: admin
    deploy:
    9 changes: 4 additions & 5 deletions nginx.con
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,20 @@
    upstream backend {
    ip_hash;
    server keycloak-1:8080 fail_timeout=2s;
    server keycloak-2:8080 fail_timeout=2s;
    }

    server {
    listen 8000;
    server_name localhost;
    access_log off;

    location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Proto $scheme;


    proxy_pass http://backend;
    proxy_connect_timeout 2s;

  3. dasniko revised this gist May 13, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions docker-compose-cluster.yml
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ services:
    KC_DB_USERNAME: keycloak
    KC_DB_PASSWORD: passw0rd
    KC_PROXY: edge
    KC_HOSTNAME_STRICT: "false"
    KC_SPI_EVENTS_LISTENER_JBOSS_LOGGING_SUCCESS_LEVEL: info
    KEYCLOAK_ADMIN: admin
    KEYCLOAK_ADMIN_PASSWORD: admin
  4. dasniko revised this gist Mar 12, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions docker-compose-cluster.yml
    Original file line number Diff line number Diff line change
    @@ -5,23 +5,22 @@ services:
    image: postgres:latest
    environment:
    POSTGRES_USER: keycloak
    POSTGRES_PASSWORD: password
    POSTGRES_PASSWORD: passw0rd
    volumes:
    - pg-data:/var/lib/postgresql/data

    keycloak:
    image: quay.io/keycloak/keycloak:latest
    command: start --auto-build -Djgroups.dns.query=keycloak
    command: start-dev -Djgroups.dns.query=keycloak
    environment:
    KC_CACHE: ispn
    KC_CACHE_STACK: kubernetes
    KC_DB: postgres
    KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
    KC_DB_USERNAME: keycloak
    KC_DB_PASSWORD: password
    KC_HOSTNAME: localhost:8000
    KC_HOSTNAME_STRICT: "false"
    KC_DB_PASSWORD: passw0rd
    KC_PROXY: edge
    KC_SPI_EVENTS_LISTENER_JBOSS_LOGGING_SUCCESS_LEVEL: info
    KEYCLOAK_ADMIN: admin
    KEYCLOAK_ADMIN_PASSWORD: admin
    deploy:
    @@ -37,3 +36,4 @@ services:

    volumes:
    pg-data:
    name: keycloak-demo-cluster-data
  5. dasniko revised this gist Mar 6, 2022. 2 changed files with 1 addition and 3 deletions.
    2 changes: 0 additions & 2 deletions docker-compose-cluster.yml
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,6 @@ services:
    environment:
    POSTGRES_USER: keycloak
    POSTGRES_PASSWORD: password
    ports:
    - "5432:5432"
    volumes:
    - pg-data:/var/lib/postgresql/data

    2 changes: 1 addition & 1 deletion nginx.con
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    upstream backend {
    server keycloak-1:8080 fail_timeout=2s;
    server keycloak-22:8080 fail_timeout=2s;
    server keycloak-2:8080 fail_timeout=2s;
    }

    server {
  6. dasniko revised this gist Mar 6, 2022. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions _keycloak-cluster-config.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    # Keycloak Cluster Configuration (How to)

    This is a short and simple example on how to build a proper Keycloak cluster, using `JDBC_PING` as discovery protocol and an NGINX server as reverse proxy.
    This is a short and simple example on how to build a proper Keycloak cluster, using `DNS_PING` as discovery protocol and an NGINX server as reverse proxy.

    Please see also my video about Keycloak Clustering: http://www.youtube.com/watch?v=P96VQkBBNxU
    To get it working properly, just enable Docker Swarm mode with `docker swarm init` and just run it as it were a regular Docker Compose deployment.
    So, just `docker compose up` is enough, don't deploy a swarm `stack`! (This would cause trouble and extra complexity with networking)
    Afterwards, you can disable Swarm again with `docker swarm leave -f`, if needed.

    ---

    Please see also my video about Keycloak Clustering: http://www.youtube.com/watch?v=P96VQkBBNxU
    NOTE: The video covers `JDBC_PING` protocol and uses the **legacy** Keycloak Wildfly distribution!

    [![](http://img.youtube.com/vi/P96VQkBBNxU/maxresdefault.jpg)](http://www.youtube.com/watch?v=P96VQkBBNxU "")
  7. dasniko created this gist Mar 4, 2022.
    7 changes: 7 additions & 0 deletions _keycloak-cluster-config.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Keycloak Cluster Configuration (How to)

    This is a short and simple example on how to build a proper Keycloak cluster, using `JDBC_PING` as discovery protocol and an NGINX server as reverse proxy.

    Please see also my video about Keycloak Clustering: http://www.youtube.com/watch?v=P96VQkBBNxU

    [![](http://img.youtube.com/vi/P96VQkBBNxU/maxresdefault.jpg)](http://www.youtube.com/watch?v=P96VQkBBNxU "")
    41 changes: 41 additions & 0 deletions docker-compose-cluster.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    version: '3.8'

    services:
    postgres:
    image: postgres:latest
    environment:
    POSTGRES_USER: keycloak
    POSTGRES_PASSWORD: password
    ports:
    - "5432:5432"
    volumes:
    - pg-data:/var/lib/postgresql/data

    keycloak:
    image: quay.io/keycloak/keycloak:latest
    command: start --auto-build -Djgroups.dns.query=keycloak
    environment:
    KC_CACHE: ispn
    KC_CACHE_STACK: kubernetes
    KC_DB: postgres
    KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
    KC_DB_USERNAME: keycloak
    KC_DB_PASSWORD: password
    KC_HOSTNAME: localhost:8000
    KC_HOSTNAME_STRICT: "false"
    KC_PROXY: edge
    KEYCLOAK_ADMIN: admin
    KEYCLOAK_ADMIN_PASSWORD: admin
    deploy:
    replicas: 2
    endpoint_mode: dnsrr

    lb:
    image: nginx:alpine
    volumes:
    - ./nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
    - "8000:8000"

    volumes:
    pg-data:
    26 changes: 26 additions & 0 deletions nginx.con
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    upstream backend {
    server keycloak-1:8080 fail_timeout=2s;
    server keycloak-22:8080 fail_timeout=2s;
    }

    server {
    listen 8000;
    server_name localhost;

    location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass http://backend;
    proxy_connect_timeout 2s;

    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    }
    }