当前位置:网站首页>使用 Terraform 在 AWS 上快速部署 MQTT 集群
使用 Terraform 在 AWS 上快速部署 MQTT 集群
2022-07-25 15:56:00 【EMQX】
Terraform 是由 HashiCorp 推出的一个基础设施即代码(IaC)工具,它包括了底层的组件如计算实例、存储和网络,以及高层的组件如 DNS、LBS 等。用户可以使用 Terraform 安全、高效地构建、改变和更新基础设施。
在传统的私有云或公有云部署方式中,用户需要先部署好基础设施(虚拟机、网络和存储等),之后才能开始部署 MQTT 集群。而如果使用 Terraform,用户则可以同时完成这两项工作。此外,同一套工具可以在不同的平台上进行部署,通过模版可重复、可预测的方式定义和配置资源,可大大减少人为因素导致的错误。
本文将以分布式物联网 MQTT 消息服务器 EMQX 为例,采用 AWS 作为公有云平台,介绍如何使用 Terraform 快速部署一个高可用的 MQTT 集群。
Terraform 简介
作为一个管理服务生命周期的工具,Terraform 可以用状态文件记录和跟踪所有环境变化。默认状态是存储在本地的,通过 HCL 或者 JSON 来定义,HCL 是 HashiCorp 提供的模板语言。

- Coding:用 HCL 来编写基础设施代码。可以定义块,参数和表达式。
- Plan:运行 Terraform Plan 来检查执行计划是否符合期望。
- Apply:运行 Terraform Apply 来构建用户所需的基础设施
安装与准备
安装 Terraform
以 Mac 为例,通过 brew 安装。
brew tap hashicorp/tap 2brew install hashicorp/tap/terraform
验证安装
terraform -help
具体参考官方文档
安装 AWS Cli
以 Mac 为例
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
验证安装
which aws
aws --version
参考官方文档
添加用户
进入 AWS 的 IAM 菜单,点击“添加用户”

在添加用户中,记得勾选“访问密钥”

选择“创建组”

在创建组中添加“AdministratorAccess”策略

点击“创建用户”,完成用户添加

获取 Access Key 和 Secret

配置 AWS 的 Access Key
拿到上面创建好的access key和secret,设置环境变量
AWS_ACCESS_KEY_ID: ${anaccesskey}
AWS_SECRET_ACCESS_KEY: ${asecretkey}
使用 Terraform 在 AWS 上部署 EMQX 集群
下载 AWS 部署脚本
git clone "https://github.com/emqx/terraform-emqx-emqx-aws.git"
部署脚本说明:
- 暂时不支持 EMQX 5.X
- AWS CLI:aws-cli/2.2.41 Python/3.8.8 Darwin/21.4.0 exe/x86_64 prompt/off
脚本配置文件路径:
- 单机部署配置文件:services/emqx/terraform.tfvars
- 集群部署配置文件:services/emqx_cluster/terraform.tfvars
部署脚本默认使用以下配置,读者可根据实际情况自行修改 terraform.tfvars 文件:
- 默认 EMQX 版本:企业版 4.4.3
如果要部署开源版,需要修改
emqx_package值,比如部署开源版 4.4.3:https://www.emqx.com/en/downloads/broker/v4.4.3/emqx-4.4.3-otp23.3.4.9-3-ubuntu20.04-amd64.zip
- 默认AWS Region 为us-east-1
- 默认集群节点:3台
t3.small
部署 EMQX 集群
用户可以通过修改terraform.tfvars文件,来更改默认的配置
cd services/emqx_cluster
terraform init
terraform plan
terraform apply -auto-approve
等待几分钟,部署完成的结果如下所示

验证 EMQX 集群部署结果
集群部署成功后,我们可以简单测试集群是否已正常运行。先从上图获取到集群的IP 地址,并通过以下信息访问 EMQX 企业版的 Dashboard。
http://tf-elb-nlb-5bff6976b15586dd.elb.us-east-1.amazonaws.com:18083用户名:
admin密码:
public

从上图可以看到我们部署了 3 个节点,这时可以通过 Websocket 工具进行简单验证:
左边菜单栏选择工具→WebSocket

点击“连接”

订阅主题

点击发布,正常能看到如下结果

结语
至此,我们完成了通过 Terraform 在 AWS 上快速部署 MQTT 集群的全部流程。读者可根据实际情况修改部署脚本,创建满足自己业务需求的 EMQX 集群,借助 EMQX 在物联网数据连接、移动与处理方面的优势构建具有竞争力的物联网平台与应用。
参考:
代码仓库:https://github.com/emqx/terraform-emqx-emqx-aws
EMQX Terraform模块官方文档:https://docs.emqx.com/zh/emqx-terraform/latest
版权声明: 本文为 EMQ 原创,转载请注明出处。
原文链接:https://www.emqx.com/zh/blog/deploy-mqtt-cluster-on-aws-using-terraform
边栏推荐
- Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
- Equivalent change of resistance circuit (Ⅱ)
- [IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
- MySQL implicit lock
- MySQL教程67-使用DISTINCT过滤重复数据
- Ml image depth learning and convolution neural network
- What is a physical firewall? What's the effect?
- Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)
- MySQL table read lock
- Distributed | practice: smoothly migrate business from MYCAT to dble
猜你喜欢

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

Implementation of recommendation system collaborative filtering in spark

Matlab -- CVX optimization kit installation

Pytoch learning notes advanced_ CNN (using perception_module) implements MNIST dataset classification - (comments and results)

推荐收藏,这或许是最全的类别型特征的编码方法总结

Beyond Compare 4 实现class文件对比【最新】

狂神redis笔记12

泰雷兹推出解决方案,助力SAP客户控制云端数据

How matlab saves all the data after running

Pytoch learning notes -- seresnet50 construction
随机推荐
I interviewed 8 companies and got 5 offers in a week. Share my experience
MySQL教程66-数据表查询语句
泰雷兹推出解决方案,助力SAP客户控制云端数据
Zhaoqi Kechuang high-level innovation and Entrepreneurship Talent Service Platform at home and abroad, mass entrepreneurship and innovation achievement transformation platform
面试8家公司,1周拿了5个offer,分享一下自己的心得
记得那两句话
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】
Typescript learning 2 - Interface
Redis distributed lock, it's really impossible without it
What is a physical firewall? What's the effect?
排他锁(EXclusive Lock)
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
[server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage
Copy a word style template to another document
Wavelet transform --dwt2 and wavedec2
兆骑科创高质量海归人才双创服务平台,线上直播路演
Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]
MySQL显式锁
通用测试用例写作规范