How to deploy Keycloak HA cluster in Docker

Viewed 8306

I'm following http://blog.keycloak.org/2015/04/running-keycloak-cluster-with-docker.html and when trying to run the first keycloak instance:

docker run --name postgres -e POSTGRES_DATABASE=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password -e POSTGRES_ROOT_PASSWORD=password -d postgres

docker run -p 8080:8080 --name keycloak --link postgres:postgres -e POSTGRES_DATABASE=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password -d jboss/keycloak-ha-postgres

I am getting the error:

javax.resource.ResourceException: IJ031083: Wrong driver class [org.postgresql.Driver] for this connection URL [jdbc:postgresql://postgres:tcp://172.17.0.2:5432/keycloak]

Has anyone got experience using this Keycloak Docker image? Or is there an easier way to deploy a Keycloak cluster to Docker?

2 Answers

an example for deploy keycloak in HA mode with postgres

version: '3'

volumes:
  postgres_data:
    driver: local

services:
  postgres:
    image: 'postgres:alpine'
    volumes:
      - ./postgres:/var/lib/postgresql/data
    restart: 'always'
    # ports:
    #   - 5432:5432
    environment:
      POSTGRES_USER: keycloak
      POSTGRES_PASSWORD: password
      POSTGRES_DB: keycloak
      POSTGRES_HOST: postgres

  traefik:
    image: library/traefik:alpine
    container_name: traefik
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command: >
      --logLevel=ERROR
      --api.dashboard
      --docker
      --entrypoints="Name:http Address::80"
      --defaultentrypoints="http"
    ports:
      - 80:80
      - 3000:8080

  keycloak:
    image: jboss/keycloak
    environment:
      DB_VENDOR: postgres
      DB_ADDR: postgres
      DB_PORT: 5432
      DB_DATABASE: keycloak
      DB_USER: keycloak
      DB_PASSWORD: password
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: Pa55w0rd
      # KEYCLOAK_LOGLEVEL: DEBUG
      JGROUPS_DISCOVERY_PROTOCOL: JDBC_PING
      JGROUPS_DISCOVERY_PROPERTIES: datasource_jndi_name=java:jboss/datasources/KeycloakDS,info_writer_sleep_time=500,initialize_sql="CREATE TABLE IF NOT EXISTS JGROUPSPING ( own_addr varchar(200) NOT NULL, cluster_name varchar(200) NOT NULL, created timestamp default current_timestamp, ping_data BYTEA, constraint PK_JGROUPSPING PRIMARY KEY (own_addr, cluster_name))"
    depends_on:
      - postgres
    labels:
      traefik.enable: true
      traefik.port: 8080
      traefik.protocol: http
      traefik.frontend.rule: Host:localhost
      traefik.frontend.passHostHeader: true
      # traefik.backend.loadbalancer.stickiness: true

https://gist.github.com/ERRECabrera/a4fb1ebdba300521b46587881b66aaf4

You should try using this more updated docker-compose file provided by the people behind the image wich might contain updated var names and versions. I ran it and it created the containers correctly. Just download the file and run docker-compose up and you'll have the stack running.

In the example you mentioned they just start another docker container, which in docker compose would only mean adding a new entry. Checkout this gist: https://gist.github.com/pacuna/e7427d8fef752992ff1df944223ad0ab

Now, that's not the ideal way of running a cluster of docker containers, you may want to checkout docker swarm or Kubernetes if it's a serious project. The docker compose template would be easy to translate to Kubernetes files.

Related