Use the Serverless Function Method to Build a Machine Learning Microservice System
💡 本文同步發表於 Medium:原文連結
https://medium.com/@simon3458/use-serverless-function-method-to-build-a-ml-microservice-system-a108f3f2c1c
點擊查看連結詳情...
Use the Serverless Function Method to Build a Machine Learning Microservice System

Introduction
In our previous experience in machine learning deployment service, we write the business and model logic functions into one service. We found that it is hard to maintain the service. If we need to deploy the new service, we must restart all services and cause downtime. Therefore, monolithic systems fail to meet the mark in the current era of containerization and cloud computing.
Microservice architecture can help us to solve the single service problem. It is a software development style that breaks into smaller independent components. It is also a collection of autonomous services designed around a single business domain. These individual units can handle one part of the software's functionality.
The benefit of this is:
- In the event of a bottleneck, the individual services can be scaled at any time
- They are easier to maintain than a monolithic codebase
- Each module developer can use their own coding style
In this showcase, we will try using the function-as-a-service(FAAS) open source platform OpenFAAS and our PrimeHub deployments to build a microservice system. If you are using cloud service to build up your machine learning system, you can use the serviceless cloud function, like the GCP cloud function or AWS lambda, as your application microservice. They provide the same method as OpenFAAS and use more accessible deployment methods.
Architecture
When we finish training the machine learning model, we will use the docker image to build a machine learning RestAPI service. The input data of the machine learning RestAPI service is the same as our model, and the output data is our model output value. After we finish deploying the model service, we can try to write the application service to let our user use the model service in web UI or other application services.

Machine Learning Microservice System Architecture
This method is easier to maintain:
- we can reuse the model service and do not need to write the model logic again because we previously defined the model service input and output.
- We can use the rolling update method to start the new model service if we need to renew the model. It does not require downtime and lets our service can be online continuously.
- If the model service usage is increased, we can add the number of replicas and makes our service high Availability.
Showcase
We use the MNIST image classification model as our model service to build our machine learning microservice system. The structure of the showcase is shown below, and we use two application services to show how to reuse the model service.

MNIST Image Classification Model Microservice System
Part 1: MNIST Model Service
We can use PrimeHub deployments to deploy the model. Create the new PrimeHub deployment and deploy our model docker image as the service.

The Table of Configuring the PrimeHub Deployments Method
After we start the model service, we can see the service's endpoint. We can use this RestAPI service to make our application.

The Resulting Snapshot of the PrimeHub Deployment Service
Part 2: OpenFaaS
We use OpenFaaS as our example. OpenFaaS (Functions as a Service) is a framework for building serverless function services with Docker and Kubernetes. Any process can be packaged as a function enabling you to consume a range of web events without repetitive boilerplate coding.
Home
Serverless Functions Made Simple with Kubernetes.

Before deploying OpenFaaS, we should provision a Kubernetes cluster. OpenFaaS should run on any compliant Kubernetes distribution.
OpenFaaS CE - OpenFaaS
OpenFaaS - Serverless Functions Made Simple
I use the helm chart to deploy the OpenFaaS platform. They provide the installation method in the Github document. It is easier to deploy the OpenFaaS platform into the Kubernetes system.
faas-netes/chart/openfaas/README.md at master · openfaas/faas-netes
Serverless Functions For Kubernetes. Contribute to openfaas/faas-netes development by creating an account on GitHub.
OpenFaaS provides a CLI method to initiate the project. You can use the following document to install faas-cli. We will use faas-cli to initial, build docker images, push docker images to Dockerhub and deploy docker images to the OpenFaaS platform.
Installation - OpenFaaS
OpenFaaS - Serverless Functions Made Simple
Part 3: Multiple Application Services
Log into OpenFAAS and Initial the OpenFAAS project:
$ git clone <https://github.com/LiuYuWei/openfaas-model-microservice>
$ cd openfaas-model-microservice
$ faas-cli login <openfaas-platform-url> -u <account> -p <password>
The code is available in the following GitHub repo. You can check the content in the following link. Remember to change the variable value in the YAML files to configure the setting.
GitHub - LiuYuWei/openfaas-model-microservice: Use openfaas to do the model microservice system
Use openfaas to do the model microservice system. Contribute to LiuYuWei/openfaas-model-microservice development by creating an account on GitHub.
After we finish configuring the setting, we can use the faas-cli tool to build, push, and deploy the application docker image as the application service.
- Login to the DockerHub:
docker login -u <docker-username> -p <docker-password>
- openfaas-service application service setting via faas-cli:
$ cd openfaas-service
$ faas-cli build -f application-service.yml
$ faas-cli push -f application-service.yml
$ faas-cli deploy -f application-service.yml
- openfaas-service-image application service setting via faas-cli:
$ cd openfaas-service-image
$ faas-cli build -f application-service-image.yml
$ faas-cli push -f application-service-image.yml
$ faas-cli deploy -f application-service-image.yml
After we deploy two application services into OpenFaaS, we can use the OpenFaaS web UI interface to do our testing. Although the input data type is different, we can get the same output result and verify whether the number is correct or not.


The Snapshot of OpenFaaS Service prediction.
Conclusion
In this case, you get two different kinds of business logic. First, the OpenFAAS service input data comes from the URL link and the second service input data comes from the list or numpy array. We use the same model as our ML service, and we can reuse the service. I think we can use this microservice structure to build our machine learning system.
I am Simon
Hi, I am Simon, the Customer Success Engineer at InfuseAI. If you think the article is helpful to you, please give me applause. Welcome to provide some suggestions and discuss with me in InfuseAI Discord.
Linkedin: <https://www.linkedin.com/in/simonliuyuwei/>
We are InfuseAI
InfuseAI provides MLOps tools:
- PrimeHub— Our end-to-end MLOps platform, is the entire machine learning lifecycle in one platform.
- Piperider— The data quality toolkit for data professionals.
Join our machine learning community to discover more: InfuseAI Discord

Join our Discord Community
這篇文章有幫上忙嗎?