A sample ML app that trains a model and we deploy it to k8s
The project contains a ml app
Machine Learning application, and the docker configuration to run it
locally or deploy it on k8s platform.
- The
root
directory of the project contains the following filesml-app-k8s
folder that contains all the spring boot code basecharts
folder contains helm chart files.templates
folder contains the following:deployment.yaml
contains K8s deployment specifics and env level variables.hpa.yaml
is used to specify auto-pod scaling in the clusterNetworkPolicy.yaml
file manages external access to the services in a cluster, typically HTTP.pdb.yaml
file is used to manage Pod Distribution Budget that the cluster will honorservice.yaml
specifies service management like the loadbalancer with AWScronjob.yaml
CronJob manifest prints the current time and a simple hello message every 5 minute
chart.yaml
is used to specify app-pod level information that is used throughout the K8s configvalues.yaml
is used to handle app information and specifics. we can usevalues/dev.yaml
to override these values and use them for each environment.
core
folder is used to importing the packages we need, as well as the classes, which will be used to train our modelsdata
folder is used to contain the datasets based out of Auto MPG dataset which is used for deploying a machine learning modelmodels
folder contains the model which is dumped and exported to a file, that we can load into Python at another point in timemain.py
contains the code to expose the model as a servicetrain.py
contains the code to train our model by specifying which feature we want to predictDockerfile
is the dockerfile used to build the image and/or run the app
To run this project locally, please cd
to the location this template is present and run the
following:
docker docker build --tag ml-app .
docker run -p 8080:8080 -itd ml-app:latest
deploy the application to your kubernetes cluster using HELM.
kubectl config set-context `clusterName` --namespace `namespace`
cd chart/ml-app
helm upgrade ml-app .
now lets fetch the external or internal endpoint of the service that is deployed
kubectl get svc
We have the external-ip from earlier, which we are going to reuse here. The following JSON request can now be sent as with a HTTP POST method, and we will receive the expected response.
{
"cylinders": 8,
"displacement": 307.0,
"horsepower": 130.0,
"weight": 3504,
"acceleration": 12.0,
"model_year": 70,
"origin": 1,
"car_name": "chevrolet chevelle malibu"
}
In just 0.29
seconds, we received the prediction of this car, and our machine learning model predicted 16.383.