Kafka
GitLab
CI/CD
DevOps
Configuration Management

How do I set up a Kafka service on gitlab-ci.yml?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

To set up a Kafka service in your gitlab-ci.yml file for GitLab CI/CD pipelines, you essentially enable your applications to perform integration tests involving Apache Kafka without needing to install Kafka on the runners themselves. This guide will walk you through how to configure Kafka as a service, along with ZooKeeper (which Kafka uses for cluster management), and how to interact with these services in your CI jobs.

1. Understanding GitLab CI Services

GitLab CI/CD allows you to define services that run alongside the jobs in your pipeline. These services are additional Docker containers that your application might depend on for testing, such as databases or message brokers like Kafka.

2. Configuring Kafka and ZooKeeper Services

To start, you need to specify Kafka and ZooKeeper in the .gitlab-ci.yml file. You can use the official Docker images bitnami/kafka and bitnami/zookeeper for this purpose.

Here's an example configuration:

yaml
1stages:
2  - test
3
4services:
5  - name: bitnami/zookeeper:latest
6    alias: zookeeper
7  - name: bitnami/kafka:latest
8    alias: kafka
9    command: ["/bin/bash", "-c", "sleep 10 && /opt/bitnami/scripts/kafka/run.sh"]  
10    variables:
11      KAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181
12      ALLOW_PLAINTEXT_LISTENER: "yes"
13
14kafka-integration-test:
15  stage: test
16  script:
17    - echo "Waiting for Kafka to be ready..."
18    - sleep 30  # Wait to ensure Kafka is fully up and running
19    - ./run_tests.sh  # Your test script which includes Kafka interactions

Explanation:

  • Services: The services keyword is used to define Docker images as services.
  • ZooKeeper: We first define the ZooKeeper service. The alias is used so other services and jobs can refer to it by name.
  • Kafka: We specify the Kafka service next, including a custom command delaying its startup until ZooKeeper is ready (sleep 10).
  • Environment Variables: KAFKA_CFG_ZOOKEEPER_CONNECT is set to the ZooKeeper service alias and port, and ALLOW_PLAINTEXT_LISTENER allows non-TLS connections within the pipeline network.

3. Testing Integration

In the kafka-integration-test job:

  • A delay (sleep 30) is added to ensure Kafka has sufficient time to initialize fully considering dependencies on ZooKeeper.
  • The run_tests.sh should contain your actual Kafka-invoking test code, making sure your application can connect to Kafka at kafka:9092, utilizing the service's alias.

4. Additional Configuration for Robust Testing

To further enhance reliability and performance in your CI tests:

  • Resource Limitations: Adjust resource settings under the services if your GitLab runner faces constraints.
  • Kafka Configuration Tweaking: Depending on test specifics, you might need to alter more Kafka settings via environment variables or custom configurations.
  • Networking Issues: Ensure connectivity between services by correctly referring to service aliases and ensuring exposed ports align with Kafka client configurations in your test suites.

Summary Table: Key Configuration Items

Configuration ItemPropertyDescription
ServicesservicesDefines ZooKeeper and Kafka services
ZooKeeper Aliasalias: zookeeperAllows reference to the ZooKeeper service by alias
Kafka Alias and Commandsalias: kafka, commandSets Kafka service alias and startup command
Kafka ConfigurationKAFKA_CFG_ZOOKEEPER_CONNECT, ALLOW_PLAINTEXT_LISTENERInternal networking and security settings
Network Connectivitykafka:9092Kafka service URL for client connections

Conclusion

Setting up Kafka in GitLab CI involves configuring Kafka and ZooKeeper as services in your .gitlab-ci.yml file, ensuring they are properly linked, and handling the startup sequencing. This setup provides a robust environment for running integration tests involving Kafka without elaborate setup requirements on your runners, thus streamlining the integration and test processes in CI pipelines.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.