欢迎光临
我们一直在努力

怎样用Ansible/Terraform自动化部署MLOps整套工具链?

AI基础设施的自动化部署是加速模型从实验到生产的关键。MLOps工具链,如Kubeflow、MLFlow或Seldon,涉及复杂的组件依赖和跨云资源的配置。本篇文章将聚焦于如何结合使用Terraform进行云基础设施(AWS EKS)的快速置备,以及使用Ansible来管理和部署上层的MLFlow应用组件,实现端到端的MLOps环境自动化部署。

1. 为什么结合使用Terraform和Ansible?

  • Terraform (IaC): 专长于基础设施即代码(Infrastructure as Code)。它管理资源生命周期、依赖关系和状态(如VPC、EKS集群、IAM角色)。
  • Ansible (CM): 专长于配置管理(Configuration Management)。它在基础设施之上执行配置任务、安装软件、管理用户和部署应用(如在EKS集群上安装Helm Charts)。

我们将采用“Terraform Provisioning -> Ansible Configuration”的流水线。

2. Phase 1: 使用 Terraform 部署 AWS EKS 集群

首先,我们需要一个Kubernetes集群来承载MLOps组件。这里使用AWS EKS。

创建一个基础的main.tf文件来定义EKS集群:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# providers.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# main.tf - 基础 EKS 部署
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "3.1.0"
  name = "mlops-vpc"
  cidr = "10.0.0.0/16"
  azs = ["us-east-1a", "us-east-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets = ["10.0.3.0/24", "10.0.4.0/24"]
  enable_nat_gateway = true
}

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "19.15.3"
  cluster_name    = "mlflow-mlops-cluster"
  cluster_version = "1.26"
  vpc_id          = module.vpc.vpc_id
  subnet_ids      = concat(module.vpc.private_subnets, module.vpc.public_subnets)

  eks_managed_node_groups = {
    general = {
      desired_capacity = 2
      max_capacity     = 3
      instance_types   = ["t3.medium"]
    }
  }
}

# 输出 kubeconfig 路径,供 Ansible 使用
resource "local_file" "kubeconfig" {
  content  = module.eks.kubeconfig
  filename = "./kubeconfig"
}

执行 Terraform:


1
2
3
terraform init
terraform apply -auto-approve
# 此时,一个名为 'kubeconfig' 的文件已生成,包含了访问 EKS 的凭证。

3. Phase 2: 使用 Ansible 部署 MLFlow

基础设施就绪后,我们使用Ansible来部署MLFlow。MLFlow官方提供了Helm Chart,我们可以利用Ansible的kubernetes.core.helm模块来管理这个部署。

确保你的Ansible控制节点安装了必要的集合:


1
ansible-galaxy collection install kubernetes.core

创建 ansible/mlflow_deploy.yml 剧本:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# ansible/mlflow_deploy.yml
---
- name: Deploy MLFlow Tracking Server on EKS
  hosts: localhost
  connection: local
  vars:
    kubeconfig_path: "./kubeconfig" # 引用 Terraform 输出的文件
    mlflow_namespace: "mlflow-system"

  tasks:
    - name: 确保 Kubeconfig 文件存在
      stat:
        path: "{{ kubeconfig_path }}"
      register: kubeconfig_stat

    - name: 检查 kubeconfig 是否有效
      fail:
        msg: "Kubeconfig file not found at {{ kubeconfig_path }}. Run terraform apply first."
      when: not kubeconfig_stat.stat.exists

    - name: 1. 添加 MLFlow Helm Repository
      kubernetes.core.helm_repository:
        name: mlflow-repo
        repo_url: https://databricks.github.io/helm-charts
        state: present

    - name: 2. 部署 MLFlow Tracking Server
      kubernetes.core.helm:
        name: mlflow
        chart_ref: mlflow-repo/mlflow
        release_namespace: "{{ mlflow_namespace }}"
        create_namespace: true
        kubeconfig: "{{ kubeconfig_path }}"
        values:
          serviceType: LoadBalancer # 使用 LoadBalancer 暴露服务
          backendStore:
            type: postgres # 生产环境建议使用外部数据库
            host: "mlflow-db-svc"
            database: mlflow
        state: present

    - name: 3. 验证部署状态 (等待 LoadBalancer IP)
      ansible.builtin.shell:
        cmd: kubectl get svc mlflow -n {{ mlflow_namespace }} -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' --kubeconfig={{ kubeconfig_path }}
      register: mlflow_service_ip
      retries: 20
      delay: 15
      until: mlflow_service_ip.stdout is defined and mlflow_service_ip.stdout != ""

    - name: 打印 MLFlow 访问地址
      ansible.builtin.debug:
        msg: "MLFlow Tracking Server 部署完成,访问地址: {{ mlflow_service_ip.stdout }}"

执行 Ansible 部署:


1
ansible-playbook -i localhost, ansible/mlflow_deploy.yml

4. 总结

通过上述两阶段流程,我们首先使用Terraform以声明式的方式构建了可靠且可重复的EKS基础设施。随后,Ansible利用Terraform生成的kubeconfig凭证,成功地以配置管理的方式将MLFlow这一复杂的应用部署到EKS集群上。这种 IaC + CM 的组合,为构建稳定、可扩展的 MLOps 工具链提供了高度自动化的解决方案。

【本站文章皆为原创,未经允许不得转载】:汤不热吧 » 怎样用Ansible/Terraform自动化部署MLOps整套工具链?
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址