搜索

第 3 章 在容器中运行红帽 Keycloak 构建

download PDF

本章论述了如何优化并运行红帽构建的 Keycloak 容器镜像,以提供运行容器的最佳体验。

警告

本章仅适用于构建您在 OpenShift 环境中运行的镜像。只有 OpenShift 环境支持此镜像。如果您在其他 Kubernetes 发行版中运行它,则不支持它。

3.1. 创建自定义和优化的容器镜像

Keycloak 容器镜像的默认红帽构建已准备好配置和优化。

为了更好地启动红帽构建的 Keycloak 容器,请在容器构建过程中运行 构建步骤 构建镜像。此步骤将在容器镜像的每个后续开始阶段节省时间。

3.1.1. 编写您优化的红帽构建的 Keycloak Dockerfile

以下 Dockerfile 会创建预配置的 Keycloak 镜像构建,启用健康和指标端点,启用令牌交换功能,并使用 PostgreSQL 数据库。

Dockerfile:

FROM registry.redhat.io/rhbk/keycloak-rhel9:24 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

WORKDIR /opt/keycloak
# for demonstration purposes only, please make sure to use proper certificates in production instead
RUN keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 -dname "CN=server" -alias server -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -keystore conf/server.keystore
RUN /opt/keycloak/bin/kc.sh build

FROM registry.redhat.io/rhbk/keycloak-rhel9:24
COPY --from=builder /opt/keycloak/ /opt/keycloak/

# change these values to point to a running postgres instance
ENV KC_DB=postgres
ENV KC_DB_URL=<DBURL>
ENV KC_DB_USERNAME=<DBUSERNAME>
ENV KC_DB_PASSWORD=<DBPASSWORD>
ENV KC_HOSTNAME=localhost
ENTRYPOINT ["/opt/keycloak/bin/kc.sh"]

构建过程包含多个阶段:

  • 运行 build 命令,以设置服务器构建选项来创建优化的镜像。
  • 构建阶段生成的文件复制到新镜像中。
  • 在最终镜像中,设置了主机名和数据库的额外配置选项,以便在运行容器时不需要再次设置它们。
  • 在入口点中,kc.sh 启用对所有分发子命令的访问。

要安装自定义提供程序,您只需要定义一个步骤,将 JAR 文件包含到 /opt/keycloak/providers 目录中。此步骤必须放在 RUNs build 命令的行之前,如下所示:

# A example build step that downloads a JAR file from a URL and adds it to the providers directory
FROM registry.redhat.io/rhbk/keycloak-rhel9:24 as builder

...

# Add the provider JAR file to the providers directory
ADD --chown=keycloak:keycloak --chmod=644 <MY_PROVIDER_JAR_URL> /opt/keycloak/providers/myprovider.jar

...

# Context: RUN the build command
RUN /opt/keycloak/bin/kc.sh build

3.1.2. 安装额外的 RPM 软件包

如果您尝试在一个阶段的 FROM registry.redhat.io/rhbk/keycloak-rhel9 中安装新软件,您会注意到 microdnfdnf,甚至没有安装 rpm。另外,很少的软件包可用,仅适用于 bash shell,并运行红帽构建的 Keycloak 本身。这是因为安全强化措施,这降低了红帽构建的 Keycloak 容器的攻击面。

首先,请考虑您的用例是否可以以不同的方式实施,因此请避免将新 RPM 安装到最终容器中:

  • Dockerfile 中的 RUN curl 指令可以替换为 ADD,因为该指令原生支持远程 URL。
  • 一些常见的 CLI 工具可以被 Linux 文件系统递归使用替代。例如,ip addr show tap0 变为 cat /sys/class/net/tap0/address
  • 需要 RPM 的任务可以移到镜像构建的前阶段,以及复制的结果。

下面是一个示例。在以前的构建阶段运行 update-ca-trust,然后复制结果:

FROM registry.access.redhat.com/ubi9 AS ubi-micro-build
COPY mycertificate.crt /etc/pki/ca-trust/source/anchors/mycertificate.crt
RUN update-ca-trust

FROM registry.redhat.io/rhbk/keycloak-rhel9
COPY --from=ubi-micro-build /etc/pki /etc/pki

如果需要,可以安装新的 RPM,遵循由 ubi-micro 建立的双阶段模式:

FROM registry.access.redhat.com/ubi9 AS ubi-micro-build
RUN mkdir -p /mnt/rootfs
RUN dnf install --installroot /mnt/rootfs <package names go here> --releasever 9 --setopt install_weak_deps=false --nodocs -y && \
    dnf --installroot /mnt/rootfs clean all && \
    rpm --root /mnt/rootfs -e --nodeps setup

FROM registry.redhat.io/rhbk/keycloak-rhel9
COPY --from=ubi-micro-build /mnt/rootfs /

这种方法使用 chroot /mnt/rootfs,以便只安装您指定的软件包及其依赖项,因此无需猜测在没有猜测的情况下,可以轻松地复制到第二个阶段。

警告

有些软件包具有大量依赖项。通过安装新的 RPM,您可能会意外增加容器的攻击面。仔细检查安装的软件包列表。

3.1.3. 构建容器镜像

要构建实际容器镜像,请从包含 Dockerfile 的目录运行以下命令:

podman build . -t mykeycloak

3.1.4. 启动优化的红帽 Keycloak 容器镜像构建

要启动镜像,请运行:

podman run --name mykeycloak -p 8443:8443 \
        -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=change_me \
        mykeycloak \
        start --optimized

红帽构建的 Keycloak 以生产模式启动,仅使用安全 HTTPS 通信,并可在 https://localhost:8443 中提供。

健康检查端点位于 https://localhost:8443/healthhttps://localhost:8443/health/readyhttps://localhost:8443/health/live

打开 https://localhost:8443/metrics 会导致页面,包含您的监控解决方案可以使用的操作指标。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.