Terraform is an infrastructure-as-code (IaC) tool launched by HashiCorp, which includes low-level components such as computing instances, storage, and networking, as well as high-level components such as DNS, LBS, and so on. Users can use Terraform to safely and efficiently build, change and update infrastructure.

In the traditional private cloud or public cloud deployment method, users need to deploy the infrastructure (virtual machine, network, storage, etc.) first, and then start to deploy the MQTT cluster. With Terraform, users can do both at the same time. In addition, the same set of tools can be deployed on different platforms, and resources can be defined and configured in a repeatable and predictable way through templates, which can greatly reduce errors caused by human factors.

This article will take the distributed IoT MQTT message server EMQX as an example, and use Alibaba Cloud as the public cloud platform to introduce how to use Terraform to quickly deploy a highly available MQTT cluster.

Introduction to Terraform

As a tool for managing the lifecycle of services, Terraform can record and track all environmental changes with state files. The default state is stored locally and is defined by HCL or JSON, which is the template language provided by HashiCorp.

Terraform 工作原理

  • Coding: Use HCL to write infrastructure code. Blocks, parameters and expressions can be defined.
  • Plan: Run Terraform Plan to check that the execution plan meets expectations.
  • Apply: Run Terraform Apply to build the infrastructure your users need

Preparation and installation

Install Ali Cloud SDK for Go

 go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

Install Terraform

Take Mac as an example, install it through brew.

 brew tap hashicorp/tap 2brew install hashicorp/tap/terraform

Verify installation

 terraform -help

For details, please refer to the official documentation .

Deploy EMQX cluster using Terraform

Download the Alibaba Cloud deployment script

 git clone https://github.com/emqx/terraform-emqx-emqx-alicloud.git

Deployment script description

Script configuration file path:

  • Single-machine deployment configuration file: services/emqx/terraform.tfvars
  • Cluster deployment configuration file: services/emqx_cluster/terraform.tfvars

The deployment script uses the following configuration by default, and readers can modify it according to the actual situation:

  • Ali Cloud SDK: v1.61.1608

    Older SDK versions may cause ELB deployment to fail
  • Default EMQX version: Enterprise 4.4.3

    If you want to deploy the open source version, you need to modify the terraform.tfvars emqx_package value at the end of the file, such as deploying the open source version 5.0.3:

    emqx_package = https://www.emqx.com/en/downloads/broker/5.0.3/emqx-5.0.3-ubuntu20.04-amd64.tar.gz

  • The default Alibaba Cloud Region is: cn-shenzhen
  • Default cluster nodes: 2 ecs.t6-c1m1.large

Configure Alibaba Cloud AccessKey

Enter the Alibaba Cloud AccessKey page

阿里云 AccessKey 页面

Get the created AccessKey and Secret, and set the environment variables

 export ALICLOUD_ACCESS_KEY=${anaccesskey}
export ALICLOUD_SECRET_KEY=${asecretkey}
export ALICLOUD_REGION=${region}

Deploy EMQX Enterprise Edition Cluster

Users can change the default configuration by modifying the terraform.tfvars file
 cd services/emqx_cluster
terraform init
terraform plan
terraform apply -auto-approve

After waiting for a few minutes, you can see that the deployment is completed as follows:

部署完成

Verify cluster deployment results

After the cluster is successfully deployed, we can simply test whether the cluster is running normally. First obtain the IP address of the cluster (emqx_cluster_address = "120.79.163.50") from the above figure, and access the Dashboard of the EMQX Enterprise Edition through the following information.

 http://120.79.163.50:18083

Username: admin

Password: public

EMQX Dashboard

As can be seen from the above figure, we have deployed 2 nodes. At this time, we can perform simple verification through the WebSocket tool:

  1. On the left menu bar, select Tools→WebSocket

    MQTT WebSocket

  2. Click "Connect"

    MQTT WebSocket连接

  3. Subscribe to topics

    MQTT WebSocket订阅主题

  4. Click Publish, you can see the following results normally

    MQTT WebSocket发布

Epilogue

So far, we have completed the entire process of quickly deploying an MQTT cluster on Alibaba Cloud through Terraform. Readers can modify the deployment script according to the actual situation, create an EMQX cluster that meets their own business needs, and build a competitive IoT platform and application with the help of EMQX's advantages in IoT data connection, mobility, and processing.

References

Code repository: https://github.com/emqx/terraform-emqx-emqx-alicloud

Official documentation of EMQX Terraform module: https://docs.emqx.com/zh/emqx-terraform/latest

Copyright statement: This article is original by EMQ, please indicate the source when reprinting.

Original link: https://www.emqx.com/zh/blog/deploy-mqtt-cluster-on-alibaba-cloud-using-terraform


EMQX
336 声望436 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...