简体   繁体   English

Keycloak不使用postgres数据库,尝试连接h2数据库

[英]Keycloak does not use postgres database and tries to connect h2 database

I am trying to configure Keycloak to use postgres using docker-compose.我正在尝试将 Keycloak 配置为使用 docker-compose 使用 postgres。

Docker compose file for reference: Docker 撰写文件供参考:

version: "3.9"

services:

  keycloak-postgres:
    image: postgres:latest

    restart: unless-stopped
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: ${POSTGRESQL_DB}
      POSTGRES_USER: ${POSTGRESQL_USER}
      POSTGRES_PASSWORD: ${POSTGRESQL_PASS}

    volumes:
      - postgres_data:/var/lib/postgresql/data
      
  keycloak:
    depends_on:
      - keycloak-postgres
    image: quay.io/keycloak/keycloak
    container_name: keycloak
    ports:
      - 8030:8080
    environment:
      KC_DB: postgres
      KC_DB_URL_HOST: keycloak-postgres
      KC_DB_URL_DATABASE: ${POSTGRESQL_DB}
      KC_DB_USERNAME: ${POSTGRESQL_USER}
      KC_DB_PASSWORD: ${POSTGRESQL_PASS}
      KEYCLOAK_ADMIN: ${KEYCLOAK_ADMIN}
      KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD}
      KC_HOSTNAME: ${KEYCLOAK_HOSTNAME}
      KC_PROXY: edge
      KC_HTTP_ENABLED: true
    restart: unless-stopped
    command:
      - start --optimized
      
volumes:
  postgres_data:
    driver: local

I have found that if I run start without the optimized flag, keycloak starts without any issues, but also does not use the postgres database - as there are no tables or anything created by Keycloak when I connect to the DB.我发现,如果我在没有优化标志的情况下运行 start,keycloak 将毫无问题地启动,但也不使用 postgres 数据库——因为当我连接到数据库时,Keycloak 没有创建任何表或任何内容。

When I run with the optimized flag, I get the following error:当我使用优化标志运行时,出现以下错误:

URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:postgresql://keycloak-postgres:5432/keycloak" [90046-214]

From what I can make out the postgres connection string which Keycloak has generated is correct.据我所知,Keycloak 生成的 postgres 连接字符串是正确的。 However it is trying to connect to a h2 database, which is clearly incorrect.但是它试图连接到 h2 数据库,这显然是不正确的。

I have looked through all the configuration options and just can't make out why:我查看了所有配置选项,但无法弄清楚原因:

a) Keycloak isn't storing any data in postgres in start mode. a) Keycloak 在启动模式下没有在 postgres 中存储任何数据。

b) Keycloak is trying to access a H2 database in --optimized mode. b) Keycloak 正在尝试以--optimized 模式访问H2 数据库。

Update更新

