Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying Canal Cluster in Kubernetes

Tech Dec 4 4

Canal operates by masquerading as a MySQL replica server and sending dump requests to the master. Upon receiving these requests, the MySQL master pushes binlog entries to Canal, which then parses the binlog stream.

Components and Functions

Component Function
MySQL Stores canal-admin and canal-server metadata
Zookeeper Maintains cluster state, client information, and consumer offsets. Handles load balancing between canal-server nodes
canal-admin Web-based management interface for canal-server
canal-server Worker nodes that perform binlog parsing

Prerequisites

  • MySQL instance
  • Zookeeper cluster

Important Notes

  • The MySQL instance being monitored must have binlog enabled in row format
  • Canal clients must connect to canal-server over the network layer (L4)
  • canal-admin uses MySQL for data storage. Without an external database, it embeds a local MySQL instance
  • If using an external database, initialize it with scripts from the GitHub repository
  • Default login credentials for canal-admin web UI: admin/123456
  • canal-admin and canal-server use mutual authentication
  • canal-server is stateless and stores no data locally
  • Instance monitoring positions are stored in Zookeeper
  • Instance names must be unique within the Zookeeper namesapce

Deploying canal-admin

Database Initialization

Two databases are required: one for canal-admin data (requires SQL initialization), and another for canal-server instance metadata (no initialization needed).

The canal-admin initialization SQL:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `canal_manager` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;

USE `canal_manager`;

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `canal_adapter_config`;
CREATE TABLE `canal_adapter_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `category` varchar(45) NOT NULL,
  `name` varchar(45) NOT NULL,
  `status` varchar(45) DEFAULT NULL,
  `content` text NOT NULL,
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `canal_cluster`;
CREATE TABLE `canal_cluster` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(63) NOT NULL,
  `zk_hosts` varchar(255) NOT NULL,
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `canal_config`;
CREATE TABLE `canal_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cluster_id` bigint(20) DEFAULT NULL,
  `server_id` bigint(20) DEFAULT NULL,
  `name` varchar(45) NOT NULL,
  `status` varchar(45) DEFAULT NULL,
  `content` text NOT NULL,
  `content_md5` varchar(128) NOT NULL,
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `sid_UNIQUE` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `canal_instance_config`;
CREATE TABLE `canal_instance_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cluster_id` bigint(20) DEFAULT NULL,
  `server_id` bigint(20) DEFAULT NULL,
  `name` varchar(45) NOT NULL,
  `status` varchar(45) DEFAULT NULL,
  `content` text NOT NULL,
  `content_md5` varchar(128) DEFAULT NULL,
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `canal_node_server`;
CREATE TABLE `canal_node_server` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cluster_id` bigint(20) DEFAULT NULL,
  `name` varchar(63) NOT NULL,
  `ip` varchar(63) NOT NULL,
  `admin_port` int(11) DEFAULT NULL,
  `tcp_port` int(11) DEFAULT NULL,
  `metric_port` int(11) DEFAULT NULL,
  `status` varchar(45) DEFAULT NULL,
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `canal_user`;
CREATE TABLE `canal_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(31) NOT NULL,
  `password` varchar(128) NOT NULL,
  `name` varchar(31) NOT NULL,
  `roles` varchar(31) NOT NULL,
  `introduction` varchar(255) DEFAULT NULL,
  `avatar` varchar(255) DEFAULT NULL,
  `creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 1;

BEGIN;
INSERT INTO `canal_user` VALUES (1, 'admin', '6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', 'Canal Manager', 'admin', NULL, NULL, '2019-07-14 00:05:28');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

Deploying canal-admin

The official Docker image can be used for deployment. Configure parameters via environment variables:

env:
  - name: server.port
    value: '8089'
  - name: spring.datasource.address
    value: '192.168.31.204:3306'
  - name: spring.datasource.database
    value: canal_manager
  - name: spring.datasource.username
    value: canal_admin
  - name: spring.datasource.password
    value: '123qwe!@#QWE'
  - name: canal.adminUser
    value: admin
  - name: canal.adminPasswd
    value: admin

