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
rootdirectory of the project contains the following filesml-app-k8sfolder that contains all the spring boot code basechartsfolder contains helm chart files.templatesfolder contains the following:deployment.yamlcontains K8s deployment specifics and env level variables.hpa.yamlis used to specify auto-pod scaling in the clusterNetworkPolicy.yamlfile manages external access to the services in a cluster, typically HTTP.pdb.yamlfile is used to manage Pod Distribution Budget that the cluster will honorservice.yamlspecifies service management like the loadbalancer with AWScronjob.yamlCronJob manifest prints the current time and a simple hello message every 5 minute
chart.yamlis used to specify app-pod level information that is used throughout the K8s configvalues.yamlis used to handle app information and specifics. we can usevalues/dev.yamlto override these values and use them for each environment.
corefolder is used to importing the packages we need, as well as the classes, which will be used to train our modelsdatafolder is used to contain the datasets based out of Auto MPG dataset which is used for deploying a machine learning modelmodelsfolder contains the model which is dumped and exported to a file, that we can load into Python at another point in timemain.pycontains the code to expose the model as a servicetrain.pycontains the code to train our model by specifying which feature we want to predictDockerfileis 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.