Run Microservice Created Using MATLAB Compiler SDK on Google Cloud
This example shows how to create a microservice Docker® image and deploy it on Google® Cloud. The microservice image created by MATLAB® Compiler SDK™ provides an HTTP/HTTPS endpoint to access MATLAB code.
You package a MATLAB function into a deployable archive, and then create a Docker image that contains the archive and a minimal MATLAB Runtime package. You can then run the image in Docker and make calls to the service using any of the MATLAB Production Server™ client APIs.
Prerequisites
Verify that you have MATLAB Compiler SDK installed on the development machine.
Verify that you have Docker installed and configured on the development machine by typing
[~,msg] = system('docker version')
in a MATLAB command window. If you are using WSL, use the command[~,msg] = system('wsl docker version')
instead.If you do not have Docker installed, follow the instructions on the Docker website to install and set up Docker. For details, see
docs.docker.com/engine/install/
.To build microservice images on Windows®, you must install either Docker Desktop or Docker on Windows Subsystem for Linux® v2 (WSL2). To install Docker Desktop, see
docs.docker.com/desktop/install/windows-install/
. For instructions on how to install Docker on WSL2, seehttps://www.mathworks.com/matlabcentral/answers/1758410-how-do-i-install-docker-on-wsl2
.
Create MATLAB Function
Create a MATLAB function from the MATLAB desktop. For this example, write a function named simpInterest.m
using the following code.
function i = simpInterest(p,r,t) i = p * (1 + (r * t)) - p; end
Create Deployable Archive
Package the simpInterest.m
function into a deployable archive using the compiler.build.productionServerArchive
function. Save the build information as the compiler.build.Results
object results
.
results = compiler.build.productionServerArchive("simpInterest.m","ArchiveName","financetools","Verbose","on")
Package Deployable Archive into Microservice Docker Image
Package the deployable archive into a microservice Docker image using the results
object.
compiler.package.microserviceDockerImage(results,"ImageName","financetools")
The function generates the following files within a folder named financetoolsmicroserviceDockerImage
:
applicationFilesForMATLABCompiler/financetools.ctf
— Deployable archive fileDockerfile
— Docker file that specifies run-time optionsGettingStarted.txt
— Text file that contains deployment information
Verify that the financetools
image has been created by typing docker images
.
For details, see Create Microservice Docker Image.
Create Google Cloud Artifact Registry Repository
Go to your Google Cloud Dashboard and search for Artifact Registry
in the search bar. Select the Artifact Registry service from the results. Click Create Repository and fill out the settings using the following information.
Name — Give it a name, it does not need to match the name of your image.
Format — Docker.
Mode — Standard.
Location Type — Select as appropriate.
Encryption — Select as appropriate. This example uses the
Google managed encryption key
option.
Click Create
to create your repository. In this example, the repository is named matlabservice
.
Upload Microservice Docker Image to Google Cloud Artifact Registry
Navigate to your newly created repository and click Setup Instructions. A command to configure Docker is displayed in the window that opens. Copy the command and run it in a command window. The command should resemble the following.
gcloud auth configure-docker us-east1-docker.pkg.dev
Return to the Google Cloud Artifact Registry. While you are in the Repository Details page, locate the repository path located above Repository Details. Copy the full path of the repository. The path should resemble the following.
us-east1-docker.pkg.dev > voltaic-flag-382813 > matlabservice
Tag the microservice Docker image with the copied container repository path using the docker tag
command. For example:
docker tag financetools us-east1-docker.pkg.dev/voltaic-flag-382813/matlabservice/financetools:1.0
Upload the microservice Docker image to your repository using the same repository path. For example:
docker push us-east1-docker.pkg.dev/voltaic-flag-382813/matlabservice/financetools:1.0
This takes a few minutes as the microservice Docker image is uploaded to the cloud. When complete, your Docker image is visible as a repository in your Google Cloud Artifact Registry.
Run Microservice Docker Image in Google Cloud Run
Go to your Google Cloud Dashboard and search for Google Cloud Run
in the search bar. Click Create Service to create a new service. Select Deploy one revision from an existing container image and in the dialog box, select the financetools
container image you uploaded.
Fill the form using the following information:
Service name — financetools
Region — Select the appropriate region
CPU Allocation and Pricing — Use default
Autoscaling > Minimum number of instances — Select appropriately
Autoscaling > Maximum number of instances — Select appropriately
Ingress — Select All to allow direct access to your service from the internet
Authentication — Allow unauthenticated invocations
Container:
Container Port: 9910
Memory: 2 GiB
CPU: 1
Execution environment: Default
Click Create to deploy the service. You can make requests to the image using the URL specified on the Service details page.
Make Request to Microservice Running in Google Cloud Run
Make a request to the microservice using command line tool or UI such as Postman. Pass in the three input variables for principal, interest, and time in JSON format.
POST /financetools/simpInterest HTTP/1.1 Host: https://4kxqyz9md2.us-east-1.awsapprunner.com/financetools/simpInterest Content-Type: application/json Content-Length: 42 {"nargout": 1, "rhs": [21000, 0.043, 12] }
The format of the host URI is Default_domain/container_image_name/matlab_function_name
.
Make a request using curl (replace with your own default domain prefix).
curl --location 'https://financetools-wuterchjka-uc.a.run.app/financetools/simpInterest' \ --header 'Content-Type: application/json' \ --data '{"nargout": 1, "rhs": [21000, 0.043, 12] }'
You receive the simple interest amount as a JSON formatted result.
{"lhs":[{"mwdata":[10836],"mwsize":[1,1],"mwtype":"double"}]}