Deploy a cluster on bare metal

提示

  1. 除了最极端的情况,对于绝大多数的用例来说,单机群安装的Pulsar就能够满足要求了。 如果是创业公司或单个团队想体验下Pulsar,我们推荐使用单集群。 如果你需要使用多集群的 Pulsar 实例 请看这个指南here

  2. 如果要在部署的 Pulsar 集群中使用所有内置的 Pulsar IO连接器。你必须先下载apache-pulsar-io-connectors安装包,然后将apache-pulsar-io-connectors安装到每台 broker 中 Pulsar 安装路径的connectors目录下。如果需要用独立的 function worker 运行Pulsar Functions,则也需要将包安装在 worker 节点的对应目录。

  3. 如果要在部署的集群使用分层存储特性,你必须先下载apache-pulsar-offloaders安装包,然后将apache-pulsar-offloaders安装到每台 broker 中 Pulsar 安装路径的offloaders </1>目录下。 了解该特性的详细配置,请参考分层存储指南

部署Pulsar集群包括以下步骤(按顺序):

准备工作

要求

Currently, Pulsar is available for 64-bit *macOS, Linux, and Windows. To use Pulsar, you need to install 64-bit JRE/JDK 8 or later versions.

If you already have an existing ZooKeeper cluster and want to reuse it, you do not need to prepare the machines for running ZooKeeper.

裸机部署 Pulsar, 推荐如下配置:

  • 至少 6 台 Linux 机器或虚拟机
  • 包含 Pulsar 所有 broker 主机的 DNS(域名解析服务器)

Note

Broker is only supported on 64-bit JVM.

如果你没有足够的机器,或者希望尝试集群模式(待后续扩容集群),你能够在一个节点上部署整个 Pulsar 集群,即 Zookeeper ,bookie 和 broker 都运行在同一台机器上。

如果你没有 DNS 服务器,你能够使用多个域名组成的格式的服务的 URL。

Each machine in your cluster needs to have Java 8 or Java 11 installed.

下面是一个显示部署架构的图:

alt-text

在图中,客户端必须能够使用单个 URL 连接到 Pulsar 集群。 在这种情况下,pulsar-cluster.acme.com 包含了所有的broker 的地址。 Pulsar消息brokers与BookKeeper bookies一起运行;反过来,brokers 和 bookies 都依赖 ZooKeeper。

硬件条件

部署 Pulsar 集群,当你进行容量规划时,下面是推荐的规划信息。

ZooKeeper

For machines running ZooKeeper, is is recommended to use less powerful machines or VMs. Pulsar uses ZooKeeper only for periodic coordination-related and configuration-related tasks, not for basic operations. 如果 Pulsar 是运行在Amazon Web Service(AWS)上,比如使用t2.small实例就足够了。

Bookies 和 Brokers

运行 bookie 和 Pulsar broker 的机器,必须使用高规格的机器。 例如,对于AWS部署,i3.4xlarge实例可能是合适的。 你可以参考如下方法选择机器:

  • 高性能的CPU和10Gbps NIC (适用于Pulsar brokers)
  • 小型快速固态硬盘(SSD)或硬盘驱动器(HDD),带有RAID控制器和电池供电的写缓存(适用于BookKeeper bookies)

Pulsar 二进制包安装

You need to install the Pulsar binary package on each machine in the cluster, including machines running ZooKeeper and BookKeeper.

开始在裸机上部署 Pulsar 集群,你必须通过如下方式下载发行版的二进制的包:

  1. $ wget https://archive.apache.org/dist/pulsar/pulsar-2.8.0/apache-pulsar-2.8.0-bin.tar.gz

包下载完成后,解压并进入到解压后的目录:

  1. $ tar xvzf apache-pulsar-2.8.0-bin.tar.gz
  2. $ cd apache-pulsar-2.8.0

解压后的文件目录包含以下子目录:

目录内容
bincommand-line tools of Pulsar, such as pulsar and pulsar-admin
confPulsar的配置文件,包含broker配置,ZooKeeper 配置 等等
dataZookeeper 和 Bookeeper 使用的数据保存目录
libPulsar 使用的 JAR 文件
logs日志目录

安装内置 Connectors(可选)

从 Pulsar 2.1.0-incubating发行版开始,Pulsar 提供了独立的二进制包,包含了所有内置的connector。 如果你想要使用内置的connector,你可以参考如下的说明,否则你可以 暂时跳过此部分

要启用内置的connector,你必须通过如下方式在每台 broker 上下载 connector 发行版压缩包:

