在Netcup服务器上搭建MLflow实验跟踪平台指南
2025-07-08 19:54:17作者:柏廷章Berta
前言
在机器学习和数据科学项目中,实验跟踪是至关重要的环节。MLflow作为一个开源的机器学习生命周期管理平台,能够有效帮助团队记录实验参数、代码版本、评估指标和输出文件。本文将详细介绍如何在Netcup服务器上搭建一个带有基础认证功能的MLflow服务。
系统要求
- 操作系统:Ubuntu 22.04 LTS
- 服务器配置:建议至少VPS 200 G10s规格
- 用户权限:拥有sudo权限的用户
- 网络:开放HTTP/HTTPS端口
环境准备
1. 系统更新与基础软件安装
首先更新系统并安装必要的依赖包:
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt-get install -y python3 python3-pip python3.10-venv postgresql nginx gcc
这些软件包包括:
- Python 3及虚拟环境支持
- PostgreSQL数据库
- Nginx作为反向代理
- GCC编译器
2. 数据库配置
为MLflow创建专用数据库和用户:
sudo -u postgres psql
在PostgreSQL交互界面中执行:
CREATE DATABASE mlflow;
CREATE USER mlflow WITH ENCRYPTED PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE mlflow TO mlflow;
请务必将your_secure_password替换为强密码。
MLflow安装与配置
1. 创建虚拟环境
python3 -m venv mlflow_venv
source mlflow_venv/bin/activate
2. 安装MLflow及相关包
pip install mlflow psycopg2-binary
psycopg2-binary是PostgreSQL的Python适配器。
3. 创建数据目录
mkdir -p ~/mlflow/mlruns # 模型存储目录
mkdir -p ~/mlflow/mllogs # 日志目录
服务部署
1. 测试运行
首次运行MLflow服务进行测试:
mlflow server \
--backend-store-uri postgresql://mlflow:your_secure_password@localhost/mlflow \
--artifacts-destination ~/mlflow/mlruns \
--serve-artifacts \
-h 0.0.0.0 \
-p 8000
访问服务器IP:8000应能看到MLflow界面。
2. 配置系统服务
创建/etc/systemd/system/mlflow.service文件:
[Unit]
Description=MLflow Server
After=network.target
[Service]
Restart=on-failure
RestartSec=30
StandardOutput=file:/home/user/mlflow/mllogs/stdout.log
StandardError=file:/home/user/mlflow/mllogs/stderr.log
User=root
ExecStart=/bin/bash -c 'PATH=/home/user/mlflow_venv/bin:$PATH exec mlflow server --backend-store-uri postgresql://mlflow:your_secure_password@localhost/mlflow --artifacts-destination ~/mlflow/mlruns --serve-artifacts -h 127.0.0.1 -p 8000'
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable mlflow
sudo systemctl start mlflow
Nginx反向代理与认证
1. 安装认证工具
sudo apt-get install -y apache2-utils
2. 创建认证用户
sudo htpasswd -c /etc/apache2/.htpasswd your_username
3. 配置Nginx
修改/etc/nginx/sites-enabled/default:
location / {
proxy_pass http://localhost:8000;
auth_basic "MLflow Access";
auth_basic_user_file /etc/apache2/.htpasswd;
}
重启Nginx:
sudo systemctl restart nginx
使用MLflow跟踪实验
1. 本地环境准备
python3 -m venv mlflow_client
source mlflow_client/bin/activate
pip install mlflow scikit-learn
2. 示例实验代码
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import mlflow
# 设置MLflow跟踪URI
mlflow.set_tracking_uri('http://username:password@your_server_ip')
# 加载数据
data = load_iris()
X, y = data['data'], data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建/获取实验
exp_name = '随机森林树数量实验'
experiment = mlflow.get_experiment_by_name(exp_name)
if experiment is None:
experiment_id = mlflow.create_experiment(exp_name)
else:
experiment_id = experiment.experiment_id
# 运行实验
for n_est in range(1, 21):
with mlflow.start_run(experiment_id=experiment_id):
# 训练模型
rf = RandomForestClassifier(n_estimators=n_est)
rf.fit(X_train, y_train)
# 评估模型
accuracy = rf.score(X_test, y_test)
# 记录参数和指标
mlflow.log_param('n_estimators', n_est)
mlflow.log_metric('accuracy', accuracy)
# 保存模型
mlflow.sklearn.log_model(rf, "random_forest_model")
安全建议
- 启用HTTPS:使用Let's Encrypt获取免费SSL证书
- IP限制:配置防火墙只允许特定IP访问
- 定期备份:设置PostgreSQL数据库的定期备份
- 监控:设置服务监控确保MLflow持续运行
总结
通过本教程,您已经成功搭建了一个具有基础认证功能的MLflow实验跟踪平台。这个平台可以帮助您和团队:
- 系统记录机器学习实验参数和结果
- 比较不同模型的表现
- 保存和共享训练好的模型
- 提高实验的复现性
MLflow还提供更多高级功能如模型注册、部署等,您可以在官方文档中探索这些功能来进一步完善您的机器学习工作流。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0204- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
607
4.05 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
暂无简介
Dart
849
205
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.47 K
829
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
24
0
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
924
772
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
235
152
昇腾LLM分布式训练框架
Python
131
157