k8s 部署 canal 集群,RocketMQ 模式

k8s 部署 canal 集群,RocketMQ 模式

  • [k8s 部署 canal 集群,RocketMQ 模式](#k8s 部署 canal 集群,RocketMQ 模式)
    • 前提
      • MySQL
      • RocketMQ
      • [制作 canal-admin、canal-server 镜像](#制作 canal-admin、canal-server 镜像)
    • [部署 zookeeper](#部署 zookeeper)
    • [部署 canal-admin](#部署 canal-admin)
    • [部署 canal-server](#部署 canal-server)
    • 测试

k8s 部署 canal 集群,RocketMQ 模式

前提

MySQL

开启 binlog 的 MySQL 服务器:192.168.59.1:3306

ini 复制代码
[mysqld]
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定义,不要和 canal 的 slaveId 重复

具有 slave 权限的账号:用户名 canal,密码:canal

sql 复制代码
CREATE USER canal IDENTIFIED BY 'canal';  
-- GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;

RocketMQ

RocketMQ 地址:192.168.56.1:9876

创建 tipoc:canal_test

RocketMQ-Dashboard 地址:192.168.56.1:8080,便于查看消息

制作 canal-admin、canal-server 镜像

由于 canal:v1.1.7 有一个 bug com.alibaba.otter.canal.server.embedded.CanalServerWithEmbedded isn't start , please check · Issue #4930,而官方已经在 Release 1.1.8-alpha 中修复了这个问题,但没有发布官方的 docker 镜像,所以需要自己制作镜像。以下是我制作的镜像,已发布到 dockerhub,可直接使用

复制代码
zhaobingshuang/canal-admin:v1.1.8-alpha
zhaobingshuang/canal-server:v1.1.8-alpha

部署 zookeeper

zk-configmap.yaml

数据目录配置为 /data

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: zk-configmap
data:
  "zoo.cfg": |
    tickTime=2000
    dataDir=/data
    clientPort=2181

zk-pvc.yaml

数据目录持久化

yaml 复制代码
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zk-data-pvc
spec:
  resources:
    requests:
      storage: 2Gi
  accessModes:
    - ReadWriteOnce

zk-deployment.yaml

无状态服务

将 zk-data-pvc 挂载到数据目录 /data,将 zk-configmap 的 zoo.cfg 挂载到 /conf/zoo.cfg 文件

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zk
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zk
  template:
    metadata:
      labels:
        app: zk
    spec:
      containers:
        - name: zk
          image: zookeeper
          ports:
            - containerPort: 2181
              name: serve
          volumeMounts:
            - mountPath: /data
              name: zk-persistent-storage
            - mountPath: /conf
              name: zk-config
              readOnly: true
      volumes:
        - name: zk-persistent-storage
          persistentVolumeClaim:
            claimName: zk-data-pvc
        - name: zk-config
          configMap:
            name: zk-configmap
            items:
              - key: zoo.cfg
                path: zoo.cfg

zk-service.yaml

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: zk
spec:
  selector:
    app: zk
  ports:
    - protocol: TCP
      port: 2181
      targetPort: 2181
  type: ClusterIP
  clusterIP: None

执行以下命令

shell 复制代码
kubectl apply -f zk-configmap.yaml
kubectl apply -f zk-pvc.yaml
kubectl apply -f zk-deployment.yaml
kubectl apply -f zk-service.yaml

打开 kubernetes-dashboard,进入 zk pod 验证。

执行 create /cluster_1 创建 znode,为之后 canal-admin 创建集群使用。

部署 canal-admin

执行 manager.sql 初始化数据库。

canal-admin-configmap.yaml

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: canal-admin-configmap
data:
  "server.port": "8089"
  "spring.datasource.address": "192.168.59.1:3306"
  "spring.datasource.database": "canal_manager"
  "spring.datasource.username": "canal"
  "spring.datasource.password": "canal"
  "canal.adminPasswd": "admin"

canal-admin-deployment.yaml

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: canal-admin
spec:
  selector:
    matchLabels:
      app: canal-admin
  template:
    metadata:
      labels:
        app: canal-admin
    spec:
      containers:
        - name: canal-admin
          image: zhaobingshuang/canal-admin:v1.1.8-alpha
          ports:
            - containerPort: 8089
          envFrom:
            - configMapRef:
                name: canal-admin-configmap

canal-admin-service.yaml

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: canal-admin
spec:
  selector:
    app: canal-admin
  ports:
    - protocol: TCP
      port: 8089
      targetPort: 8089
  type: ClusterIP

canal-admin-ingress.yaml

yaml 复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: canal-admin
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: canal-admin.com
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: canal-admin
              port:
                number: 8089

执行以下命令

shell 复制代码
kubectl apply -f canal-admin-configmap.yaml
kubectl apply -f canal-admin-deployment.yaml
kubectl apply -f canal-admin-service.yaml
kubectl apply -f canal-admin-ingress.yaml

zhaobingshuang/canal-admin:v1.1.8-alpha 镜像比较大,需要等待一段时间等待,等 pod canal-admin 启动后,浏览器访问 http://canal-admin.com/。

用户名:admin,密码:123456

创建集群,集群名称:cluster_1 ,ZK 地址:zk:2181/cluster_1

点击主配置 -> 载入模板

修改以下配置:

properties 复制代码
canal.zkServers =zk:2181/cluster_1
canal.serverMode = rocketMQ
canal.instance.global.spring.xml = classpath:spring/default-instance.xml

rocketmq.namesrv.addr = 192.168.56.1:9876
rocketmq.tag =canal

部署 canal-server

canal-server-configmap.yaml

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: canal-server-configmap
data:
  canal.admin.manager: "canal-admin:8089"
  canal.admin.port: "11110"
  canal.admin.user: "admin"
  canal.admin.passwd: "4ACFE3202A5FF5CF467898FC58AAB1D615029441"
  canal.admin.register.auto: "true"
  canal.admin.register.cluster: "cluster_1"
  

canal-server-service.yaml

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: canal-server-headless
spec:
  selector:
    app: canal-server
  ports:
    - port: 11110
      name: admin
    - port: 11111
      name: tcp
    - port: 11112
      name: metric
  type: ClusterIP
  clusterIP: None

canal-server-statefulset.yaml

yaml 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: canal-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: canal-server
  serviceName: canal-server-headless
  template:
    metadata:
      labels:
        app: canal-server
    spec:
      containers:
        - name: canal-server
          image: zhaobingshuang/canal-server:v1.1.8-alpha
          envFrom:
            - configMapRef:
                name: canal-server-configmap
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: "metadata.namespace"
            - name: POD_HOST
              valueFrom:
                fieldRef:
                  fieldPath: "metadata.name"
            - name: RAW_NAME
              valueFrom:
                fieldRef:
                  fieldPath: "metadata.labels['app']"
            - name: canal.register.ip
              value: $(POD_HOST).$(RAW_NAME)-headless.$(POD_NAMESPACE)
            - name: canal.admin.register.name
              value: $(POD_HOST)
          ports:
            - containerPort: 11110
              name: admin
            - containerPort: 11111
              name: tcp
            - containerPort: 11112
              name: metric

执行以下命令:

shell 复制代码
kubectl apply -f canal-server-configmap.yaml
kubectl apply -f canal-server-service.yaml
kubectl apply -f canal-server-statefulset.yaml

等 pod canal-server启动后,查看 Server 管理

测试

载入模板,修改以下配置之后保存:

properties 复制代码
canal.instance.master.address=192.168.59.1:3306
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal
canal.instance.filter.regex=test\\..*
canal.mq.topic=canal_test

启动实例

执行以下 SQL,创建数据库、创建表、插入数据:

sql 复制代码
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE `table_1` (
  `id` int NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO table_1 VALUES(1, 'tom');
INSERT INTO table_1 VALUES(2, 'jack')

浏览器访问 http://192.168.56.1:8080/ 打开 RocketMQ-Dashboard 查看 canal_test 的消息:

相关推荐
敲上瘾14 小时前
【探索实战】:Kurator分布式统一应用分发平台的全面解析与实践指南
分布式·容器·kubernetes·serverless
Connie14511 天前
记一次K8s故障告警排查(Grafna告警排查)
云原生·容器·kubernetes·grafana
谷隐凡二1 天前
Kubernetes主从架构简单解析:基于Python的模拟实现
python·架构·kubernetes
陈陈CHENCHEN1 天前
SuperMap iManager for K8s 离线环境镜像仓库 Containerd 部署
kubernetes
会飞的小蛮猪2 天前
Ubuntu24.04 基于Containerd部署K8s1.34(私服部署)
docker·云原生·kubernetes
间彧2 天前
Kubernetes滚动发布详解
kubernetes
间彧2 天前
在实际生产环境中,Kubernetes声明式API如何实现蓝绿部署、金丝雀发布等高级部署策略?
kubernetes
间彧2 天前
Kubernetes声明式API相比传统命令式API在故障恢复场景下的具体优势有哪些?
kubernetes·github
间彧2 天前
为什么说Kubernetes的API设计是其成功的关键因素之一?
kubernetes
间彧2 天前
Kubernetes Deployment 配置简化实战:从复杂到高效
kubernetes