下载完成 .nar 文件后,将其复制到 Pulsar 安装目录的connectors目录下。 例如,你下载了pulsar-io-aerospike-2.8.0.narconnector 文件:

  1. $ mkdir connectors
  2. $ mv pulsar-io-aerospike-2.8.0.nar connectors
  3. $ ls connectors
  4. pulsar-io-aerospike-2.8.0.nar
  5. ...

安装分层存储(可选)

从Pulsar 2.2.0发行版开始,Pulsar 提供了一个独立的包含分层存储的二进制包。 如果你想启用分层存储,你可以参考如下的说明;否则你可以 暂时跳过该部分。

要启用分层存储,你必须通过如下方式在每台 broker 上下载分层存储发行版压缩包:

压缩包下载完后,解压压缩包,并将复制后重命名为offloaders,存放在 pulsar 安装目录下。

  1. $ tar xvfz apache-pulsar-offloaders-2.8.0-bin.tar.gz
  2. // you can find a directory named `apache-pulsar-offloaders-2.8.0` in the pulsar directory
  3. // then copy the offloaders
  4. $ mv apache-pulsar-offloaders-2.8.0/offloaders offloaders
  5. $ ls offloaders
  6. tiered-storage-jcloud-2.8.0.nar

了解更多分层存储的详细配置,请参考分层存储指南

部署 Zookeeper 集群

如果你已经有 zookeeper 集群,并且你想复用它,你可以跳过这一步。

ZooKeeper manages a variety of essential coordination- and configuration-related tasks for Pulsar. 部署 Pulsar 集群,你必须首先部署 Zookeeper (在所有其他组件之前)。 建议部署3个节点的 Zookeeper 集群。 Pulsar does not make heavy use of ZooKeeper, so more lightweight machines or VMs should suffice for running ZooKeeper.

首先,将所有的 Zookeeper 服务器信息添加到conf/zookeeper.conf配置文件(文件在 Pulsar 的安装目录里面)。 如下所示:

  1. server.1=zk1.us-west.example.com:2888:3888
  2. server.2=zk2.us-west.example.com:2888:3888
  3. server.3=zk3.us-west.example.com:2888:3888

如果你只有一台机器部署 Pulsar,你仅仅只需把加这台服务器添加到配置文件里面。

在每台机器,你必须在每个节点的myid文件里面配置集群唯一的ID。默认情况下,这个文件在每台机器的data/zookeeper目录里面(你可以通过修改dataDir配置项修改这个路径)。

参考多集群安装指南的Zookeeper 文档了解更多关于myid或者其他部分的详细信息。

例如,在地址为zk1.us-west.example.com的Zookeeper 服务器上,你能够使用如下命令设置myid

  1. $ mkdir -p data/zookeeper
  2. $ echo 1 > data/zookeeper/myid

zk2.us-west.example.com服务器上,这命令可以是:echo 2>data/zookeeper/myid

一旦你在每台机器增加了zookeeper.conf配置文件,并且设置了myid,你能够在所有机器上使用pulsar-daemon命令去启动Zookeeper 服务(前台运行或者后台运行)。

  1. $ bin/pulsar-daemon start zookeeper

如果你规划 Zookeeper 和 Bookie部署在同一台机器上,你 必须配置 zookeeper 监听其他的端口,然后再启动。

使用pulsar-daemon CLI命令行工具启动zookeeper,如:

  1. $ PULSAR_EXTRA_OPTS="-Dstats_server_port=8001" bin/pulsar-daemon start zookeeper

初始化集群元数据

Once you deploy ZooKeeper for your cluster, you need to write some metadata to ZooKeeper for each cluster in your instance. You only need to write this data once.

You can initialize this metadata using the initialize-cluster-metadata command of the pulsar CLI tool. This command can be run on any machine in your ZooKeeper cluster. 如下所示:

  1. $ bin/pulsar initialize-cluster-metadata \
  2. --cluster pulsar-cluster-1 \
  3. --zookeeper zk1.us-west.example.com:2181 \
  4. --configuration-store zk1.us-west.example.com:2181 \
  5. --web-service-url http://pulsar.us-west.example.com:8080 \
  6. --web-service-url-tls https://pulsar.us-west.example.com:8443 \
  7. --broker-service-url pulsar://pulsar.us-west.example.com:6650 \
  8. --broker-service-url-tls pulsar+ssl://pulsar.us-west.example.com:6651

从上面的示例中可以看出,您需要指定以下内容:

