Chapter 3. Install and authenticate the MLflow SDK
Install the MLflow SDK and configure authentication for your OpenShift cluster to track machine learning experiments in Red Hat OpenShift AI.
Prerequisites
- You have access to a OpenShift cluster.
-
You have installed the OpenShift CLI (
oc) and are able to access MLflow in your OpenShift AI project or MLflow workspace. - For the automated authentication method, you have a service account with the appropriate permissions or are running locally.
Procedure
- Install the MLflow SDK:
pip install "mlflow[kubernetes]>=3.11"
Authenticate the SDK by using one of the following methods:
To use automated authentication, enable the
kubernetes-namespacedclient-side authentication plugin.NoteThis plugin reads credentials from the mounted service account token when running in a pod, or from the active
kubeconfigcontext when running on a workstation. The MLflow workspace is automatically configured by reading either the service account’s namespace or activekubeconfignamespace.Enter the following commands:
export MLFLOW_TRACKING_URI="https://<dashboard-url>/mlflow" export MLFLOW_TRACKING_AUTH=kubernetes-namespacedTo authenticate manually, export your tracking token and project workspace as environment variables. Enter the following commands:
export MLFLOW_TRACKING_URI="https://<dashboard-url>/mlflow" export MLFLOW_TRACKING_TOKEN="$(oc whoami --show-token)" export MLFLOW_WORKSPACE="<project-name>"ImportantThis configuration example is not recommended for production environments.
If your OpenShift cluster does not use trusted TLS certificates, enter the following command to disable TLS verification:
export MLFLOW_TRACKING_INSECURE_TLS=true
Verification
Use the following command to verify connectivity python -c "import mlflow; print(mlflow.list_workspaces())" This command lists the workspaces you can access on the OpenShift cluster.
3.1. Configuring the MLflow SDK for a local workstation Copy linkLink copied to clipboard!
When you run the MLflow SDK on a local workstation, the authentication plugin uses the active kubeconfig context. The plugin uses the namespace from the kubeconfig context as the workspace and resolves the authentication token from your kubeconfig credentials, including exec-based authentication providers used by oc login command.
+ .Prerequisites
- You have installed the MLflow SDK.
- You have an active kubeconfig context.
Procedure
+ . Set the following environment variables to configure the tracking URI and authentication method:
+
export MLFLOW_TRACKING_URI="https://<dashboard-url>/mlflow"
export MLFLOW_TRACKING_AUTH=kubernetes-namespaced
Optional: If you prefer to set the token and workspace manually, export the following variables:
export MLFLOW_TRACKING_URI="https://<dashboard-url>/mlflow" export MLFLOW_TRACKING_TOKEN="$(oc whoami --show-token)" export MLFLOW_WORKSPACE="<project-name>"
3.2. Configuring MLflow SDK environment variables for pods Copy linkLink copied to clipboard!
+ When you run a pod, the Kubernetes authentication plugin uses the mounted service account token and namespace to automatically set the workspace.
You do not have to call mlflow.set_workspace() when you enable the authentication plugin. The plugin derives the workspace from the pod’s service account namespace. You can override the workspace explicitly if you need to target a different project, provided that your service account has the necessary RBAC permissions in that project.
Prerequisites
You have installed the MLflow SDK.
Procedure
Set the following environment variables in your pod configuration:
export MLFLOW_TRACKING_URI="https://<dashboard-url>/mlflow" export MLFLOW_TRACKING_AUTH=kubernetes-namespaced
Verification
Run a Python script using the following code to confirm that the MLflow SDK successfully connects to the tracking server and logs data:
import mlflow mlflow.set_experiment("demo-experiment") with mlflow.start_run(): mlflow.log_param("framework", "pytorch") mlflow.log_metric("accuracy", 0.95)
3.3. Upstream MLflow SDK reference Copy linkLink copied to clipboard!
Additional resources
For more information about the upstream MLflow SDK, see the following resources:
3.4. MLflow SDK troubleshooting reference Copy linkLink copied to clipboard!
If you encounter errors when working with experiments or artifacts in the MLflow SDK, use the following information to resolve common issues and error messages.
If your issue is not described here, contact Red Hat support.
Common issues
- 403 or permission denied
- Problem
- The active project is missing the required role-based access control (RBAC) permissions.
- Resolution
- Verify that your user or service account has the necessary role binding in the active project.
- Workspace not found
Problem
The SDK cannot locate the workspace because the project name is incorrect, the namespace is filtered, or no workspace was selected.Resolution
Verify that your project name is correct and that the namespace is not restricted. Ensure that you have selected a workspace in your MLflow environment settings by using one of the following methods: * TheMLFLOW_TRACKING_AUTH=kubernetes-namespacedenvironment variable. * TheMLFLOW_WORKSPACE=<workspace_name>environment variable. * Themlflow.set_workspace("team-a")Python function.- Artifact override is not applied
- Problem
-
The
MLflowConfigresource is missing, has the wrong name, or exists in the wrong project. Alternatively, the associated secret does not exist. - Resolution
-
Ensure that an
MLflowConfigresource exists and is namedmlflow. Verify that themlflow-artifact-connectionsecret is present in the namespace.
- Kubernetes-namespaced authentication plugin cannot resolve credentials
- Problem
-
The authentication plugin is missing a service account token (when running in-cluster) or an active
kubeconfigcontext (when running locally). - Resolution
-
If running in-cluster, ensure the service account has a valid token. If running locally, verify that your
kubeconfigcontext is active and points to the correct cluster and project by runningoc project.
- Artifact writes go to the default storage location
- Problem
-
The
MLflowConfigresource does not exist in the active project, or theartifactRootSecretis invalid. - Resolution
-
Create the
MLflowConfigresource in your active project and verify that theartifactRootSecretcontains the correct connection credentials.
- JSON decode error: Expecting value
- Problem
- The Red Hat OpenShift AI authentication token is missing or invalid. Consequently, OpenShift AI receives an HTML response from MLflow instead of the expected JSON, causing OpenShift AI to prompt for a login.
- Resolution
-
Log in to the OpenShift cluster by running the
oc logincommand. Ensure that your environment uses theMLFLOW_TRACKING_AUTH=kubernetes-namespacedenvironment variable to authenticate requests.
3.5. MLflow version compatibility Copy linkLink copied to clipboard!
The following information describes the compatible versions of MLflow and Red Hat OpenShift AI 3.4 GA.
| Item | Description |
|---|---|
| Deployed MLflow server version |
|
| Required MLflow SDK version | 3.11 or later |
| Authentication plugin name |
|
| Environment variable |
|
The following command installs the compatible version of the MLflow SDK:
+
pip install "mlflow[kubernetes]>=3.11"
+
MLflow SDK version 3.11 and later includes the kubernetes-namespaced authentication plugin by default.
3.6. MLflow storage and database compatibility Copy linkLink copied to clipboard!
The following table lists the MLflow storage and database configurations. The configuration settings vary according to your environment, for example production, development, or testing.
| Storage area | Supported options |
|---|---|
| Artifact storage | S3 compatible object storage for production. File system for development and testing. |
| Database | PostgreSQL for production. SQLite for development and testing. |
| Artifact repository plugins | S3 and file. |
3.7. Tracking experiments with MLflow SDK Copy linkLink copied to clipboard!
Use the MLflow software development kit (SDK) to log and track machine learning experiments. With the SDK, you can record parameters, metrics, and artifacts to a centralized tracking server for later analysis.
Prerequisites
- You have installed the MLflow SDK version 3.11 or later.
- You have access to a Red Hat OpenShift AI data science project that has MLflow permissions configured.
You have configured a tracking URI and authentication. For more information, see MLflow Kubernetes Authentication. .Procedure
In your notebook, log experiments, parameters, metrics, and artifacts by using the MLflow SDK:
When the kubernetes-namespaced authentication plugin is configured, the tracking URI and workspace are resolved automatically.
import random import time import mlflow mlflow.set_experiment("demo-experiment") with mlflow.start_run(run_name="demo-run"): mlflow.log_param("model_type", "baseline") mlflow.log_param("feature_count", 3) for step in range(5): mlflow.log_metric("accuracy", 0.8 + random.random() * 0.2, step=step) mlflow.log_metric("loss", 0.5 - random.random() * 0.2, step=step) time.sleep(0.2)Optional: To target a project other than the one associated with your credentials, set the workspace explicitly:
mlflow.set_workspace("<project-name>")
Troubleshooting
If you encounter errors when using the MLflow SDK to manage experiments or artifacts, see Troubleshooting MLflow SDK.
3.8. Configure project-specific S3 artifact storage Copy linkLink copied to clipboard!
By default, all projects use the artifact storage configured in the MLflow resource. You can override the artifact storage for a specific project by creating an MLflowConfig resource and a connection in that project. After you create these resources, MLflow resolves the artifact root from the override for any new experiments and runs that you create in that project. However, MLflow does not serve artifacts when you configure a per-project override. The client accesses the S3 bucket directly, so the client must have valid S3 credentials.
The MLflowConfig resource is namespace-scoped and must be named mlflow. It points to an S3 compatible object storage connection that holds the credentials and bucket information for the project.
Prerequisites
- You have installed MLflow.
- You have an S3-compatible object storage bucket with credentials.
Procedure
In your project, add a connection of type S3 compatible object storage. Set the resource name of the connection to
mlflow-artifact-connection. Provide the following connection details:- Access key: The S3 access key ID.
- Secret key: The S3 secret access key.
- Endpoint: The S3 compatible endpoint URL.
- Region: The S3 region. Optional if your storage provider does not require it.
- Bucket: The S3 bucket name.
If you are using the connections API instead of the dashboard, create a
Secretwith the annotationopendatahub.io/connection-type-protocol: "s3"and name itmlflow-artifact-connection. Setting anAWS_DEFAULT_REGIONis optional. The required keys are:-
AWS_ACCESS_KEY_ID -
AWS_SECRET_ACCESS_KEY -
AWS_S3_BUCKET -
AWS_S3_ENDPOINT
Create an
MLflowConfigresource namedmlflowin the same project:apiVersion: mlflow.kubeflow.org/v1 kind: MLflowConfig metadata: name: mlflow spec: artifactRootSecret: mlflow-artifact-connection artifactRootPath: mlflow-artifactsartifactRootSecretmust bemlflow-artifact-connection. The Custom Resource Definition (CRD) enforces this validation.NoteartifactRootPathis an optional relative path that the system appends to the bucket root from the secret. For example, if the bucket isds-team-bucketandartifactRootPathismlflow-artifacts, the resolved artifact root becomess3://ds-team-bucket/mlflow-artifacts. The path must be relative, must not use backslashes, and must not contain path traversal such as...