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 集群

本文介绍如何使用 TerraformAnsibleAmazon EC2 上部署 Milvus 集群。

预置集群

介绍如何使用 Terraform 预置 Milvus 集群。Terraform 是一个基础架构即代码 (IaC) 软件工具。使用 Terraform ,你可以通过使用声明性配置文件来预置基础设施。

先决条件

准备配置

你可以在 Google 云端硬盘 下载模板配置文件。

  • main.tf 这个文件包含了用于预置 Milvus 集群的配置。

  • variables.tf 这个文件允许快速编辑用于设置或更新 Milvus 集群的变量。

  • output.tfinventory.tmpl

    这些文件存储 Milvus 集群的元数据。本问中使用的元数据是每个节点实例的 public_ip,每个节点实例的 private_ip 和所有 EC2 实例 ID。

准备 variables.tf

本节描述 variables.tf 文件包含的配置。

  • 节点数量

    下面的模板声明一个 index_count 变量,用于设置索引节点的数量。

    index_count 的值必须大于等于1。
    1. variable "index_count" {
    2. description = "Amount of index instances to run"
    3. type = number
    4. default = 5
    5. }
  • 节点类型的实例类型

    下面的模板声明了一个 index_ec2_type 变量,用于设置索引节点的实例类型

    1. variable "index_ec2_type" {
    2. description = "Which server type"
    3. type = string
    4. default = "c5.2xlarge"
    5. }
  • 访问权限

    下面的模板声明一个 key_name 变量和一个 my_ip 变量。key_name 变量表示 AWS 访问密钥。my_ip 变量表示安全组的 IP 地址范围。

    1. variable "key_name" {
    2. description = "Which aws key to use for access into instances, needs to be uploaded already"
    3. type = string
    4. default = ""
    5. }
    6. variable "my_ip" {
    7. description = "my_ip for security group. used so that ansible and terraform can ssh in"
    8. type = string
    9. default = "x.x.x.x/32"
    10. }

准备 main.tf

本节描述 main. txt 文件包含的配置。

  • 云提供商和区域

    下面的模板使用 us-east-2 区域。更多信息请参见可用区域

    1. provider "aws" {
    2. profile = "default"
    3. region = "us-east-2"
    4. }
  • 安全组

    下面的模板声明了一个安全组,该安全组允许来自 CIDR 地址范围的流量,该地址范围由在 variables.tf 中声明的 my_ip 表示。

    1. resource "aws_security_group" "cluster_sg" {
    2. name = "cluster_sg"
    3. description = "Allows only me to access"
    4. vpc_id = aws_vpc.cluster_vpc.id
    5. ingress {
    6. description = "All ports from my IP"
    7. from_port = 0
    8. to_port = 65535
    9. protocol = "tcp"
    10. cidr_blocks = [var.my_ip]
    11. }
    12. ingress {
    13. description = "Full subnet communication"
    14. from_port = 0
    15. to_port = 65535
    16. protocol = "all"
    17. self = true
    18. }
    19. egress {
    20. from_port = 0
    21. to_port = 0
    22. protocol = "-1"
    23. cidr_blocks = ["0.0.0.0/0"]
    24. ipv6_cidr_blocks = ["::/0"]
    25. }
    26. tags = {
    27. Name = "cluster_sg"
    28. }
    29. }
  • 虚拟私有云

    下面的模板在 Milvus 集群中指定里 CIDR 块为10.0.0.0/24的虚拟私有云。

    1. resource "aws_vpc" "cluster_vpc" {
    2. cidr_block = "10.0.0.0/24"
    3. tags = {
    4. Name = "cluster_vpc"
    5. }
    6. }
    7. resource "aws_internet_gateway" "cluster_gateway" {
    8. vpc_id = aws_vpc.cluster_vpc.id
    9. tags = {
    10. Name = "cluster_gateway"
    11. }
    12. }
  • 子网 (可选)

    下面的模板声明了一个子网,其流量被路由到互联网网关。此时子网的 CIDR 块大小与虚拟私有云的 CIDR 块大小相同。

    1. resource "aws_subnet" "cluster_subnet" {
    2. vpc_id = aws_vpc.cluster_vpc.id
    3. cidr_block = "10.0.0.0/24"
    4. map_public_ip_on_launch = true
    5. tags = {
    6. Name = "cluster_subnet"
    7. }
    8. }
    9. resource "aws_route_table" "cluster_subnet_gateway_route" {
    10. vpc_id = aws_vpc.cluster_vpc.id
    11. route {
    12. cidr_block = "0.0.0.0/0"
    13. gateway_id = aws_internet_gateway.cluster_gateway.id
    14. }
    15. tags = {
    16. Name = "cluster_subnet_gateway_route"
    17. }
    18. }
    19. resource "aws_route_table_association" "cluster_subnet_add_gateway" {
    20. subnet_id = aws_subnet.cluster_subnet.id
    21. route_table_id = aws_route_table.cluster_subnet_gateway_route.id
    22. }
  • 节点实例(节点)

    下面的模板声明一个 MinIO 节点实例。main.tf 模板文件声明了11个节点类型。对于某些节点类型,需要设置 root_block_device。有关更多信息,请参见 EBS、临时块设备和根块设备

    1. resource "aws_instance" "minio_node" {
    2. count = var.minio_count
    3. ami = "ami-0d8d212151031f51c"
    4. instance_type = var.minio_ec2_type
    5. key_name = var.key_name
    6. subnet_id = aws_subnet.cluster_subnet.id
    7. vpc_security_group_ids = [aws_security_group.cluster_sg.id]
    8. root_block_device {
    9. volume_type = "gp2"
    10. volume_size = 1000
    11. }
    12. tags = {
    13. Name = "minio-${count.index + 1}"
    14. }
    15. }

