Deploying a Pulsar cluster on AWS using Terraform and Ansible

    Amazon Web Services 上运行 Pulsar 的最简单方法之一是使用 Terraform基础设施配置工具和 服务器自动化工具。 Terraform 可以创建运行 Pulsar 集群所需的资源——EC2实例、网络和安全基础设施等。而 Ansible 可以在提供的资源上安装和运行 Pulsar。

    为了使用 Terraform and Ansible 安装一个 Pulsar 集群,您需要准备下列事情:

    • AWS 命令行工具
    • Python 和
    • terraform-inventory 工具,它使得能够使用 Terraform 发行版镜像

    您还需要确保您目前通过 命令行工具登录到您的 AWS 帐户:

    安装

    您可以使用pip在Linux或者macOS上安装Ansible

    1. $ pip install ansible

    您可以使用 [这里](https://www. terraform. io/intro/getting-started/install. html) 的说明来安装 Terraform.

    您还需要本地设备上的 Terraform 和Anlibly 配置Pulsar。 您可以在 中找到他们,您可以通过 Git 命令获取:

    1. $ git clone https://github.com/apache/pulsar
    2. $ cd pulsar/deployment/terraform-ansible/aws

    如果您已经有了SSH的key并且准备使用该key,那么您可以跳过生成SSH key这个步骤,并且将 private_key_file配置在 ansible.cfg 文件中,然后将public_key_path 配置在terraform.tfvars文件中。

    例如,如果您在 ~/中已经有一个私有的 SSH 密钥。 sh/pulsar_aws~/.ssh/pulsar_aws.pub, 按照下面的步骤:

    1. 使用以下值更新 ansible.cfg
      1. 使用以下值更新 terraform.tfvars
      1. public_key_path=~/.ssh/pulsar_aws.pub

      要使用 Terraform 创建必要的 AWS 资源,您需要先创建 SSH 密钥。 输入以下命令在 ~/.ssh/id_rsa 创建一个私有的 SSH 密钥和一个 ~/.ssh/id_rsa.pub SSH 公钥:

      1. $ ssh-keygen -t rsa

      Do not enter a passphrase (hit Enter instead when the prompt comes out). Enter the following command to verify that a key has been created:

      使用 Terraform 创建AWS 资源

      1. $ terraform init
      2. # This will create a .terraform folder

      此后,您可以通过输入此命令来应用默认的 Terraform 配置:

      1. $ terraform apply

      接下来你可以看到这个提示:

      1. Do you want to perform these actions?
      2. Terraform will perform the actions described above.
      3. Only 'yes' will be accepted to approve.
      4. Enter a value:

      Type yes and hit Enter. 应用配置可能需要几分钟。 当配置完成后,您可以看到 Apply complete! 以及其他信息,包括创建的资源数量。

      You can apply a non-default Terraform configuration by changing the values in the file. The following variables are available:

      当您运行Ansible 操作手册时,使用了以下AWS资源:

      集群的所有 EC2 实例都运行在 us-west-2 区域。

      在你输入 terraform apply这个命令时将应用Terraform的配置,Terraform命令将输出的pulsar_service_url的值. 输出结果类似以下︰

      1. pulsar://pulsar-elb-1800761694.us-west-2.elb.amazonaws.com:6650

      您可以随时通过输入命令 terraform output pulsar_service_url 或者读取 文件 来获取该值(即JSON, 即使文件名没有反映这一点):

      1. $ cat terraform.tfstate | jq .modules[0].outputs.pulsar_service_url.value

      在任何时候,您都可以使用 Terraform’s destroy 命令来销毁与您的集群所使用的所有AWS 资源:

      在运行 Pulsar之前,您需要将磁盘挂载到这些节点上的正确目录中。 由于不同类型的机器有不同的磁盘配置,您需要更新在 < setup-disk.yaml设置磁盘中定义的任务,在您的terraform配置中更改 instance_types

      1. $ ansible-playbook \
      2. --user='ec2-user' \
      3. --inventory=`which terraform-inventory` \
      4. setup-disk.yaml

      此后,磁盘被挂载在 /mnt/journal> 下作为日记磁盘,/mnt/storage 作为记账磁盘。 注意只需要输入一次该命令。 如果您试图在运行 Pulsar 示例后再次输入此命令, 您的磁盘可能再次被擦除,导致Pulsar未能启动。

      运行 Pulsar 示例

      Once you have created the necessary AWS resources using Terraform, you can install and run Pulsar on the Terraform-created EC2 instances using Ansible.

      (可选) 如果您想要使用任何 built-in IO connectors , 下载 Pulsar IO 包 任务并编辑 pulsar.yaml 文件,然后取消对您想要使用的连接器配置。

      要运行示例,请输入此命令:

      1. $ ansible-playbook \
      2. --user='ec2-user' \
      3. --inventory=`which terraform-inventory` \
      4. ../deploy-pulsar.yaml

      If you have created a private SSH key at a location different from ~/.ssh/id_rsa, you can specify the different location using the --private-key flag in the following command:

      1. $ ansible-playbook \
      2. --user='ec2-user' \
      3. --inventory=`which terraform-inventory` \
      4. --private-key="~/.ssh/some-non-default-key" \
      5. ../deploy-pulsar.yaml

      You can now access your running Pulsar using the unique Pulsar connection URL for your cluster, which you can obtain following the instructions .

      For a quick demonstration of accessing the cluster, we can use the Python client for Pulsar and the Python shell. First, install the Pulsar Python module using pip:

      1. $ pip install pulsar-client

      Now, open up the Python shell using the python command:

      1. $ python

      Once you are in the shell, enter the following command:

      If all of these commands are successful, Pulsar clients can now use your cluster!