标记说明
—cluster集群名字
—zookeeperA “local” ZooKeeper connection string for the cluster. This connection string only needs to include one machine in the ZooKeeper cluster.
—configuration-store整个集群实例的配置存储连接字符串。 As with the —zookeeper flag, this connection string only needs to include one machine in the ZooKeeper cluster.
—web-service-urlThe web service URL for the cluster, plus a port. This URL should be a standard DNS name. The default port is 8080 (you had better not use a different port).
—web-service-url-tlsIf you use TLS, you also need to specify a TLS web service URL for the cluster. The default port is 8443 (you had better not use a different port).
—broker-service-urlBroker服务的URL,用于与集群中的brokers进行交互。 这个 URL 不应该使用和 web 服务 URL 同样的 DNS名称,而应该是用pulsar方案。 默认端口是6650(我们不建议使用其他端口)。
—broker-service-url-tls如果使用TLS,你必须为集群指定一个 TLS web 服务URL,以及用于集群中 broker TLS 服务的URL。 默认端口是6651(不建议使用其他端口)。

如果你没有 DNS 服务器,你能够使用多个域名组成的格式的服务的 URL,格式如下:

  1. --web-service-url http://host1:8080,host2:8080,host3:8080 \
  2. --web-service-url-tls https://host1:8443,host2:8443,host3:8443 \
  3. --broker-service-url pulsar://host1:6650,host2:6650,host3:6650 \
  4. --broker-service-url-tls pulsar+ssl://host1:6651,host2:6651,host3:6651

如果想使用已经存在的 Bookeeper 集群,你可以增加--existing-bk-metadata-service-uri如下标记:

  1. --existing-bk-metadata-service-uri "zk+null://zk1:2181;zk2:2181/ledgers" \
  2. --web-service-url http://host1:8080,host2:8080,host3:8080 \
  3. --web-service-url-tls https://host1:8443,host2:8443,host3:8443 \
  4. --broker-service-url pulsar://host1:6650,host2:6650,host3:6650 \
  5. --broker-service-url-tls pulsar+ssl://host1:6651,host2:6651,host3:6651

您可以使用 bin/bookkeeper shell whatisinstanceid 命令获取现有BookKeeper 集群的元数据服务 的URI。 由于多个元数据服务 URI 用分号隔开,您必须用双引号显示该值。

部署 Bookeeper 集群

BookKeeper handles all persistent data storage in Pulsar. 你必须先部署一个 BookKeeper 集群,才能够使用 Pulsar。 You can choose to run a 3-bookie BookKeeper cluster.

你能够通过配置文件conf/bookkeeper.conf去配置 BookKeeper bookies。 配置 bookies 最重要的一步,是要确保zkServers设置为 Zookeeper 集群的连接信息。 如下所示:

  1. zkServers=zk1.us-west.example.com:2181,zk2.us-west.example.com:2181,zk3.us-west.example.com:2181

一旦你适当地修改了zkServers 参数,你可以修改任何你需要的其他配置。 你可以在 here了解到完整的可用的 BookKeeper 配置项列表。 然而,建议通过 Bookeeper 官方文档了解更多深层的信息。

一旦修改完conf/bookkeeper.conf文件的相关配置,你就可以在每台 Bookeeper 机器上启动 bookie。 You can start up each bookie either in the background, using nohup, or in the foreground.

To start the bookie in the background, use the pulsar-daemon CLI tool:

  1. $ bin/pulsar-daemon start bookie

To start the bookie in the foreground:

  1. $ bin/pulsar bookie

你可以通过运行BookKeeper shell上的bookiesanity命令验证 bookie 是否正常工作:

  1. $ bin/bookkeeper shell bookiesanity

这个命令会在本地的 bookie 创建一个临时的 BookKeeper ledger,往里面写一些条目,然后读取它,最后删除这个 ledger。

启动了所有的 bookie 后,你能够在任意一台bookie上,使用BookKeeper shellsimpletest命令,去校验集群内所有的 bookie 是否都已经启动。

  1. $ bin/bookkeeper shell simpletest --ensemble <num-bookies> --writeQuorum <num-bookies> --ackQuorum <num-bookies> --numEntries <num-entries>

这个命令会在集群上创建和bookie同等数量的ledger,并往里面写一些条目,然后读取它,最后删除这个ledger。

部署 Pulsar broker

Pulsar brokers are the last thing you need to deploy in your Pulsar cluster. Broker 用来处理 Pulsar 消息,并且提供管理Pulsar的接口。 A good choice is to run 3 brokers, one for each machine that already runs a BookKeeper bookie.

配置 Broker

Broker 配置中有一些非常重要的参数,这些参数可以确保每个Broker 连接到已部署的 ZooKeeper 集群。 需要确认 zookeeperServersconfigurationStoreServers 配置项的值是正确的。 在当前情况下,由于只有一个集群,没有单独用来存储配置的 Zookeeper 集群,那么配置项configurationStoreServerszookeeperServers 是一样的值。

  1. zookeeperServers=zk1.us-west.example.com:2181,zk2.us-west.example.com:2181,zk3.us-west.example.com:2181
  2. configurationStoreServers=zk1.us-west.example.com:2181,zk2.us-west.example.com:2181,zk3.us-west.example.com:2181