Creating a Cluster

  1. After deploying canal-admin, access the web interface at the service port (default credentials: admin/123456)

  2. Navigate to Cluster Management and select New Cluster. Enter the cluster name and Zookeeper address, then confirm to create a new canal-server cluster

  3. In Cluster Management, select the cluster, then go to Main Configuration to modify the cluster's main configuration file

Required configuration changes:

canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
canal.serverMode = tcp

canal.instance.global.spring.xml = classpath:spring/default-instance.xml

canal.instance.tsdb.enable = true
canal.instance.tsdb.dir = ${canal.file.data.dir:../conf}/${canal.instance.destination:}
canal.instance.tsdb.url = jdbc:mysql://192.168.31.204:3306/canal_manager?useSSL=false&useUnicode=true&characterEncoding=UTF-8
canal.instance.tsdb.dbUsername = canal_admin
canal.instance.tsdb.dbPassword = 123qweQWE!@#
canal.instance.tsdb.spring.xml = classpath:spring/tsdb/mysql-tsdb.xml

Complete configuration details:

#################################################
#########         common argument        #############
#################################################
canal.ip =
canal.register.ip =
canal.port = 11111
canal.metrics.pull.port = 11112

canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441

canal.zkServers = zookeeper-svc:2181
canal.zookeeper.flush.period = 1000
canal.withoutNetty = false
canal.serverMode = tcp
canal.file.data.dir = ${canal.conf.dir}
canal.file.flush.period = 1000

canal.instance.memory.buffer.size = 16384
canal.instance.memory.buffer.memunit = 1024
canal.instance.memory.batch.mode = MEMSIZE
canal.instance.memory.rawEntry = true

canal.instance.detecting.enable = false
canal.instance.detecting.sql = select 1
canal.instance.detecting.interval.time = 3
canal.instance.detecting.retry.threshold = 3
canal.instance.detecting.heartbeatHaEnable = false

canal.instance.transaction.size = 1024
canal.instance.fallbackIntervalInSeconds = 60

canal.instance.network.receiveBufferSize = 16384
canal.instance.network.sendBufferSize = 16384
canal.instance.network.soTimeout = 30

canal.instance.filter.druid.ddl = true
canal.instance.filter.query.dcl = false
canal.instance.filter.query.dml = false
canal.instance.filter.query.ddl = false
canal.instance.filter.table.error = false
canal.instance.filter.rows = false
canal.instance.filter.transaction.entry = false
canal.instance.filter.dml.insert = false
canal.instance.filter.dml.update = false
canal.instance.filter.dml.delete = false

canal.instance.binlog.format = ROW,STATEMENT,MIXED
canal.instance.binlog.image = FULL,MINIMAL,NOBLOB

canal.instance.get.ddl.isolation = false

canal.instance.parser.parallel = true
canal.instance.parser.parallelBufferSize = 256

canal.instance.tsdb.enable = true
canal.instance.tsdb.dir = ${canal.file.data.dir:../conf}/${canal.instance.destination:}
canal.instance.tsdb.url = jdbc:mysql://192.168.31.204:3306/canal_manager?useSSL=false&useUnicode=true&characterEncoding=UTF-8
canal.instance.tsdb.dbUsername = canal_admin
canal.instance.tsdb.dbPassword = 123qweQWE!@#
canal.instance.tsdb.snapshot.interval = 24
canal.instance.tsdb.snapshot.expire = 360

#################################################
#########         destinations        #############
#################################################
canal.destinations =
canal.conf.dir = ../conf
canal.auto.scan = true
canal.auto.scan.interval = 5
canal.auto.reset.latest.pos.mode = false

canal.instance.tsdb.spring.xml = classpath:spring/tsdb/mysql-tsdb.xml

canal.instance.global.mode = manager
canal.instance.global.lazy = false
canal.instance.global.manager.address = ${canal.admin.manager}
canal.instance.global.spring.xml = classpath:spring/default-instance.xml

##################################################
#########           MQ Properties      #############
##################################################
canal.aliyun.accessKey =
canal.aliyun.secretKey =
canal.aliyun.uid=

