Dejan Lukić

How to Deploy MLflow on the Vultr Kubernetes Engine

Originally written for Vultr


MLflow is a versatile open-source Machine Learning (ML) platform for model lifecycle management. It supports experimentation, duplicability, deployment, and a central model registry. MLflow is used to track machine learning workload experiments, packaging and deployment of code, and collaborative management. Frequently used by data scientists and by MLOps professionals.

This article explains how to deploy MLflow on a Vultr Kubernetes Engine (VKE) cluster with a working environment and demo application.


Before you begin:

Deploy Persistent Volume and Persistent Volume Claims (PVCs)

MLflow requires persistent storage to store artifacts and experiment data. In this section, deploy a PV and PVC to the cluster as described in the steps below.

  1. Create a new file mlflow-pv-pvc.yaml

    $ touch mlflow-pv-pvc.yaml
  2. Using a text editor such as nano, edit the file

    $ nano mlflow-pv-pvc.yaml
  3. Add the following contents to the file

     apiVersion: v1
     kind: PersistentVolume
     name: mlflow-pv
         type: local
     storageClassName: manual
         storage: 10Gi
         - ReadWriteOnce
         path: "/mnt/data"
     apiVersion: v1
     kind: PersistentVolumeClaim
     name: mlflow-pvc
     storageClassName: manual
         - ReadWriteOnce
         storage: 10Gi

    Save the file.

  4. Apply the configuration to your cluster

     $ kubectl apply -f mlflow-pv-pvc.yaml
  5. Verify the deployed Cluster PVs

     $ kubectl get pv


     mlflow-pv   10Gi       RWO            Retain           Bound    default/mlflow-pvc   manual                

Install Mlflow

  1. Using Helm, add community-charts to your repositories

     $ helm repo add community-charts
  2. Update the Helm repository index

     $ helm repo update
  3. Install MLflow. Replace `vultr`` with your desired MLflow label

     $ helm install vultr community-charts/mlflow
  4. View the cluster deployment to verify that MLflow is ready and available

     $ kubectl get deployments

    Your output should look like the one below:

     vultr-mlflow    1/1     1            1           19h

Expose the MLflow Forwarding Service For External Access

To access your MLflow deployment over the Internet, set up a forwarding service to expose the application for external access as described in the steps below.

  1. Create a new file mlflow-service.yaml

     $ touch mlflow-service.yaml
  2. Edit the file

     $ nano mlflow-service.yaml
  3. Add the following contents to the file

     apiVersion: v1
     kind: Service
         name: mlflow-service
             app: mlflow
            - protocol: TCP
             port: 80
             targetPort: 5000
     type: LoadBalancer
  4. Apply the service to your cluster

     $ kubectl apply -f mlflow-service.yaml

Wait for at least 3 minutes, view the cluster services, and verify the MLflow External IP value

    $ kubectl get services

Your output should look like the one below:

    NAME             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE

    kubernetes       ClusterIP       <none>        443/TCP        40m

    mlflow-service   LoadBalancer   80:31545/TCP   64s

    vultr-mlflow     ClusterIP    <none>        5000/TCP       101s

Create a sample Mlflow Experiment

To test the Mlflow functionalities, create a sample linear regression experiment as described in the steps below.

  1. Create a new directory Models

     $ mkdir Models
  2. Switch to the directory

     $ cd Models
  3. Using `pip``, install the necessary experiment dependencies

     $ pip install mlflow scikit-learn shap matplotlib

    When using Conda, install the dependencies in your environment:

     $ conda install mlflow scikit-learn shap matplotlib
  4. Create a new environment variable with your desired experiment name

     $ export MLFLOW_EXPERIMENT_NAME='my-sample-experiment'
  5. Export a new MLFLOW_TRACKING_URI with your MLflow service external IP HTTP URL as the value. For example

     $ export MLFLOW_TRACKING_URI=''
  6. Create a new file

     $ touch
  7. Edit the file

     $ nano
  8. Add the following contents to the file

     # Import Libraries
     import os
     import numpy as np
     import shap
     from sklearn.datasets import load_diabetes
     from sklearn.linear_model import LinearRegression
     import mlflow
     from mlflow.artifacts import download_artifacts
     from mlflow.tracking import MlflowClient
     # Prepare the Training Data
     X, y = load_diabetes(return_X_y=True, as_frame=True)
     X = X.iloc[:50, :4]
     y = y.iloc[:50]
     # Train a model
     model = LinearRegression()
, y)
     # Log an explanation
     with mlflow.start_run() as run:
         mlflow.shap.log_explanation(model.predict, X)
     # List Artifacts
     client = MlflowClient()
     artifact_path = "model_explanations_shap"
     artifacts = [x.path for x in client.list_artifacts(, artifact_path)]
     print("# artifacts:")
     # Load the logged explanation
     dst_path = download_artifacts(, artifact_path=artifact_path)
     base_values = np.load(os.path.join(dst_path, "base_values.npy"))
     shap_values = np.load(os.path.join(dst_path, "shap_values.npy"))
     # Show a Force Plot
     shap.force_plot(float(base_values), shap_values[0, :], X.iloc[0, :], matplotlib=True)

    Save and close the file.

The above Python application imports all necessary libraries and uses the diabetes dataset for training data. The dataset contains a total of 442 samples. Then, the model is trained and an explanation is logged to MLflow using SHAP (Shapley Additive Explanations) that plots the output for a visual representation of the data.

Test the Mlflow Experiement

  1. Run the Python application

     $ python3
  2. Using a web browser such as Chrome, visit your Mlflow external IP address

Verify that the application is included in your MLflow experiments. It uses a linear regression statistical approach to model a relationship between a scalar response and one or more variables.

Mlflow UI showing the diabetes linear regression experiment


You have deployed MLflow on a Vultr Kubernetes Engine (VKE) cluster. To test the service operations, implement more examples from the MLflow repository. For more information about MLflow, visit the following documentation resources:

Next Steps

To implement more solutions, visit the following resources:

View original

#kubernetes #ml #vultr