你必须配置集群的名字( 初始化集群元数据 提供的集群名字必须和这个配置项匹配):

  1. clusterName=pulsar-cluster-1

此外,初始化集群的时候提供的 broker 和 web 服务的端口也必须和下面的配置匹配(特别是当你定义了和默认值不同的端口的时候)

  1. brokerServicePort=6650
  2. brokerServicePortTls=6651
  3. webServicePort=8080
  4. webServicePortTls=8443

如果你部署的是单节点的 Pulsar 集群,你需要把配置文件conf/broker.conf的副本数量配置为1.

  1. # Number of bookies to use when creating a ledger
  2. managedLedgerDefaultEnsembleSize=1
  3. # Number of copies to store for each message
  4. managedLedgerDefaultWriteQuorum=1
  5. # Number of guaranteed copies (acks to wait before write is complete)
  6. managedLedgerDefaultAckQuorum=1

启用 Pulsar Function(可选)

If you want to enable Pulsar Functions, you can follow the instructions as below:

  1. conf/broker.conf文件的配置项functionsWorkerEnabled设置为true,启用函数 worker,

    1. functionsWorkerEnabled=true
  2. 修改conf/functions_worker.yml的配置项pulsarFunctionsCluster 为集群的名称,该名称和初始化集群元数据使用的集群名字是同一个。

    1. pulsarFunctionsCluster: pulsar-cluster-1

如果想了解更多函数 worker 部署相关的信息,请参考函数 worker 部署和管理

启动 Broker

你可以将配置文件conf/broker.conf提供的任何配置项,修改为你需要的值。 一旦确定了配置,就可以启动 Pulsar 集群里的 broker。 和 Zookeeper 和 BookKeeper 一样,你可以选择前台启动或后台启动。

You can start a broker in the foreground using the pulsar broker command:

  1. $ bin/pulsar broker

You can start a broker in the background using the pulsar-daemon CLI tool:

  1. $ bin/pulsar-daemon start broker

一旦启动了你打算使用的所有 broker, 你的 Pulsar 集群应该就准备就绪了!

连接集群

Once your Pulsar cluster is up and running, you should be able to connect with it using Pulsar clients. One such client is the pulsar-client tool, which is included with the Pulsar binary package. pulsar-client工具能够从 Pulsar 主题生产消息和消费消息,并提供了一个简单的方式来确保你的集群运行正常。

To use the pulsar-client tool, first modify the client configuration file in conf/client.conf in your binary package. 你需要修改webServiceUrlbrokerServiceUrl的值,将默认localhost值替换为你给 broker/bookie 指定的 DNS 名称。 如下所示:

  1. webServiceUrl=http://us-west.example.com:8080
  2. brokerServiceurl=pulsar://us-west.example.com:6650

如果你没有 DNS 服务器,你能够使用多个域名组成的格式的服务的 URL:

  1. webServiceUrl=http://host1:8080,host2:8080,host3:8080
  2. brokerServiceurl=pulsar://host1:6650,host2:6650,host3:6650

完成后,你能够发送消息到 Pulsar 主题:

  1. $ bin/pulsar-client produce \
  2. persistent://public/default/test \
  3. -n 1 \
  4. -m "Hello Pulsar"

如果你指定了 pulsar-cluster 1 以外的集群名称,你需要在主题中使用不同的集群名称。

This command publishes a single message to the Pulsar topic. In addition, you can subscribe to the Pulsar topic in a different terminal before publishing messages as below:

  1. $ bin/pulsar-client consume \
  2. persistent://public/default/test \
  3. -n 100 \
  4. -s "consumer-test" \
  5. -t "Exclusive"

一旦您成功地向主题发送了上面的消息,您应该在标准输出中看到它:

  1. ----- 收到消息 -----
  2. Hello Pulsar

运行函数

如果你启用 Pulsar 函数,你就可以试用 Pulsar 函数了。

创建一个名为exclamation的函数 ExclamationFunction。

  1. bin/pulsar-admin functions create \
  2. --jar examples/api-examples.jar \
  3. --classname org.apache.pulsar.functions.api.examples.ExclamationFunction \
  4. --inputs persistent://public/default/exclamation-input \
  5. --output persistent://public/default/exclamation-output \
  6. --tenant public \
  7. --namespace default \
  8. --name exclamation

检查是否按照预期触发函数运行。

  1. bin/pulsar-admin functions trigger --name exclamation --trigger-value "hello world"

你能够看到如下的输出:

  1. hello world!