Cloud and Container Integration Options for Operationalizing MATLAB Analytics | MathWorks Energy Conference 2022
From the series: MathWorks Energy Conference 2022
Discover how to run MATLAB® analytics in the Cloud to speed up computation and to deploy algorithms in a production environment. Watch a demonstration of a typical workflow through which you can access and preprocess data, develop predictive models, and integrate your analytics using common enterprise platforms. The presentation also covers reference architectures that can be used to integrate with Microsoft® Azure®, AWS®, and Kubernetes® and shows how to integrate with operational systems such as AVEVA® PI, Apache® Kafka®, and Azure Event Hub.
Published: 22 Mar 2023
Hello, everyone. My name is Mil Shastri. I'm a senior application engineer at MathWorks. And today, we'll be talking about how you can deploy your algorithms to the cloud and containers using MATLAB. So a typical MATLAB based workflow would look something like this. Starting from the left, you access your data-- could be from various sources.
And then you'd quickly go into building a model out of it or a simulation or a test. And once that's done, you'd like to deploy this and integrate this. So we'll start off with how you can access your data, particularly from PI historians, in this case an OSIsoft or AVEVA PI historian. And then, we'll see how you can deploy this into Docker-based microservices, as well as cloud-based applications.
So to set the big picture, we'll see how you can access your data primarily from historians, as well as a variety of other sources. We'll then jump past few steps of algorithm creation, and we'll see how you can primarily deploy the two options that MATLAB provides, one being container based microservices, and two, cloud based applications. Finally, we'll look at some helpful resources to refer back to on the topics that we discussed today.
So to start things off, the first thing you need is to access your data. And your data could reside in any of these three sources. You could have operational data, which is typically you could have data in a PI historian, or any kind of other historians. You could have your data and data lakes. So this is typically stored in AWS or Azure or somewhere on the cloud. As well as you could have data streaming directly from sensors, like raw vibrational data or any other data from your assets.
So let's assume the data you need to develop and test your algorithm is collected and stored in a plant historian. And this could be AVEVA PI, or other historians like Factory Talk, Emerson DeltaV, and Honeywell. And the PI historian can be accessed through a special interface built by MathWorks.
Other historians can also be accessed from MATLAB through their industry standard OPC interfaces. So the Industrial Communication Toolbox provides a secure access to OPC UA servers, and also supports access to classic OPC DA and OPC HDA servers. In this presentation, we'll focus on the PI historian, and we'll show how you can get data from the PI historian into MATLAB for algorithm development and deployment.
So let's see how you can access data stored in a PI historian directly into MATLAB. The first thing we do is go ahead and run the script. To access data from the PI historian, you can use the piclient command. In this case, I'm pointing it to the local host, because I have API server installed on my local machine. But you can definitely point in, provide your authentication, your username, password, and location of the client in a remote location and directly access data from there.
Next thing, we'll go ahead and look through the tags that are available. As you can see here, with the command tags, we see a list of tags available. So we see there is vibration data, there is temperature data, concentration, a variety of data from different assets. All of these can be explored here. And really, you can get an idea of the different tags that are available.
Next, we'll go ahead and read these values. So we'll read values from two tags. The first tag we'll read from a temperature sensor. To do that, as you can see in line 7, you can go ahead and run the read command. You provide the client, the name of the tag, and a date range.
And right there, you're able to immediately pull in data from the PI server for that specific tag. You can see this is time series data with some timestamps, the name of the tag, and the value and some additional values. Next, we'll go ahead and read another set of data from another tag.
And the reason we're doing this is downstream we'll combine this data together and see how easy it is to perform some operations in MATLAB. So you can see here, again, we've read we've read data from another tag, with some timestamps. It has the name of the tag, some values.
And combining all of this information within MATLAB is fairly simple, right? You can use the synchronize command to go ahead and read your data, to synchronize both these timestamps, these data sets together. The synchronize command really combines both of these two data sets together and synchronizes their timestamps, so any kind of data that's subsampled in between gets interpolated, and you can specify all of this information downstream.
Now, alternatively, the pipeline also has a visual way of reading data. So you can see you can select your tags on the left, select the time range on top. And the pipeline has a simple visualization on the right that shows all your data quickly, as well as multiple ways to visualize your data set.
So we just saw how you could access data that resides on a PI server. Now, with the Industrial Communications Toolbox, you can also access data that resides on OPC UA servers. And devices such as PLCs typically hosts such servers. So you can definitely communicate with them. In fact, any device that supports Modbus communication is also supported. And you can also subscribe and publish to MQTT topics directly from MATLAB using the Industrial Communications Toolbox.
Now, alternatively, if your data is residing on-- to access raw sensor data, you can use the Data Acquisition Toolbox. It enables you to communicate directly with your sensors and instruments connected to these sensors.
Now, in addition to the data sources that we just looked at, MathWorks also provides a range of connectors to a variety of other data sources, like there's documentation for connecting with AWS S3 brackets, Azure BLOB storage, data lakes. And even in the right areas, there are connectors and examples and documentation for communicating with big data repositories, such as Spark, Databricks, Domino Data Labs, Google BigQuery, and a variety of other sources.
So we looked at how you could access your data from a few sources. Now, let's just jump ahead a few steps. Let's assume you've already built your algorithm. Now you'd like to deploy these to various target platforms. So particularly, we'll see two possibilities, how you can deploy your algorithms to containers, deploy them to Docker container microservices, as well as to the cloud as applications using MATLAB Production Server.
So let's focus on the option of deploying the algorithm as a microservice. And let's talk about what the microservice architecture is. So this microservice architecture is a popular architectural pattern where an application is broken down into smaller self-contained microservices, each of which perform a very specific function.
Now, each microservice is completely independent and oftentimes written in different languages. So in this case, there's a microservice written in MATLAB communicating with another microservice in Java, communicating with another microservice in Python, all through an API gateway. So let's see how we can deploy a MATLAB algorithm as a microservice.
So the first thing we do is let's assume you have a trained model that reads vibration data from your sensors, and then it generates some outputs, like the forecasted value of the vibration, a state of health of the machine, whether it's healthy or not healthy, Some kind of estimate about that. And also, an estimate of the remaining useful life left on that asset of yours.
Now, this module is being called in a particular function that we have listed that's called check thresholds. And we'll see how you can deploy this as a microservice. And the first thing to do is to create a production server archive.
To do that, we'll run compiler or build or production server archive options, and provide a name of the function that we'd like to convert into an archive. We'll provide the name of the archive, so we call it generator, and some additional options like automatically detect files and pull them in and Once we've created this options object, we'll pass it to compiler or build or production server archive.
And this function is going to go ahead and generate a CTF. A CTF is a combined technology file that can be called by MATLAB Production Server to deploy your algorithm as an application on the cloud, as well as the same file will be used to generate a microservice.
So once the CTF has generated, now we're ready to take the CTF and create a microservice from it. And to do so, we're going to use the compiler.package.microserviceDockerImage function, where we provide the CTF object, as well as a name of the image and hit Run.
Now, this is going to go ahead and package the original algorithm as a microservice Docker image. It's packaging the runtime, and only looking at the relevant components of the runtime, in spirit of keeping the Docker container compact and lightweight. That command also generates right at the bottom a GettingStarted.txt.
And this getting started guide has information of how you can now use the microservice-- now use the Docker image to fire up the microservice. So let's open up this GettingStarted.txt to run this microservice image. It has an example command. You can use a WSL command, WSL Docker command to run it on a Windows machine.
It also provides the name of the archive and a mapping for the port. So there's additional information also where you can check the status of the service, provides a URL where you can fire it up on a web browser and see the health of the microservice. So we'll go ahead and copy this run command, fire up this microservice.
So let's open up the command prompt in Windows. And let's just pace this command. Now, you can change the port mapping however you would like. 9900 was just an initial guess that the documentation provides, but you can map it to any port that you'd like.
Now, in the second window, I have already fired up the microservice. I've already mapped it to port 99999. And as you can see, the microservice is ready and it's waiting to be queried by any other application. So we'll see, I've created this dashboard that's querying the same microservice. As you can see, it's the reading the results from the microservice, which is an estimate on the bearing frequency, some statistical measure on the peak frequency.
So running this for two assets. It also shows below that a threshold status or an estimate of what the health is looking like, and an estimate on the remaining useful life or the time to failure. So anyone who is monitoring this asset now can understand whether the asset is actually heading towards a catastrophic failure and take action way before any of that happens.
Now, microservices are just one way of deploying your MathWorks software into containers. We also let you package your code as a standalone application container, for if you'd like to run it in a batch or scheduled fashion.
Now, alternatively, you might also want to containerize MathWorks own software, typically for continuous integration or to run in Kubernetes. And MathWorks supports the ability to package softwares like MATLAB or Simulink or MATLAB Online Server, Polyspace, or even MATLAB Production Server into containers.
So for example, this is what we deploy when you run our Kubernetes helm chart. Now, we don't have time to cover all of these options right now. So if you have any questions, you're interested in this space, definitely reach out to us and we'll be more than happy to discuss this further.
We looked at how you could deploy your algorithms to a Docker container based microservice. Now let's look at an option by which you can deploy or your algorithms, let's say, your analytics model, onto the cloud. To do that, we provide a variety of cloud reference architecture templates. And the process is really simple.
You go ahead and fill out a form, hit create and submit, and this will take about 10 to 30 minutes to fire up the complete stack on AWS, Azure, or Google Cloud, wherever that is. And then all you have to do is go ahead and upload your license file, upload your model, and you're good to go.
So let's take an example to see how you can deploy your MATLAB algorithm onto MATLAB Production Server using the reference architectures that MathWorks provide. Now, MathWorks provides a variety of reference architectures for its cloud based products and they're hosted on GitHub.
So in this case, we'll particularly look at MATLAB Production Server on Azure. If you click on that, this opens up the reference architecture for Production Server on Azure. And if you go right to the bottom, there is an network architecture diagram that describes the whole stack. Now, this is built using best principles. But you are absolutely welcome to modify this template.
So to launch this template, let's go right on top to the first step. And you can launch the template using this deploy to Azure button. This would open up your Azure portal. You'd have to ensure that you have your account and access to the Azure portal. But once you ensure that's there, it opens up a page where you fill out a form.
So you can specify a resource group. You can either create a new one or use an existing one. You can specify the VM instances, the type of machines that you'd like to deploy on this stack. You can provide your admin username, as well as a password. There's some recommendations on what the password should look like.
Then you can also configure which IP addresses you'd like to allow connections from. So in this case, I've just given my own IP address. So you can also optionally provide, if you'd like to create a Redis cache or not, the stack supports Redis caches. You can specify the kind of platform, whether a Linux or a Windows platform.
Now, if you have an existing license manager, you don't have to deploy that. But if you're creating a brand new stack without a license manager, you can also create a license manager for it. Specify the kind of certificate you'd like.
So since I was providing certificate, provide the Base64 encoded certificate data and password, and then hit Review. So now you get an option to review all the information that you provided. And if everything looks good, hit Create.
Now, this typically would take about 10 to 30 minutes, depending on the stack, but the reference architecture provides some convenient outputs. So to access these outputs, once the deployment is complete you go into the outputs tab on the right. And first thing we'll do is open up the network license manager, the third URL.
We can copy this go onto a browser page. And then log in with the username and password. And this would be the first step to firing up our algorithm on the production server stack. So we'll go into this Administration tab, click Manage Licenses, and upload a license file.
This license file is something that will be provided by a licensed manager, and it's going to typically be a .lic file. Once that is uploaded, we are ready, we are good to go from the license point of view.
Next, we'll go ahead and open up the MATLAB Production Server Admin Dashboard, so you can copy the link. Paste it in a browser. And provide the username and password that we had provided originally. Now, this opens up the MATLAB Production Server Dashboard page.
It shows the URL endpoint that you can use to access the algorithms hosted here talks, about the status of Production Server. And then if you go into the Applications tab, you can upload the CTF file or the application. Now, remember we generated a CTF file while creating a microservice of our algorithm. We'll use that same CTF file to upload onto MATLAB Production Server and deploy it.
So I picked that generator.ctf file, can go ahead and upload this. Now, in this case, it's a large file. So I've already uploaded it. And as you can see here right below, you can see this application's already been uploaded. It has a base URL which can be appended on to the MATLAB Production Server endpoint.
Now, I go into the back end of the dashboard that we built. And instead of calling the microservice, I'd replace the URL of the microservice with the endpoint of the MATLAB Production Server application. So you can see, it's the same Production Server algorithm hosted on an Azure. They give it a new name, enabled TLS security. And then save this. And just trigger this algorithm.
Now, if we go back into the dashboard, this is the same algorithm that the dashboard that was querying the microservice, which is now querying the algorithm running on MATLAB Production Server. So you could see how easily we could switch between the same algorithm that was built in MATLAB from a microservice, from being hosted on a microservice, to now being hosted on the cloud using MATLAB Production Server.
Now, in addition to MATLAB Production Server, there are multiple other ways that you can use to operationalize your analytics on the cloud. We provide you with some of these architectural patterns, and I've included a link here that really takes you to a page that looks at a variety of options that you can leverage. So there's web app server, production server, MATLAB Online Server, a variety of other possibilities.
And to conclude, what we looked at today was how you could access your data, particularly for access data from the PI historian, as well as a variety of other options using the Industrial Communication Toolbox and the Data Acquisition Toolbox.
We also saw how you could deploy your algorithms on to containers. So there's a whole resource, a link I've provided, a section with a variety of links. We also saw how you could deploy your algorithms on microservices. So I've included a few more links that talk about microservices resources. And finally, a variety of resources for the cloud, particularly the reference architecture that MathWorks provides on the cloud.
So with that, I thank you all for your time, and it was a pleasure presenting to you all. And if you'd like to discuss any of these possibilities or any other-- you have any other applications in mind, please feel free to reach out to us.