canal.mq.flatMessage = true
canal.mq.canalBatchSize = 50
canal.mq.canalGetTimeout = 100
canal.mq.accessChannel = local

canal.mq.database.hash = true
canal.mq.send.thread.size = 30
canal.mq.build.thread.size = 8

##################################################
#########              Kafka              #############
##################################################
kafka.bootstrap.servers = 127.0.0.1:6667
kafka.acks = all
kafka.compression.type = none
kafka.batch.size = 16384
kafka.linger.ms = 1
kafka.max.request.size = 1048576
kafka.buffer.memory = 33554432
kafka.max.in.flight.requests.per.connection = 1
kafka.retries = 0

kafka.kerberos.enable = false
kafka.kerberos.krb5.file = "../conf/kerberos/krb5.conf"
kafka.kerberos.jaas.file = "../conf/kerberos/jaas.conf"

##################################################
#########             RocketMQ         #############
##################################################
rocketmq.producer.group = test
rocketmq.enable.message.trace = false
rocketmq.customized.trace.topic =
rocketmq.namespace =
rocketmq.namesrv.addr = 127.0.0.1:9876
rocketmq.retry.times.when.send.failed = 0
rocketmq.vip.channel.enabled = false
rocketmq.tag = 

##################################################
#########             RabbitMQ         #############
##################################################
rabbitmq.host =
rabbitmq.virtual.host =
rabbitmq.exchange =
rabbitmq.username =
rabbitmq.password =
rabbitmq.deliveryMode =

Deploying canal-server

The official Docker image can be used. Since pod IPs change after container restarts and the old registered nodes with stale IPs remain in canal-admin, StatefulSet should be used for deployment.

Deploying canal-server

Configure parameters via environment variables:

env:
  - name: POD_NAME
    valueFrom:
      fieldRef:
        apiVersion: v1
        fieldPath: metadata.name
  - name: SERVICE_NAME
    valueFrom:
      fieldRef:
        apiVersion: v1
        fieldPath: 'metadata.labels[\'app\']'
  - name: STS_NAMESPACE
    valueFrom:
      fieldRef:
        apiVersion: v1
        fieldPath: metadata.namespace
  - name: canal.register.ip
    value: $(POD_NAME).$(SERVICE_NAME)-headless-svc.$(STS_NAMESPACE)
  - name: canal.admin.manager
    value: 'canal-admin-svc.mid:8089'
  - name: canal.admin.port
    value: '11110'
  - name: canal.admin.user
    value: admin
  - name: canal.admin.passwd
    value: 4ACFE3202A5FF5CF467898FC58AAB1D615029441
  - name: canal.admin.register.auto
    value: 'true'
  - name: canal.admin.register.name
    valueFrom:
      fieldRef:
        apiVersion: v1
        fieldPath: metadata.name
  - name: canal.admin.register.cluster
    value: canal-server-beta
  - name: canal.auto.scan
    value: 'true'

Zookeeper Data Structure for Canal

When using canal-admin cluster mode, consumer offsets and related information are stored in Zookeeper. Modifying consumer offset data should be done after stopping the corresponding instance. Use the zkCli.sh tool to manipulate Zookeeper data. Canal stores data under the root path: /otter/canal

Zookeeper structure:

/otter/canal
/otter/canal/cluster
/otter/canal/destinations
/otter/canal/destinations/{instance}/cluster
/otter/canal/destinations/{instance}/running
/otter/canal/destinations/{instance}/1001
/otter/canal/destinations/{instance}/1001/cursor
/otter/canal/destinations/{instance}/1001/filter
  • /otter/canal: Root directory
  • /otter/canal/cluster: Server node list for the entire canal cluster
  • /otter/canal/destinations: Root directory for instance list
  • /otter/canal/destinations/{instance}/cluster: Server node list for a specific instance
  • /otter/canal/destinations/{instance}/running: Currently running node information for an instance
  • /otter/canal/destinations/{instance}/1001: Client information directory
  • /otter/canal/destinations/{instance}/1001/cursor: Client binlog synchronization information (includes consumer offset)
  • /otter/canal/destinations/{instance}/1001/filter: Client interceptor configuration

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.