Sure! Building a proof-of-concept (PoC) for streaming data ingestion using Kafka and Debezium with MySQL as the source and PostgreSQL as the sink is a great way to understand the ecosystem. Below is a comprehensive, step-by-step guide to achieve this setup using Docker Compose.
Table of Contents
- Prerequisites
- Overview of the Architecture
- Step-by-Step Setup
- Running the Setup
- Testing the Pipeline
- Troubleshooting Tips
- Conclusion
Prerequisites
Before starting, ensure you have the following installed on your machine:
Overview of the Architecture
Here's a high-level diagram of the components involved:
MySQL (Source) → Debezium MySQL Connector (Kafka Connect) → Kafka Topics → PostgreSQL Sink Connector (Kafka Connect) → PostgreSQL (Sink)
Key Components:
- MySQL: Source database with binlog enabled for CDC.
- Debezium MySQL Connector: Captures changes from MySQL and streams to Kafka.
- Apache Kafka: Messaging system to transport data.
- Kafka Connect: Facilitates connectors for source and sink.
- PostgreSQL: Destination database to receive data.
- Zookeeper: Manages Kafka cluster coordination.
Step-by-Step Setup
1. Set Up Docker Compose File
Create a directory for your project and navigate into it:
mkdir kafka-debezium-poc
cd kafka-debezium-poc
Create a docker-compose.yml file with the following content:
version: '3.8'
services:
zookeeper:
image: wurstmeister/zookeeper:3.4.6
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:2.13-2.7.0
ports:
- "9092:9092"
environment:
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zookeeper
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: testdb
MYSQL_USER: debezium
MYSQL_PASSWORD: dbz
command: >
--server-id=1
--log-bin=mysql-bin
--binlog-format=ROW
--gtid_mode=OFF
--enforce-gtid-consistency=OFF
--log-slave-updates=ON
--binlog-row-image=FULL
volumes:
- mysql_data:/var/lib/mysql
postgres:
image: postgres:13
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
kafka-connect:
image: debezium/connect:1.9
ports:
- "8083:8083"
environment:
BOOTSTRAP_SERVERS: kafka:9092
GROUP_ID: 1
CONFIG_STORAGE_TOPIC: my_connect_configs
OFFSET_STORAGE_TOPIC: my_connect_offsets
STATUS_STORAGE_TOPIC: my_connect_statuses
KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter
VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
KEY_CONVERTER_SCHEMAS_ENABLE: "false"
VALUE_CONVERTER_SCHEMAS_ENABLE: "false"
CONNECT_REST_ADVERTISED_HOST_NAME: kafka-connect
CONNECT_PLUGIN_PATH: /kafka/connect
depends_on:
- kafka
- mysql
- postgres
volumes:
- ./plugins:/kafka/connect
volumes:
mysql_data:
postgres_data:
Explanation:
- zookeeper: Manages Kafka's cluster state.
- kafka: Kafka broker accessible on port 9092.
- mysql: MySQL 5.7 with CDC enabled via binlog configurations.
- postgres: PostgreSQL 13 as the sink database.
- kafka-connect: Debezium Kafka Connect container to manage connectors.
2. Configure MySQL
Debezium requires specific MySQL configurations to capture changes. In the docker-compose.yml, the MySQL service is configured with the necessary parameters:
command: >
--server-id=1
--log-bin=mysql-bin
--binlog-format=ROW
--gtid_mode=OFF
--enforce-gtid-consistency=OFF
--log-slave-updates=ON
--binlog-row-image=FULL
This ensures:
- Row-based replication for detailed change data.
- Binary logging enabled for CDC.
Initialize MySQL with sample data:
After starting the containers (next step), you can insert sample data into MySQL.
3. Configure PostgreSQL
PostgreSQL will act as the sink to receive data from Kafka. The PostgreSQL service is set up with default configurations.
Create a Table in PostgreSQL:
We'll set up a table that matches the MySQL source structure for data consistency.
4. Configure Kafka and Zookeeper
The kafka and zookeeper services are set up using wurstmeister images. Kafka is accessible on localhost:9092.
5. Configure Kafka Connect with Debezium MySQL Connector
Debezium Connectors:
To enable Debezium connectors, place the connector plugins in the ./plugins directory.
Create Plugins Directory:
mkdir plugins
Download Debezium MySQL Connector:
You need to download the Debezium MySQL connector and place it in the plugins directory.
However, the debezium/connect Docker image comes pre-installed with connectors, so you might not need to add plugins manually unless you require additional connectors.
6. Configure Kafka Connect with PostgreSQL Sink Connector
We'll use the JDBC Sink Connector provided by Confluent. Ensure the connector’s JAR is available in the ./plugins directory.
Update docker-compose.yml for PostgreSQL Connector:
Modify the kafka-connect service to include the JDBC Sink Connector.
Add environment variables and ensure the connector is accessible.
However, the Debezium Connect image typically includes several connectors. If not, you might need to add them manually.
Alternative Approach:
Use a custom Dockerfile to include the necessary connectors.
Create Dockerfile:
Create a Dockerfile in the plugins directory:
FROM debezium/connect:1.9
# Install JDBC Sink Connector
RUN confluent-hub install --no-prompt confluentinc/kafka-connect-jdbc:latest
Update docker-compose.yml:
Modify the kafka-connect service to build from the Dockerfile:
kafka-connect:
build: ./plugins
ports:
- "8083:8083"
environment:
BOOTSTRAP_SERVERS: kafka:9092
GROUP_ID: 1
CONFIG_STORAGE_TOPIC: my_connect_configs
OFFSET_STORAGE_TOPIC: my_connect_offsets
STATUS_STORAGE_TOPIC: my_connect_statuses
KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter
VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
KEY_CONVERTER_SCHEMAS_ENABLE: "false"
VALUE_CONVERTER_SCHEMAS_ENABLE: "false"
CONNECT_REST_ADVERTISED_HOST_NAME: kafka-connect
CONNECT_PLUGIN_PATH: /kafka/connect
depends_on:
- kafka
- mysql
- postgres
Build the Custom Image:
docker-compose build
7. Configure Kafka Connectors
After the services are up, configure the connectors via Kafka Connect's REST API.
Start Docker Compose:
docker-compose up -d
Wait for all services to start. You can check logs:
docker-compose logs -f
Configure Debezium MySQL Source Connector:
Create a JSON file register-mysql.json:
{
"name": "mysql-connector",
"config": {
"connector.class": "io.debezium.connector.mysql.MySqlConnector",
"tasks.max": "1",
"database.hostname": "mysql",
"database.port": "3306",
"database.user": "debezium",
"database.password": "dbz",
"database.server.id": "184054",
"database.server.name": "dbserver1",
"database.include.list": "testdb",
"database.history.kafka.bootstrap.servers": "kafka:9092",
"database.history.kafka.topic": "schema-changes.testdb"
}
}
Register the Source Connector:
curl -X POST -H "Content-Type: application/json" --data @register-mysql.json http://localhost:8083/connectors
Configure PostgreSQL Sink Connector:
Create a JSON file register-postgres-sink.json:
{
"name": "postgres-sink-connector",
"config": {
"connector.class": "io.confluent.connect.jdbc.JdbcSinkConnector",
"tasks.max": "1",
"topics": "dbserver1.testdb.your_table", # Replace 'your_table' with actual table name
"connection.url": "jdbc:postgresql://postgres:5432/postgres",
"connection.user": "postgres",
"connection.password": "postgres",
"auto.create": "true",
"auto.evolve": "true",
"insert.mode": "insert",
"pk.mode": "none",
"table.name.format": "${topic.split('\\.')[2]}"
}
}
Register the Sink Connector:
curl -X POST -H "Content-Type: application/json" --data @register-postgres-sink.json http://localhost:8083/connectors
Note:
- Replace
your_table with the actual table name from MySQL.
- The sink connector subscribes to the Kafka topic where Debezium publishes changes.
Running the Setup
-
Start All Services:
docker-compose up -d
-
Verify Services are Running:
docker-compose ps
-
Ensure Kafka Connect is Ready:
Wait a few moments to allow Kafka Connect to start and register connectors.
Testing the Pipeline
-
Insert Data into MySQL:
Access the MySQL shell:
docker exec -it kafka-debezium-poc_mysql_1 mysql -u debezium -pdbz testdb
Insert sample data:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john@example.com');
-
Verify Data in PostgreSQL:
Access the PostgreSQL shell:
docker exec -it kafka-debezium-poc_postgres_1 psql -U postgres -d postgres
Check the users table:
SELECT * FROM users;
You should see the data inserted in MySQL replicated to PostgreSQL.
-
Test Real-Time Updates:
In MySQL, perform an update:
UPDATE users SET email = 'john.doe@example.com' WHERE id = 1;
Verify the update in PostgreSQL:
SELECT * FROM users;
Troubleshooting Tips
-
Connector Status:
Check connector status:
curl http://localhost:8083/connectors/mysql-connector/status
curl http://localhost:8083/connectors/postgres-sink-connector/status
-
Kafka Connect Logs:
View Kafka Connect logs for errors:
docker-compose logs -f kafka-connect
-
Ensure Network Connectivity:
All services should be on the same Docker network as defined by Docker Compose.
-
Topic Naming:
Debezium uses a specific naming convention (e.g., dbserver1.testdb.users). Ensure the sink connector listens to the correct topic.
Conclusion
By following this guide, you've set up a local environment where changes in a MySQL database are captured by Debezium, streamed through Kafka, and finally ingested into a PostgreSQL database using Kafka Connect. This PoC demonstrates the powerful combination of Kafka and Debezium for real-time data streaming and replication.
Feel free to expand upon this foundation by adding more tables, handling schema changes, or deploying this setup in a more robust environment!