keycloak高可用部署本身就比较复杂,在k8s 上高可用涉及到更多的知识面,好在默认官方的镜像中已经初步支持了standalone-ha.xml 这种配置文件的高可用,我们要做的只是把加载镜像的入口脚本呢/opt/jboss/tools/docker-docker-entrypoint.sh 这个脚本会探测jboss的启动参数,最后会加载其他*.sh 脚本, 这些“其他” shell脚本,会通过/opt/jboss/keycloak/bin/jboss-cli.sh 加载系列*.cli 文件来更改 standalone-ha.xml 文件,这些更改包括:配置infinispan缓存,增加对各种数据库支持,增加支持高可用的探测方式(比如:dns_ping),支持keycloak 自动重连功能等。这些配置文件是通过变量形式写入standalone-ha.xml文件中,所有在k8s 中可以通过env 命令覆盖,十分方便。
我们只需要使用官方镜像,然后增加keycloak对postgresql的支持即可。
FROM hub.jundax.com/pub/keycloak:v15.0.2 #官方镜像
#下面两条命令添加keycloak对postgresql数据库支持的驱动,其他数据库类似
RUN mkdir -p /opt/jboss/keycloak/modules/system/layers/keycloak/org/postgresql
COPY main /opt/jboss/keycloak/modules/system/layers/keycloak/org/postgresql/main
#下面这条命令是修改jboss启动参数中的 -Xms512m -Xmx512m 一致,默认 -Xms 为64m
COPY standalone.conf /opt/jboss/keycloak/bin
ENTRYPOINT ["/opt/jboss/tools/docker-entrypoint.sh"] #这条命令是jboss的启动入口脚本呢,通过这个脚本,官方已经支持了高可用,这些修改都是通过变量写入:standalone.xml文件中的,我们要做的只是在k8s deployment.yml 文件中,定义同样的参数,覆盖掉这些默认的变量就可以了,比如,默认官方镜像的的的高可用探测方式为:ping 方式,我们可以定义JGROUPS_DISCOVERY_PROTOCOL 自动配置默认探测方式。
Replacing the default discovery protocols (PING for the UDP stack and MPING for the TCP one) can be achieved by defining some additional environment variables:
Warning: It's an error to set both JGROUPS_DISCOVERY_PROPERTIES and JGROUPS_DISCOVERY_PROPERTIES_DIRECT. No more than one of them may be set.
The bootstrap script will detect the variables and adjust the standalone-ha.xml configuration based on them.
当然自定义的cli脚本和 其他类型的可执行脚本呢可以放在 /opt/jboss/startup-scripts 下面,其他脚本通过
autorun.sh 来加载。
Warning: Custom scripts have no guarantees. The directory layout within the image may change at any time.
To run custom scripts on container startup place a file in the /opt/jboss/startup-scripts directory.
Two types of scripts are supported:
WildFly .cli scripts. In most of the cases, the scripts should operate in offline mode (using embed-server instruction). It's also worth to mention, that by default, keycloak uses standalone-ha.xml configuration (unless other server configuration is specified).
Any executable (chmod +x) script
Scripts are ran in alphabetical order.
keycloak 镜像重新打包和 k8s yaml文件下载地址为:
keycloak 镜像定制和k8s部署文件下载
使用方法:
下载文件,解压,可以做适当修改,以适合自己环境:
cd kc-ha1
docker buil -t hub.jundax.com/base/keycloak-ha-postgres:v15.0.2 .
docker push hub.jundax.com/base/keycloak-ha-postgres:v15.0.2
kubectl apply -f houchu/k8s1/dev-houchu-0-3/uc
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!