Deploy a cluster on Docker

You can use two kinds of methods to deploy a Pulsar cluster on Docker. The first uses Docker commands, while the second uses a compose.yml file.

Deploy a cluster using Docker commands

To deploy a Pulsar cluster on Docker, you need to complete the following steps:

  1. Pull a Pulsar Docker image.
  2. Create a network.
  3. Create and start the ZooKeeper, bookie, and broker containers.

Pull a Pulsar image

To run Pulsar on Docker, you need to create a container for each Pulsar component: ZooKeeper, bookie, and the broker. You can pull the images of ZooKeeper and bookie separately on Docker Hub, and pull the Pulsar image for the broker. You can also pull only one Pulsar image and create three containers with this image. This tutorial takes the second option as an example. You can pull a Pulsar image from Docker Hub with the following command. If you do not want to use some connectors, you can use apachepulsar/pulsar:latest there.

  1. docker pull apachepulsar/pulsar-all:latest

Create a network

To deploy a Pulsar cluster on Docker, you need to create a network and connect the containers of ZooKeeper, bookie, and broker to this network. Use the following command to create the network pulsar:

  1. docker network create pulsar

Create and start containers

Create a ZooKeeper container

Create a ZooKeeper container and start the ZooKeeper service.

  1. docker run -d -p 2181:2181 --net=pulsar \
  2. -e metadataStoreUrl=zk:zookeeper:2181 \
  3. -e cluster-name=cluster-a -e managedLedgerDefaultEnsembleSize=1 \
  4. -e managedLedgerDefaultWriteQuorum=1 \
  5. -e managedLedgerDefaultAckQuorum=1 \
  6. -v $(pwd)/data/zookeeper:/pulsar/data/zookeeper \
  7. --name zookeeper --hostname zookeeper \
  8. apachepulsar/pulsar-all:latest \
  9. bash -c "bin/apply-config-from-env.py conf/zookeeper.conf && bin/generate-zookeeper-config.sh conf/zookeeper.conf && exec bin/pulsar zookeeper"

Initialize the cluster metadata

After creating the ZooKeeper container successfully, you can use the following command to initialize the cluster metadata.

  1. docker run --net=pulsar \
  2. --name initialize-pulsar-cluster-metadata \
  3. apachepulsar/pulsar-all:latest bash -c "bin/pulsar initialize-cluster-metadata \
  4. --cluster cluster-a \
  5. --zookeeper zookeeper:2181 \
  6. --configuration-store zookeeper:2181 \
  7. --web-service-url http://broker:8080 \
  8. --broker-service-url pulsar://broker:6650"

Create a bookie container

Create a bookie container and start the bookie service.

  1. docker run -d -e clusterName=cluster-a \
  2. -e zkServers=zookeeper:2181 --net=pulsar \
  3. -e metadataServiceUri=metadata-store:zk:zookeeper:2181 \
  4. -v $(pwd)/data/bookkeeper:/pulsar/data/bookkeeper \
  5. --name bookie --hostname bookie \
  6. apachepulsar/pulsar-all:latest \
  7. bash -c "bin/apply-config-from-env.py conf/bookkeeper.conf && exec bin/pulsar bookie"

Create a broker container

Create a broker container and start the broker service.

  1. docker run -d -p 6650:6650 -p 8080:8080 --net=pulsar -e metadataStoreUrl=zk:zookeeper:2181 -e zookeeperServers=zookeeper:2181 -e clusterName=cluster-a -e managedLedgerDefaultEnsembleSize=1 -e managedLedgerDefaultWriteQuorum=1 -e managedLedgerDefaultAckQuorum=1 --name broker --hostname broker apachepulsar/pulsar-all:latest bash -c "bin/apply-config-from-env.py conf/broker.conf && exec bin/pulsar broker"

Deploy a cluster by using compose.yml

Use the following template to create a compose.yml file to deploy a Pulsar cluster quickly. And you can modify or add the configurations in the environment section.

  1. version: '3'
  2. networks:
  3. pulsar:
  4. driver: bridge
  5. services:
  6. # Start zookeeper
  7. zookeeper:
  8. image: apachepulsar/pulsar:latest
  9. container_name: zookeeper
  10. restart: on-failure
  11. networks:
  12. - pulsar
  13. volumes:
  14. - ./data/zookeeper:/pulsar/data/zookeeper
  15. environment:
  16. - metadataStoreUrl=zk:zookeeper:2181
  17. command: >
  18. bash -c "bin/apply-config-from-env.py conf/zookeeper.conf && \
  19. bin/generate-zookeeper-config.sh conf/zookeeper.conf && \
  20. exec bin/pulsar zookeeper"
  21. healthcheck:
  22. test: ["CMD", "bin/pulsar-zookeeper-ruok.sh"]
  23. interval: 10s
  24. timeout: 5s
  25. retries: 30
  26. # Init cluster metadata
  27. pulsar-init:
  28. container_name: pulsar-init
  29. hostname: pulsar-init
  30. image: apachepulsar/pulsar:latest
  31. networks:
  32. - pulsar
  33. command: >
  34. bin/pulsar initialize-cluster-metadata \
  35. --cluster cluster-a \
  36. --zookeeper zookeeper:2181 \
  37. --configuration-store zookeeper:2181 \
  38. --web-service-url http://broker:8080 \
  39. --broker-service-url pulsar://broker:6650
  40. depends_on:
  41. zookeeper:
  42. condition: service_healthy
  43. # Start bookie
  44. bookie:
  45. image: apachepulsar/pulsar:latest
  46. container_name: bookie
  47. restart: on-failure
  48. networks:
  49. - pulsar
  50. environment:
  51. - clusterName=cluster-a
  52. - zkServers=zookeeper:2181
  53. - metadataServiceUri=metadata-store:zk:zookeeper:2181
  54. depends_on:
  55. zookeeper:
  56. condition: service_healthy
  57. pulsar-init:
  58. condition: service_completed_successfully
  59. # Map the local directory to the container to avoid bookie startup failure due to insufficient container disks.
  60. volumes:
  61. - ./data/bookkeeper:/pulsar/data/bookkeeper
  62. command: bash -c "bin/apply-config-from-env.py conf/bookkeeper.conf
  63. && exec bin/pulsar bookie"
  64. # Start broker
  65. broker:
  66. image: apachepulsar/pulsar:latest
  67. container_name: broker
  68. hostname: broker
  69. restart: on-failure
  70. networks:
  71. - pulsar
  72. environment:
  73. - metadataStoreUrl=zk:zookeeper:2181
  74. - zookeeperServers=zookeeper:2181
  75. - clusterName=cluster-a
  76. - managedLedgerDefaultEnsembleSize=1
  77. - managedLedgerDefaultWriteQuorum=1
  78. - managedLedgerDefaultAckQuorum=1
  79. - advertisedAddress=broker
  80. - advertisedListeners=external:pulsar://127.0.0.1:6650
  81. depends_on:
  82. zookeeper:
  83. condition: service_healthy
  84. bookie:
  85. condition: service_started
  86. ports:
  87. - "6650:6650"
  88. - "8080:8080"
  89. command: bash -c "bin/apply-config-from-env.py conf/broker.conf
  90. && exec bin/pulsar broker"

To create a Pulsar cluster by using the compose.yml file, run the following command.

  1. docker compose up -d

If you want to destroy the Pulsar cluster with all the containers, run the following command. It will also delete the network that the containers are connected to.

  1. docker compose down