我看网上给的教程都比较偏向POC一个简单的demo,我在实际部署到K8S的时候遇到了非常多的问题。这里总结出来。
1. Debezium的docker用处
https://hub.docker.com/u/debezium
在这里可以看到有非常多debezium的docker镜像,
debezium/server 这个主要是用来把信息写入一个自定义的消息队列。例如我们可以使用GCP 的Pub/Sub,可以使用Amazon Kinesis ,HTTP Client,Apache Pulsar,Azure Event Hubs,Redis (Stream)等等。
debezium/kafka 这个就是写入Kafka集群的一个docker
debezium有很多的应用场景,比如在Spring中内嵌也可以。这个具体场景具体分析。
本文使用的是Debezium/server 这个通用的Docker镜像做的部署。
2. debezium/server 的Dockerfile问题
debezium/server - Docker Image | Docker Hub
点击这个链接只能看到最新的一些tag和镜像,网站隐藏了Dockerfile的入口
在这个网址后缀加上 /Dockerfile 即可获取Dockerfile
即: https://hub.docker.com/r/debezium/server/Dockerfile
会发现这个Dockerfile的版本为1.6.1.Final,想替换成最新版本需要去网址查对应的值替换。
例如点击最新 版本2.4.0 Final,找到 ENV DEBEZIUM_VERSION=....这行
替换Dockerfile中的值DEBEZIUM_VERSION 和SERVER_URL_PATH,即可成功build
3. Docker缺少驱动程序
首先debezium/server 这个docker没有Oracle的链接驱动,需要手动下载 ojdbc8 这个jar包,并在打包的时候放到 /debezium/lib 下
GCP的service account 的json认证文件要放到环境变量指定的路径下
4. 用户权限问题
官方默认的Dockerfile用户是一个叫jboss的用户,打包到k8s平台后会报错:
Error: container has runAsNonRoot and image has non-numeric user (jboss), cannot verify user is non-root
这个是因为K8S默认都是非root用户启动,这里需要改Dockerfile文件
完整的Dockerfile:
FROM registry.access.redhat.com/ubi8/openjdk-11
LABEL maintainer="Debezium Community"
#
# Set the version, home directory, and MD5 hash.
#
ENV DEBEZIUM_VERSION=2.4.0.Final\
SERVER_HOME=/debezium \
MAVEN_REPO_CENTRAL="https://repo1.maven.org/maven2"
ENV SERVER_URL_PATH=io/debezium/debezium-server-dist/$DEBEZIUM_VERSION/debezium-server-dist-$DEBEZIUM_VERSION.tar.gz \
SERVER_MD5=34537ef25170a297ceb0df007c35491e#
# Create a directory for Debezium Server
#
USER root
RUN microdnf -y install gzip && \
microdnf clean all && \
mkdir $SERVER_HOME && \
chmod 755 $SERVER_HOME# RUN microdnf -y install iputils
#
# Change ownership and switch user
#
RUN chown -R 1000 $SERVER_HOME && \
chgrp -R 1000 $SERVER_HOME
USER 1000RUN mkdir $SERVER_HOME/conf && \
mkdir $SERVER_HOME/data#
# Download and install Debezium Server
#
RUN curl -fSL -o /tmp/debezium.tar.gz "$MAVEN_REPO_CENTRAL/$SERVER_URL_PATH"#
# Verify the contents and then install ...
#
RUN echo "$SERVER_MD5 /tmp/debezium.tar.gz" | md5sum -c - &&\
tar xzf /tmp/debezium.tar.gz -C $SERVER_HOME --strip-components 1 &&\
rm -f /tmp/debezium.tar.gz#
# Allow random UID to use Debezium Server
#
RUN chmod -R g+w,o+w $SERVER_HOME# Set the working directory to the Debezium Server home directory
WORKDIR $SERVER_HOME
ENV GOOGLE_APPLICATION_CREDENTIALS=/tmp/credentials/service-account.json
COPY ./demo-sa.json /tmp/credentials/service-account.json
COPY ./conf /debezium/conf
COPY ./ojdbc8-21.6.0.0.jar /debezium/lib/#
# Expose the ports and set up volumes for the data, transaction log, and configuration
#
EXPOSE 8080
VOLUME ["/debezium/conf","/debezium/data"]CMD ["/debezium/run.sh"]
5. application.properties的坑
这个文件主要用来配置debezium server和上下游之间的链接信息
debezium.sink.type=pubsub debezium.sink.pubsub.project.id=*** debezium.source.connector.class=io.debezium.connector.oracle.OracleConnector debezium.source.database.hostname=**** debezium.source.database.port=2050 debezium.source.database.user=**** debezium.source.database.password=**** debezium.source.database.dbname= **** debezium.source.table.include.list=schema.table_name debezium.source.include.schema.changes=false debezium.source.topic.prefix=your_gcp_pub_sub_topic_name debezium.source.database.history=io.debezium.relational.history.FileDatabaseHistory debezium.source.database.history.file.filename=tmp/history.dat debezium.source.database.allowPublicKeyRetrieval=true debezium.source.offset.storage.file.filename=data/offsets.dat debezium.source.offset.flush.interval.ms=0 debezium.source.transforms=unwrap debezium.source.transforms.unwrap.type=io.debezium.transforms.ExtractNewRecordState debezium.source.transforms.unwrap.add.fields=op,table,source.ts_ms debezium.source.transforms.unwrap.delete.handling.mode=rewrite debezium.source.key.converter.schemas.enable=false debezium.source.value.converter.schemas.enable=false debezium.source.snapshot.mode=initial debezium.source.schema.history.internal=io.debezium.storage.file.history.FileSchemaHistory debezium.source.schema.history.internal.file.filename=data/schema_history.dat
注意这里面很多配置都是必选项,最好按照我的模版进行修改。都是我踩了无数的坑才好用的。
debezium.source.topic.prefix 这个是GCP里Pub/Sub的Topic名称。不是什么前缀,就是全名。
debezium.sink.pubsub.project.id 就是GCP的project id
debezium.source.database.dbname 不是database的名称,是Oracle的service name。这里和官网给出的解释不一样。。。
debezium.source.table.include.list 是要CDC的table名,命名方式按照如下格式:schema_name.table_name1,schema_name.table_name12,schema_name.table_name3...
6. Mac打包的坑
Mac打包默认用的是Arm架构,在Linux集群的K8S上没法使用
build image的时候使用命令:
docker buildx build --platform linux/amd64 -f ./Dockerfile -t debezium:2.4.0 .