应用配置

  1. 打开一个终端,导航到存储 main.tf 的文件夹。
  2. 运行 terraform init 命令初始化配置。
  3. 要应用配置,运行 terraform apply 并在提示时输入 yes

你现在已经使用 Terraform 预置了 Milvus 集群。

启动集群

本节描述如何使用 Ansible 启动你预置完的 Milvus 集群。Ansible 是一个配置管理工具,用于自动化云配置和配置管理。

先决条件

准备配置

你可以在 Google 云端硬盘下载模板配置文件。

  • yaml_files 文件夹中的文件

这个文件夹存储每个节点类型的 Jinja2 文件。Ansible 使用了 Jinja2 模板。有关 Jinja2 的更多信息请参见介绍

  • playbook.yaml

该文件在特定的节点集上执行一组任务。该模板首先在 Milvus 集群的所有节点实例上安装 Docker 和 Docker Compose。

Playbook 是按照从上到下的顺序运行的。在每个 Play 中,任务也从上到下依次运行。
  1. - name: All Servers
  2. hosts: 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_public
  3. remote_user: ec2-user
  4. become: true
  5. tags:
  6. - start
  7. tasks:
  8. - name: Install docker
  9. ansible.builtin.yum:
  10. name: docker
  11. state: present
  12. - name: Run docker
  13. ansible.builtin.service:
  14. name: docker
  15. state: started
  16. - name: Install or upgrade docker-compose
  17. get_url:
  18. url : "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64"
  19. dest: /usr/local/bin/docker-compose
  20. mode: 'a+x'
  21. force: yes
  22. - name: Create symbolic link for docker-compose
  23. file:
  24. src: "/usr/local/bin/docker-compose"
  25. dest: "/usr/bin/docker-compose"
  26. state: link

在所有节点实例上安装 Docker 和 Docker Compose 后,playbook.yaml 按顺序启动所有节点实例的容器。

  1. - name: etcd
  2. hosts: etcd_ips_public
  3. remote_user: ec2-user
  4. become: true
  5. tags:
  6. - start
  7. tasks:
  8. - name: Copy etcd config
  9. ansible.builtin.template:
  10. src: ./yaml_files/etcd.j2
  11. dest: /home/ec2-user/docker-compose.yml
  12. owner: ec2-user
  13. group: wheel
  14. mode: '0644'
  15. - name: Run etcd node
  16. shell: docker-compose up -d
  17. args:
  18. chdir: /home/ec2-user/

应用配置

  1. 打开终端,导航到存放 playbook.yaml 的文件夹。
  2. 运行 ansible-playbook -i inventory playbook.yaml --tags "start"
  3. 如果成功,将启动所有节点实例。

现在你已经使用 Ansible 启动了一个 Milvus 集群。

停止节点

当不再需要 Milvus 集群时,可以停止所有节点。

确保 terraform 二进制文件在你的 PATH 上可用。
  1. 运行 terraform destroy 并在提示时输入 yes
  2. 如果成功,则停止所有节点实例。

更多内容

如果你想学习如何在其他云上部署 Milvus: