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.
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:
Explanation:
- Services: The
serviceskeyword is used to define Docker images as services. - ZooKeeper: We first define the ZooKeeper service. The
aliasis 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_CONNECTis set to the ZooKeeper service alias and port, andALLOW_PLAINTEXT_LISTENERallows 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.shshould contain your actual Kafka-invoking test code, making sure your application can connect to Kafka atkafka: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 Item | Property | Description |
| Services | services | Defines ZooKeeper and Kafka services |
| ZooKeeper Alias | alias: zookeeper | Allows reference to the ZooKeeper service by alias |
| Kafka Alias and Commands | alias: kafka, command | Sets Kafka service alias and startup command |
| Kafka Configuration | KAFKA_CFG_ZOOKEEPER_CONNECT, ALLOW_PLAINTEXT_LISTENER | Internal networking and security settings |
| Network Connectivity | kafka:9092 | Kafka 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
- How do I start a RabbitMQ node?
- How do I stop attempting to consume messages off of Kafka when at the end of the log?
- How do I stop the RabbitMQ server on localhost
- How do I stream a video file using Kafka?
- How do I set up TensorFlow in the Google cloud?
- How do I specify the model_config_file variable to tensorflow-serving in docker-compose?
- How do I show my global Git configuration?
- How do I show the changes which have been staged?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.