Train and Deploy on GCP from a Local Notebook

Use Kubeflow Fairing to train and deploy a model on Google Cloud Platform (GCP) from a local notebook.

This guide introduces you to using Kubeflow Fairing to train and deploy amodel to Kubeflow on Google Kubernetes Engine (GKE), and Google Cloud ML Engine.As an example, this guide uses a local notebook to demonstrate how to:

  • Train an XGBoost model in a local notebook,
  • Use Kubeflow Fairing to train an XGBoost model remotely on Kubeflow,
  • Use Kubeflow Fairing to train an XGBoost model remotely on Cloud ML Engine,
  • Use Kubeflow Fairing to deploy a trained model to Kubeflow, and
  • Call the deployed endpoint for predictions.

This guide has been tested on Linux and Mac OS X. Currently, this guide has not beentested on Windows.

Clone the Kubeflow Fairing repository

Clone the Kubeflow Fairing repository to download the files used in this example.

  1. git clone https://github.com/kubeflow/fairing
  2. cd fairing

Set up Python, Jupyter Notebook, and Kubeflow Fairing

  • You need Python 3.6 or later to use Kubeflow Fairing. To check ifyou have Python 3.6 or later installed, run the following command:
  1. python3 -V

The response should be something like this:

  1. Python 3.6.5

If you do not have Python 3.6 or later, you can downloadPython from the Python SoftwareFoundation.

  • Use virtualenv to create a virtual environment to install KubeflowFairing in. To check if you have virtualenv installed, run thefollowing command:
  1. which virtualenv

The response should be something like this.

  1. /usr/bin/virtualenv

If you do not have virtualenv, use pip3 to install virtualenv.

  1. pip3 install --upgrade virtualenv

Create a new virtual environment, and activate it.

  1. virtualenv venv --python=python3
  2. source venv/bin/activate
  • Install Jupyter Notebook.
  1. pip3 install --upgrade jupyter
  • Install Kubeflow Fairing from the cloned repository.
  1. pip3 install --upgrade .
  • Install the Python dependencies for the XGBoost demo notebook.
  1. pip3 install -r examples/prediction/requirements.txt

Install and configure the Google Cloud SDK

In order to use Kubeflow Fairing to train or deploy to Kubeflow on GKE,or Cloud Machine Learning Engine, you must configureyour development environment with access to GCP.

  • If you do not have the Cloud SDK installed, install theCloud SDK.

  • Use gcloud to set a default project.

  1. export PROJECT_ID=<your-project-id>
  2. gcloud config set project ${PROJECT_ID}
  • Kubeflow Fairing needs a service account to make API calls to GCP. Therecommended way to provide Fairing with access to thisservice account is to set the GOOGLE_APPLICATION_CREDENTIALS environmentvariable. To check for the GOOGLE_APPLICATION_CREDENTIALS environmentvariable, run the following command:
  1. ls "${GOOGLE_APPLICATION_CREDENTIALS}"

The response should be something like this:

  1. /.../.../key.json

If you do not have a service account, then create one and grant itaccess to the required roles.

  1. export SA_NAME=<your-sa-name>
  2. gcloud iam service-accounts create ${SA_NAME}
  3. gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  4. --member serviceAccount:${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com \
  5. --role 'roles/editor'

Create a key for your service account.

  1. gcloud iam service-accounts keys create ~/key.json \
  2. --iam-account ${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com

Create the GOOGLE_APPLICATION_CREDENTIALS environment variable.

  1. export GOOGLE_APPLICATION_CREDENTIALS=~/key.json

Set up Docker

You need to have Docker installed to use Kubeflow Fairing. Fairing packagesyour code as a Docker image and executes it in the remote cluster. To checkif your local Docker daemon is running, run the following command:

  1. docker ps

Authorize Docker to access your GCP Container Registry.

  1. gcloud auth configure-docker

Set up Kubeflow

Use the following instructions to set up and configure your Kubeflow anddevelopment environments for training and prediction from Kubeflow Fairing.

  • If you do not have a Kubeflow environment, follow the guide to deployingKubeflow on GKE to set up your Kubeflow environment.The guide provides two options for setting up your environment:

  • Update your kubeconfig with appropriate credentials and endpointinformation for your Kubeflow cluster. To find yourcluster’s name, run the following command to list the clusters in yourproject:
  1. gcloud container clusters list

Update the following command with your cluster’s name and GCP zone, thenrun the command to update your kubeconfig to provide it with credentialsto access this Kubeflow cluster.

  1. export CLUSTER_NAME=kubeflow
  2. export ZONE=us-central1-a
  3. gcloud container clusters get-credentials ${CLUSTER_NAME} --region ${ZONE}

Use Kubeflow Fairing to train a model locally and on GCP

  • Launch the XGBoost quickstart in a local Jupyter notebook.
  1. jupyter notebook examples/prediction/xgboost-high-level-apis.ipynb
  • Follow the instructions in the notebook to train a model locally, onKubeflow, and on Cloud ML Engine. Then deploy the trained modelto Kubeflow for predictions and send requests to the prediction endpoint.

Feedback

Was this page helpful?

Glad to hear it! Please tell us how we can improve.

Sorry to hear that. Please tell us how we can improve.

Last modified 02.06.2019: Adds Fairing guides to install and configure (#718) (8b31fb06)