Following advice from sonOfRa and to try and simplify the problem I have now tried the following:根据 sonOfRa 的建议并尝试简化问题,我现在尝试了以下操作:

  1. Run postgres as a seperate docker.将 postgres 作为单独的 docker 运行。
  2. Created the below Dockerfile as per the documentation (have also tried with sonOfRa's cut down Dockerfile):根据文档创建以下 Dockerfile(也尝试使用 sonOfRa 的缩减 Dockerfile):
FROM quay.io/keycloak/keycloak:latest as builder

# Enable health and metrics support
ENV KC_HEALTH_ENABLED=true
ENV KC_METRICS_ENABLED=true

# Configure a database vendor
ENV KC_DB=postgres

RUN /opt/keycloak/bin/kc.sh build

FROM quay.io/keycloak/keycloak:latest
COPY --from=builder /opt/keycloak/ /opt/keycloak/

ENV KC_DB_URL_HOST=192.168.1.25
ENV KC_DB_USERNAME=keycloak
ENV KC_DB_PASSWORD=keycloak_db_password
ENV KC_HOSTNAME=localhost

ENTRYPOINT ["/opt/keycloak/bin/kc.sh"]
  1. Run the following command to build the new Dockerfile:运行以下命令来构建新的 Dockerfile:

docker build. -t mykeycloak

  1. Run the following command to start Keycloak:运行以下命令启动Keycloak:
docker run --name mykeycloak \
 -p 8030:8080 \ 
-e KEYCLOAK_ADMIN=admin \ 
-e KEYCLOAK_ADMIN_PASSWORD=change_me \
-e KC_HOSTNAME=auth.url.com \ 
-e KC_PROXY=edge \
-e KC_HTTP_ENABLED=true \
mykeycloak start

Output from console:来自控制台的 Output:

2023-01-11 14:06:19,961 INFO  [org.keycloak.quarkus.runtime.hostname.DefaultHostnameProvider] (main) Hostname settings: Base URL: <unset>, Hostname: auth.url.com, Strict HTTPS: true, Path: <request>, Strict BackChannel: false, Admin URL: <unset>, Admin: <request>, Port: -1, Proxied: true
2023-01-11 14:06:25,844 WARN  [io.quarkus.agroal.runtime.DataSources] (main) Datasource <default> enables XA but transaction recovery is not enabled. Please enable transaction recovery by setting quarkus.transaction-manager.enable-recovery=true, otherwise data may be lost if the application is terminated abruptly
2023-01-11 14:06:28,797 INFO  [org.infinispan.server.core.transport.EPollAvailable] (keycloak-cache-init) ISPN005028: Native Epoll transport not available, using NIO instead: java.lang.UnsatisfiedLinkError: could not load a native library: netty_transport_native_epoll_aarch_64
2023-01-11 14:06:29,311 WARN  [org.infinispan.PERSISTENCE] (keycloak-cache-init) ISPN000554: jboss-marshalling is deprecated and planned for removal
2023-01-11 14:06:29,436 WARN  [org.infinispan.CONFIG] (keycloak-cache-init) ISPN000569: Unable to persist Infinispan internal caches as no global state enabled
2023-01-11 14:06:29,541 INFO  [org.keycloak.broker.provider.AbstractIdentityProviderMapper] (main) Registering class org.keycloak.broker.provider.mappersync.ConfigSyncEventListener
2023-01-11 14:06:29,581 INFO  [org.infinispan.CONTAINER] (keycloak-cache-init) ISPN000556: Starting user marshaller 'org.infinispan.jboss.marshalling.core.JBossUserMarshaller'
2023-01-11 14:06:30,440 INFO  [org.infinispan.CONTAINER] (keycloak-cache-init) ISPN000128: Infinispan version: Infinispan 'Triskaidekaphobia' 13.0.10.Final
2023-01-11 14:06:30,819 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000078: Starting JGroups channel `ISPN`
2023-01-11 14:06:30,820 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000088: Unable to use any JGroups configuration mechanisms provided in properties {}. Using default JGroups configuration!
2023-01-11 14:06:31,143 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
2023-01-11 14:06:31,144 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the receive buffer of socket MulticastSocket was set to 20.00MB, but the OS only allocated 212.99KB
2023-01-11 14:06:31,146 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
2023-01-11 14:06:31,147 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the receive buffer of socket MulticastSocket was set to 25.00MB, but the OS only allocated 212.99KB
2023-01-11 14:06:33,179 INFO  [org.jgroups.protocols.pbcast.GMS] (keycloak-cache-init) cb354516ab9d-30183: no members discovered after 2009 ms: creating cluster as coordinator
2023-01-11 14:06:33,213 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000094: Received new cluster view for channel ISPN: [cb354516ab9d-30183|0] (1) [cb354516ab9d-30183]
2023-01-11 14:06:33,228 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000079: Channel `ISPN` local address is `cb354516ab9d-30183`, physical addresses are `[172.17.0.2:52593]`
2023-01-11 14:06:35,021 INFO  [org.keycloak.connections.infinispan.DefaultInfinispanConnectionProviderFactory] (main) Node name: cb354516ab9d-30183, Site name: null
2023-01-11 14:06:41,372 INFO  [org.keycloak.quarkus.runtime.storage.legacy.liquibase.QuarkusJpaUpdaterProvider] (main) Initializing database schema. Using changelog META-INF/jpa-changelog-master.xml
2023-01-11 14:06:53,286 INFO  [org.keycloak.services] (main) KC-SERVICES0050: Initializing master realm
2023-01-11 14:07:00,559 INFO  [io.quarkus] (main) Keycloak 20.0.2 on JVM (powered by Quarkus 2.13.3.Final) started in 45.755s. Listening on: http://0.0.0.0:8080
2023-01-11 14:07:00,561 INFO  [io.quarkus] (main) Profile prod activated.
2023-01-11 14:07:00,562 INFO  [io.quarkus] (main) Installed features: [agroal, cdi, hibernate-orm, jdbc-h2, jdbc-mariadb, jdbc-mssql, jdbc-mysql, jdbc-oracle, jdbc-postgresql, keycloak, logging-gelf, narayana-jta, reactive-routes, resteasy, resteasy-jackson, smallrye-context-propagation, smallrye-health, smallrye-metrics, vault, vertx]
2023-01-11 14:07:02,212 INFO  [org.keycloak.services] (main) KC-SERVICES0009: Added user 'admin' to realm 'master'

Unforuntely the result is the same.不幸的是,结果是一样的。

I can access keycloak from the set URL and login using the admin user created on run.我可以从集合 URL 访问 keycloak,并使用在运行时创建的管理员用户登录。 Everything seemingly works on the UI, except it does not store any data in the postgres database.一切似乎都在 UI 上运行,除了它不在 postgres 数据库中存储任何数据。

This is due to your use of the --optimized parameter.这是由于您使用了 --optimized 参数。 If you use it, it is assumed that you have already ran "build", which you did not do.如果您使用它,则假定您已经运行了“构建”,而您并没有这样做。 It is recommended to create your own docker image which uses the upstream docker image as a base.建议创建您自己的docker 镜像,它使用上游 docker 镜像作为基础。 This is described in the documentation here .这在此处的文档中进行了描述。

Essentially, you need to run the build command with --db=postgres (or the KC_DB=postgres environment variable), in order to tell Quarkus to build an optimized image that will later use postgres.本质上,您需要使用 --db=postgres(或 KC_DB=postgres 环境变量)运行构建命令,以便告诉 Quarkus 构建稍后将使用 postgres 的优化图像。 That image can then be started with --optimized and it will correctly use postgres instead of H2.然后可以使用 --optimized 启动该图像,它将正确使用 postgres 而不是 H2。

Step 1 is to create a Dockerfile (not a docker-compose.yml!)第 1 步是创建一个 Dockerfile(不是 docker-compose.yml!)

FROM quay.io/keycloak/keycloak

# Configure a database vendor
ENV KC_DB=postgres

WORKDIR /opt/keycloak

RUN /opt/keycloak/bin/kc.sh build

ENTRYPOINT ["/opt/keycloak/bin/kc.sh"]

You can also include additional things at this point, like custom providers, but this is the minimal data that you need in order to make it work.此时您还可以包含其他内容,例如自定义提供程序,但这是使其正常工作所需的最少数据。

Now you have 2 options: You can build this image with docker build and push it to your own docker registry with docker push , or you can use it directly from your docker-compose.yaml .现在您有 2 个选项:您可以使用docker build构建此映像,并使用docker push push 将其推送到您自己的 docker 注册表,或者您可以直接从您的docker-compose.yaml使用它。 If you build and push, replace the image: quay.io/keycloak/keycloak line with image: your.registry/wherever/you/pushed .如果您构建并推送,请将image: quay.io/keycloak/keycloak行替换为 image image: your.registry/wherever/you/pushed If you want to use it directly in your compose-file, you can remove the image: line completely, and replace it with如果您想直接在您的撰写文件中使用它,您可以完全删除image:行,并将其替换为

build: .

When doing this, you must ensure that the Dockerfile is in the same directory as the docker-compose.yaml执行此操作时,必须确保Dockerfiledocker-compose.yaml在同一目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM