Run a standalone Pulsar cluster in Docker

For local development and testing, you can run Pulsar in standalone mode on your own machine within a Docker container.

If you have not installed Docker, download it following the instructions for your OS.

To run Pulsar in Docker, follow the steps below.

Step1: Start Pulsar in Docker

For macOS, Linux, and Windows, run the following command to start Pulsar within a Docker container.

  • macOS & Linux
  • Windows
  1. docker run -it \
  2. -p 6650:6650 \
  3. -p 8080:8080 \
  4. --mount source=pulsardata,target=/pulsar/data \
  5. --mount source=pulsarconf,target=/pulsar/conf \
  6. apachepulsar/pulsar:3.3.2 \
  7. bin/pulsar standalone
  1. docker run -it ^
  2. -p 6650:6650 ^
  3. -p 8080:8080 ^
  4. --mount source=pulsardata,target=/pulsar/data ^
  5. --mount source=pulsarconf,target=/pulsar/conf ^
  6. apachepulsar/pulsar:3.3.2 ^
  7. bin/pulsar standalone

Run Pulsar in Docker - 图1tip

You may encounter issues with the default RocksDB metadata store.

We recommend you consider using the following environment variable to use ZooKeeper as the metadata store:

  1. ...
  2. -e PULSAR_STANDALONE_USE_ZOOKEEPER=1 \
  3. ...

Don’t apply this fix for existing Pulsar standalone instances if you don’t want to loose your data.

If you want to change Pulsar configurations and start Pulsar, run the following command by passing environment variables with the PULSAR_PREFIX_ prefix. See default configuration file for more details.

  • macOS & Linux
  • Windows
  1. docker run -it \
  2. -e PULSAR_PREFIX_xxx=yyy \
  3. -p 6650:6650 \
  4. -p 8080:8080 \
  5. --mount source=pulsardata,target=/pulsar/data \
  6. --mount source=pulsarconf,target=/pulsar/conf \
  7. apachepulsar/pulsar:3.3.2 sh \
  8. -c "bin/apply-config-from-env.py \
  9. conf/standalone.conf && \
  10. bin/pulsar standalone"
  1. docker run -it ^
  2. -e PULSAR_PREFIX_xxx=yyy ^
  3. -p 6650:6650 ^
  4. -p 8080:8080 ^
  5. --mount source=pulsardata,target=/pulsar/data ^
  6. --mount source=pulsarconf,target=/pulsar/conf ^
  7. apachepulsar/pulsar:3.3.2 sh ^
  8. -c "bin/apply-config-from-env.py ^
  9. conf/standalone.conf && ^
  10. bin/pulsar standalone"

Run Pulsar in Docker - 图2tip

  • The docker container runs as UID 10000 and GID 0 by default. You need to ensure the mounted volumes give write permission to either UID 10000 or GID 0. Note that UID 10000 is arbitrary, so it is recommended to make these mounts writable for the root group (GID 0).
  • The data, metadata, and configuration are persisted on Docker volumes to not start “fresh” every time the container is restarted. For details on the volumes, you can use docker volume inspect <sourcename>.
  • For Docker on Windows, make sure to configure it to use Linux containers.

After starting Pulsar successfully, you can see INFO-level log messages like this:

  1. 08:18:30.970 [main] INFO org.apache.pulsar.broker.web.WebService - HTTP Service started at http://0.0.0.0:8080
  2. ...
  3. 07:53:37.322 [main] INFO org.apache.pulsar.broker.PulsarService - messaging service is ready, bootstrap service port = 8080, broker url= pulsar://localhost:6650, cluster=standalone, configs=org.apache.pulsar.broker.ServiceConfiguration@98b63c1
  4. ...

Run Pulsar in Docker - 图3tip

  • To perform a health check, you can use the bin/pulsar-admin brokers healthcheck command. For more information, see Pulsar admin docs.
  • When you start a local standalone cluster, a public/default namespace is created automatically. The namespace is used for development purposes. All Pulsar topics are managed within namespaces. For more information, see Topics.

Step 2: Use Pulsar in Docker

Pulsar offers a variety of client libraries, such as Java, Go, Python, C++.

If you’re running a local standalone cluster, you can use one of these root URLs to interact with your cluster:

  • pulsar://localhost:6650
  • http://localhost:8080

The following example guides you to get started with Pulsar by using the Python client API.

Install the Pulsar Python client library directly from PyPI:

  1. pip install pulsar-client

Consume a message

Create a consumer and subscribe to the topic:

  1. import pulsar
  2. client = pulsar.Client('pulsar://localhost:6650')
  3. consumer = client.subscribe('my-topic', subscription_name='my-sub')
  4. while True:
  5. msg = consumer.receive()
  6. print("Received message: '%s'" % msg.data())
  7. consumer.acknowledge(msg)
  8. client.close()

Produce a message

Start a producer to send some test messages:

  1. import pulsar
  2. client = pulsar.Client('pulsar://localhost:6650')
  3. producer = client.create_producer('my-topic')
  4. for i in range(10):
  5. producer.send(('hello-pulsar-%d' % i).encode('utf-8'))
  6. client.close()

Step 3: Get the topic statistics

In Pulsar, you can use REST API, Java, or command-line tools to control every aspect of the system. For details on APIs, refer to Admin API Overview.

In the simplest example, you can use curl to probe the stats for a particular topic:

  1. curl http://localhost:8080/admin/v2/persistent/public/default/my-topic/stats | python -m json.tool

The output is something like this:

  1. {
  2. "msgRateIn": 0.0,
  3. "msgThroughputIn": 0.0,
  4. "msgRateOut": 1.8332950480217471,
  5. "msgThroughputOut": 91.33142602871978,
  6. "bytesInCounter": 7097,
  7. "msgInCounter": 143,
  8. "bytesOutCounter": 6607,
  9. "msgOutCounter": 133,
  10. "averageMsgSize": 0.0,
  11. "msgChunkPublished": false,
  12. "storageSize": 7097,
  13. "backlogSize": 0,
  14. "offloadedStorageSize": 0,
  15. "publishers": [
  16. {
  17. "accessMode": "Shared",
  18. "msgRateIn": 0.0,
  19. "msgThroughputIn": 0.0,
  20. "averageMsgSize": 0.0,
  21. "chunkedMessageRate": 0.0,
  22. "producerId": 0,
  23. "metadata": {},
  24. "address": "/127.0.0.1:35604",
  25. "connectedSince": "2021-07-04T09:05:43.04788Z",
  26. "clientVersion": "2.8.0",
  27. "producerName": "standalone-2-5"
  28. }
  29. ],
  30. "waitingPublishers": 0,
  31. "subscriptions": {
  32. "my-sub": {
  33. "msgRateOut": 1.8332950480217471,
  34. "msgThroughputOut": 91.33142602871978,
  35. "bytesOutCounter": 6607,
  36. "msgOutCounter": 133,
  37. "msgRateRedeliver": 0.0,
  38. "chunkedMessageRate": 0,
  39. "msgBacklog": 0,
  40. "backlogSize": 0,
  41. "msgBacklogNoDelayed": 0,
  42. "blockedSubscriptionOnUnackedMsgs": false,
  43. "msgDelayed": 0,
  44. "unackedMessages": 0,
  45. "type": "Exclusive",
  46. "activeConsumerName": "3c544f1daa",
  47. "msgRateExpired": 0.0,
  48. "totalMsgExpired": 0,
  49. "lastExpireTimestamp": 0,
  50. "lastConsumedFlowTimestamp": 1625389101290,
  51. "lastConsumedTimestamp": 1625389546070,
  52. "lastAckedTimestamp": 1625389546162,
  53. "lastMarkDeleteAdvancedTimestamp": 1625389546163,
  54. "consumers": [
  55. {
  56. "msgRateOut": 1.8332950480217471,
  57. "msgThroughputOut": 91.33142602871978,
  58. "bytesOutCounter": 6607,
  59. "msgOutCounter": 133,
  60. "msgRateRedeliver": 0.0,
  61. "chunkedMessageRate": 0.0,
  62. "consumerName": "3c544f1daa",
  63. "availablePermits": 867,
  64. "unackedMessages": 0,
  65. "avgMessagesPerEntry": 6,
  66. "blockedConsumerOnUnackedMsgs": false,
  67. "lastAckedTimestamp": 1625389546162,
  68. "lastConsumedTimestamp": 1625389546070,
  69. "metadata": {},
  70. "address": "/127.0.0.1:35472",
  71. "connectedSince": "2021-07-04T08:58:21.287682Z",
  72. "clientVersion": "2.8.0"
  73. }
  74. ],
  75. "isDurable": true,
  76. "isReplicated": false,
  77. "allowOutOfOrderDelivery": false,
  78. "consumersAfterMarkDeletePosition": {},
  79. "nonContiguousDeletedMessagesRanges": 0,
  80. "nonContiguousDeletedMessagesRangesSerializedSize": 0,
  81. "durable": true,
  82. "replicated": false
  83. }
  84. },
  85. "replication": {},
  86. "deduplicationStatus": "Disabled",
  87. "nonContiguousDeletedMessagesRanges": 0,
  88. "nonContiguousDeletedMessagesRangesSerializedSize": 0
  89. }