在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还提供更多高级功能如模型注册、部署等,您可以在官方文档中探索这些功能来进一步完善您的机器学习工作流。
登录后查看全文
热门项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
GLM-4.7-FlashGLM-4.7-Flash 是一款 30B-A3B MoE 模型。作为 30B 级别中的佼佼者,GLM-4.7-Flash 为追求性能与效率平衡的轻量化部署提供了全新选择。Jinja00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin07
compass-metrics-modelMetrics model project for the OSS CompassPython00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
522
3.71 K
Ascend Extension for PyTorch
Python
327
384
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
875
576
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
334
161
暂无简介
Dart
762
184
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.32 K
744
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
React Native鸿蒙化仓库
JavaScript
302
349
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
112
134