id: aws.md title: Deploy a Milvus Cluster on EC2 related_key: cluster
summary: Learn how to deploy a Milvus cluster on AWS EC2.
在 EC2 部署 Milvus 集群
本文介绍如何使用 Terraform 和 Ansible 在 Amazon EC2 上部署 Milvus 集群。
预置集群
介绍如何使用 Terraform 预置 Milvus 集群。Terraform 是一个基础架构即代码 (IaC) 软件工具。使用 Terraform ,你可以通过使用声明性配置文件来预置基础设施。
先决条件
准备配置
你可以在 Google 云端硬盘 下载模板配置文件。
main.tf这个文件包含了用于预置 Milvus 集群的配置。variables.tf这个文件允许快速编辑用于设置或更新 Milvus 集群的变量。output.tf和inventory.tmpl这些文件存储 Milvus 集群的元数据。本问中使用的元数据是每个节点实例的
public_ip,每个节点实例的private_ip和所有 EC2 实例 ID。
准备 variables.tf
本节描述 variables.tf 文件包含的配置。
节点数量
下面的模板声明一个
index_count变量,用于设置索引节点的数量。index_count的值必须大于等于1。variable "index_count" {description = "Amount of index instances to run"type = numberdefault = 5}
节点类型的实例类型
下面的模板声明了一个
index_ec2_type变量,用于设置索引节点的实例类型。variable "index_ec2_type" {description = "Which server type"type = stringdefault = "c5.2xlarge"}
访问权限
下面的模板声明一个
key_name变量和一个my_ip变量。key_name变量表示 AWS 访问密钥。my_ip变量表示安全组的 IP 地址范围。variable "key_name" {description = "Which aws key to use for access into instances, needs to be uploaded already"type = stringdefault = ""}variable "my_ip" {description = "my_ip for security group. used so that ansible and terraform can ssh in"type = stringdefault = "x.x.x.x/32"}
准备 main.tf
本节描述 main. txt 文件包含的配置。
云提供商和区域
下面的模板使用
us-east-2区域。更多信息请参见可用区域。provider "aws" {profile = "default"region = "us-east-2"}
安全组
下面的模板声明了一个安全组,该安全组允许来自 CIDR 地址范围的流量,该地址范围由在
variables.tf中声明的my_ip表示。resource "aws_security_group" "cluster_sg" {name = "cluster_sg"description = "Allows only me to access"vpc_id = aws_vpc.cluster_vpc.idingress {description = "All ports from my IP"from_port = 0to_port = 65535protocol = "tcp"cidr_blocks = [var.my_ip]}ingress {description = "Full subnet communication"from_port = 0to_port = 65535protocol = "all"self = true}egress {from_port = 0to_port = 0protocol = "-1"cidr_blocks = ["0.0.0.0/0"]ipv6_cidr_blocks = ["::/0"]}tags = {Name = "cluster_sg"}}
虚拟私有云
下面的模板在 Milvus 集群中指定里 CIDR 块为10.0.0.0/24的虚拟私有云。
resource "aws_vpc" "cluster_vpc" {cidr_block = "10.0.0.0/24"tags = {Name = "cluster_vpc"}}resource "aws_internet_gateway" "cluster_gateway" {vpc_id = aws_vpc.cluster_vpc.idtags = {Name = "cluster_gateway"}}
子网 (可选)
下面的模板声明了一个子网,其流量被路由到互联网网关。此时子网的 CIDR 块大小与虚拟私有云的 CIDR 块大小相同。
resource "aws_subnet" "cluster_subnet" {vpc_id = aws_vpc.cluster_vpc.idcidr_block = "10.0.0.0/24"map_public_ip_on_launch = truetags = {Name = "cluster_subnet"}}resource "aws_route_table" "cluster_subnet_gateway_route" {vpc_id = aws_vpc.cluster_vpc.idroute {cidr_block = "0.0.0.0/0"gateway_id = aws_internet_gateway.cluster_gateway.id}tags = {Name = "cluster_subnet_gateway_route"}}resource "aws_route_table_association" "cluster_subnet_add_gateway" {subnet_id = aws_subnet.cluster_subnet.idroute_table_id = aws_route_table.cluster_subnet_gateway_route.id}
节点实例(节点)
下面的模板声明一个 MinIO 节点实例。
main.tf模板文件声明了11个节点类型。对于某些节点类型,需要设置root_block_device。有关更多信息,请参见 EBS、临时块设备和根块设备。resource "aws_instance" "minio_node" {count = var.minio_countami = "ami-0d8d212151031f51c"instance_type = var.minio_ec2_typekey_name = var.key_namesubnet_id = aws_subnet.cluster_subnet.idvpc_security_group_ids = [aws_security_group.cluster_sg.id]root_block_device {volume_type = "gp2"volume_size = 1000}tags = {Name = "minio-${count.index + 1}"}}
应用配置
- 打开一个终端,导航到存储
main.tf的文件夹。 - 运行
terraform init命令初始化配置。 - 要应用配置,运行
terraform apply并在提示时输入yes。
你现在已经使用 Terraform 预置了 Milvus 集群。
启动集群
本节描述如何使用 Ansible 启动你预置完的 Milvus 集群。Ansible 是一个配置管理工具,用于自动化云配置和配置管理。
先决条件
- 安装和配置 Ansible
准备配置
你可以在 Google 云端硬盘下载模板配置文件。
yaml_files文件夹中的文件
这个文件夹存储每个节点类型的 Jinja2 文件。Ansible 使用了 Jinja2 模板。有关 Jinja2 的更多信息请参见介绍。
playbook.yaml
该文件在特定的节点集上执行一组任务。该模板首先在 Milvus 集群的所有节点实例上安装 Docker 和 Docker Compose。
- name: All Servershosts: etcd_ips_public:pulsar_ips_public:minio_ips_public:data_ips_public:index_ips_public:query_ips_public:proxy_ips_public:root_coordinator_ips_public:data_coordinator_ips_public:query_coordinator_ips_public:index_coordinator_ips_publicremote_user: ec2-userbecome: truetags:- starttasks:- name: Install dockeransible.builtin.yum:name: dockerstate: present- name: Run dockeransible.builtin.service:name: dockerstate: started- name: Install or upgrade docker-composeget_url:url : "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64"dest: /usr/local/bin/docker-composemode: 'a+x'force: yes- name: Create symbolic link for docker-composefile:src: "/usr/local/bin/docker-compose"dest: "/usr/bin/docker-compose"state: link
在所有节点实例上安装 Docker 和 Docker Compose 后,playbook.yaml 按顺序启动所有节点实例的容器。
- name: etcdhosts: etcd_ips_publicremote_user: ec2-userbecome: truetags:- starttasks:- name: Copy etcd configansible.builtin.template:src: ./yaml_files/etcd.j2dest: /home/ec2-user/docker-compose.ymlowner: ec2-usergroup: wheelmode: '0644'- name: Run etcd nodeshell: docker-compose up -dargs:chdir: /home/ec2-user/
应用配置
- 打开终端,导航到存放
playbook.yaml的文件夹。 - 运行
ansible-playbook -i inventory playbook.yaml --tags "start"。 - 如果成功,将启动所有节点实例。
现在你已经使用 Ansible 启动了一个 Milvus 集群。
停止节点
当不再需要 Milvus 集群时,可以停止所有节点。
terraform 二进制文件在你的 PATH 上可用。- 运行
terraform destroy并在提示时输入yes。 - 如果成功,则停止所有节点实例。
更多内容
如果你想学习如何在其他云上部署 Milvus:
