일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- seldon core
- Argo
- nginx ingress
- CANARY
- kubernetes operator
- Litmus
- CI/CD
- opentelemetry
- xdp
- mlops
- Continuous Deployment
- keda
- tekton
- Kopf
- opensearch
- eBPF
- Kubernetes 인증
- MLflow
- Kubernetes
- operator
- Model Serving
- 카오스 엔지니어링
- 오퍼레이터
- gitops
- Kubeflow
- blue/green
- serving
- knative
- argocd
- Pulumi
- Today
- Total
Kubernetes 이야기
mlflow 본문
MLflow는 End to End 기계 학습 수명주기를 관리하기 위한 오픈 소스 플랫폼으로 다음과 같은 구성 요소를 제공한다.
구성요소
- MLflow Tracking : mlflow에서 가장 많이 사용하는 기능으로 실험을 기록하고 쿼리할 수 있다. 또한 각 실험의 코드, 데이터, 구성 및 결과를 추적한다.
- MLflow Project : 코드를 플랫폼에 구애받지 않는 형식으로 패키징하여 실험을 재현할 수 있다.
- MLflow Model : 다양한 ML 라이브러리에서 다양한 모델 제공 및 추론 플랫폼까지 모델을 관리하고 배포할 수 있다.
- MLflow Model Registry : 모델 버전 관리, 단계 전환 및 주석을 포함하여 MLflow 모델의 전체 수명 주기를 공동으로 관리하기 위한 중앙 모델 저장소를 제공
MLflow는 Python, R, Java 라이브러리를 지원하지만 특정 언어의 라이브러리에 구애받지 않도록 REST API 및 CLI 를 통해 액세스할 수 있으므로 모든 기계 학습 라이브러리 및 프로그래밍 언어와 함께 사용할 수 있다.
설치
전체적인 구성은 다음과 같이 구성하고 Kubernetes위에 설치를 진행해보자. (proxy모드를 사용하여 client에서 직접 s3로 model을 저장하지 않고 tracking server에만 접근하는 방식도 존재한다. 향후 알아보자.)
사전준비사항
helm으로 mlflow 설치 전 사전에 준비사항은 다음과 같다.
- PostreSQL ( 또는 MySQL )
- Object Storage ( S3, Azure blob Stroage, Minio 등 )
1) postgresql
apiVersion: v1
kind: Secret
metadata:
name: postgres-sec
type: Opaque
data:
POSTGRES_USER: dGVzdA==
POSTGRES_PASSWORD: dGVzdA==
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app: postgres
name: postgres
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: postgres
serviceName: postgres-svc
template:
metadata:
labels:
app: postgres
spec:
containers:
- env:
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
key: POSTGRES_USER
name: postgres-sec
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
key: POSTGRES_PASSWORD
name: postgres-sec
- name: POSTGRES_DB
value: test
image: postgres:12.4-alpine
imagePullPolicy: IfNotPresent
name: postgres
ports:
- containerPort: 5432
protocol: TCP
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-pvc
volumeClaimTemplates:
- apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
labels:
app: postgres
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-storage
---
apiVersion: v1
kind: Service
metadata:
name: mlflow-postgres
spec:
ports:
- port: 5432
selector:
app: postgres
2) minio ( 간단한 예제)
apiVersion: v1
kind: Pod
metadata:
labels:
app: minio
name: minio
spec:
containers:
- name: minio
image: quay.io/minio/minio:latest
command:
- /bin/bash
- -c
args:
- minio server /data --console-address :9090
volumeMounts:
- mountPath: /data
name: localvolume
volumes:
- name: localvolume
persistentVolumeClaim:
claimName: minio
---
apiVersion: v1
kind: Service
metadata:
name: mlflow-minio
spec:
ports:
- name: http
nodePort: 30020
port: 9090
protocol: TCP
targetPort: 9090
- name: minio
nodePort: 30021
port: 9000
protocol: TCP
targetPort: 9000
selector:
app: minio
type: NodePort
mlflow용 버킷을 생성한다.
이제 mlflow를 설치해 보자.
helm repo
helm repo add community-charts https://community-charts.github.io/helm-charts
helm repo update
helm upgrade --install mlflow -n mlflow-server community-charts/mlflow \
--set backendStore.databaseMigration=false \
--set backendStore.postgres.enabled=true \
--set backendStore.postgres.host=mlflow-postgres \
--set backendStore.postgres.port=5432 \
--set backendStore.postgres.database=test \
--set backendStore.postgres.user=test \
--set backendStore.postgres.password=test \
--set artifactRoot.s3.enabled=true \
--set artifactRoot.s3.bucket=mlflow \
--set artifactRoot.s3.awsAccessKeyId=minioadmin \
--set artifactRoot.s3.awsSecretAccessKey=minioadmin \
--set extraEnvVars.MLFLOW_S3_ENDPOINT_URL=http://mlflow-minio:9000 \
--set serviceMonitor.enabled=false
이제 mlflow 서비스에 접속해 보자.
실습
먼저 개발환경에서 mlfow sdk 를 설치해보자.
# pip install mlflow
설치 후 로컬호스트에 설치된 mlflow ui를 확인해보자. mlflow ui 명령어는 mlflow tracking server를 띄워주는 기능이다.
# mlflow ui
Kubernetes에 설치한 환경과 동일한 서버가 보이는 것을 확인할 수 있다. 개발할 때는 별도에 서버를 설치하지 않고도 쉽게 사용할 수 있다.
이제 mlflow에서 제공하는 예제를 실행해 보자.
예제는 다음 링크에 있다.
https://mlflow.org/docs/latest/tutorials-and-examples/tutorial.html
Tutorial — MLflow 2.6.0 documentation
This tutorial uses a dataset to predict the quality of wine based on quantitative features like the wine’s “fixed acidity”, “pH”, “residual sugar”, and so on. The dataset is from UCI’s machine learning repository. Now that you have your tra
mlflow.org
train.py를 저장 후 바로 실행해 보자.
python ./train.py
Elasticnet model (alpha=0.500000, l1_ratio=0.500000):
RMSE: 0.7931640229276851
MAE: 0.6271946374319586
R2: 0.10862644997792614
Traceback (most recent call last):
...
mlflow.exceptions.MlflowException: The configured tracking uri scheme: 'file' is invalid for use with the proxy mlflow-artifact scheme. The allowed tracking schemes are: {'http', 'https'}
실행 중 마지막에 오류가 발생한다.
mlflow.set_tracking_uri("http://127.0.0.1:5000") 를 추가하여 해결하였다.
실행 후 ui 화면에서 조회해 보자.
사용했던 파라미터와 메트릭 정보, 생성된 model 정보를 상세히 볼 수 있다.
이제 파라미터를 변경하여 다시 실행해 보자.
# python ./train.py 0.01 0.50
Elasticnet model (alpha=0.010000, l1_ratio=0.500000):
RMSE: 0.6778557583356976
MAE: 0.5190564939146215
R2: 0.3489590462840657
Registered model 'ElasticnetWineModel' already exists. Creating a new version of this model...
INFO mlflow.tracking._model_registry.client: Waiting up to 300 seconds for model version to finish creation. Model name: ElasticnetWineModel, version 2
Created version '2' of model 'ElasticnetWineModel'.
ui에 추가된 로그가 보이고, 2개의 모델 파라미터등의 비교해 볼 수 있다.
이제 remote mlflow에 연결하여 로그를 저장해 보자.
소스에서 다음을 추가한다.
os.environ["MLFLOW_S3_ENDPOINT_URL"] = "http://myserver:30021"
os.environ["AWS_ACCESS_KEY_ID"] = "minioadmin"
os.environ["AWS_SECRET_ACCESS_KEY"] = "minioadmin"
#mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_tracking_uri("http://myserver:30300")
이제 소스를 실행하면 다음과 같이 remote mlflow에 저장되는 것을 볼 수 있다.
'Kubernetes > MLOps' 카테고리의 다른 글
Kafka를 사용하여 실시간 데이터 파이프라인 구축 (1) | 2023.12.11 |
---|---|
MLflow Serving (0) | 2023.09.09 |
dvc (0) | 2023.09.08 |
KServe, Seldon Core, BentoML 비교 (0) | 2023.04.03 |
Kubeflow - Pipeline (0) | 2023.03.26 |