Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts

Monday, July 13, 2020

Create and Deploy Azure IoT Edge Module on NVIDIA Jetson device and send direct messages to the module

Introduction

In this tutorial we will see how we can deploy an IoT Edge Module on NVidia Jetson Nano device and send direct message to the newly created module and get its response. This tutorial can also be used on any Linux VM that Azure IoT Edge runtime supports. Just deploy the Azure IoT Edge runtime and use that VM as your device.

Link to GitHub post

Prerequisites

  1. Azure IoT Hub. Steps to create Azure IoT Hub can be found here
  2. IoT Edge Device registered. Details can be found here
  3. Docker Image repository: This will be used to push the container images to. This tutorial requires access to already created Docker Image Repository with user name and password. Details on how to create Azure Container Registry can be found here
  4. VS Code
  5. Azure IoT Tools for VS Code: This should configured and connected to your IoT Hub. More details can be found here
  6. Preconfigured Nvidia Jetson device with IoT Edge runtime installed and configured to use Azure IoT Edge. More details can be found here. Or Linux Virtual Machine with Azure IoT Edge runtime installed and configured. More details can be found here. In this case "Linux Virtual Machine" will act as a device.

Steps

Setting up the environment

Step 1. Create device identity as shown below:
az iot hub device-identity create --device-id [your-azure-iot-edge-device] --edge-enabled --hub-name [your-azure-iot-hub_name]
Step 2. On VS Code open command palette and enter command Azure IoT Edge: New IoT Edge solution.
Step 3. Choose the location for solution files.
Step 4. Choose name for solution. NvidiaJetsonEdgeSolution was selected for this tutorial.
Step 5. On "Select module template" question, choose "Python Module".
Step 6. Enter the name for "Python Module". For this tutorial "RequestProcessorModule" was chosen.
Step 7. For "Provide Docker image repository" question, choose a pre-existing image repository followed by name of your repository. Example: [your-docker-image-registry].azurecr.io/requestprocessormodule After the above step, VS Code will open a new window with the following view:
alt text
Step 8. Open the .env file and enter the user name and password for your docker image registry as shown below: alt text
Step 9. On VS Code open the command palette and enter command "Azure IoT Edge: Set Default Target Platform for Edge Solution".
Step 10. Select "arm64v8" or your correct architecture. You can find out the architecture of your device by running the following command on the device:
$ uname -m
aarch64
In this case "aarch64" corresponds to "arm64v8". Once the architecture is set the settings.json file would look like: alt text

Adding code

Step 1. Open "main.py"
Step 2 . Replace the code with the code mentioned below:

Deploy

Step 1. Right click "deployment.template.json and select "Build and Push IoT Edge Solution" as shown below: alt text
Step 2. The result of above step will be the creation of new folder called "config". The folder will contain a deployment json file corresponding to the default platform selected on step 8 under "Setting up the environment" section. For our Nvidia Jetson Nano device the architecture is arm64v8 as shown below:
alt text
Step 3. [Optional] Removing of SimulatedTemperatureSensor module. If you open the "deployment.arm64v8.json" file under config folder, you will notice that it comes with "RequestProcessorModule" and "SimulatedTemperatureSensor" module. This means if you deploy this deployment json file to the device, you will end up with additional SimulatedTemperatureSensor module. If you would like to not have that module added, then simple remove the section as shown below: alt text
Step 4. Open the "AZURE IOT HUB" section under the "Explorer" panel on VS Code.
Step 5. Select the target IoT Edge Device and right click.
Step 6. Select "Create Deployment for Single Device" menu item as shown below:
alt text
Step 7. This will open a dialog window to asking to select Edge Deployment Manifest. Select the correct deployment manifest file that corresponds to your device architecture under the config folder as shown below: alt text
This will result in the deployment of your edge module as shown below:
alt text
Step 8. Head over the Azure Portal and navigate to IoT Edge Device. This will show the newly created IoT Edge module as shown below: alt text
Step 9. To view the newly created IoT Edge module on the device, open the device terminal and run the following command:
$ sudo iotedge list
This will show the newly created IoT Edge module as shown below: alt text
Step 10. To view the log entries by the newly created IoT Edge module, run the following command on the device terminal:
$ sudo iotedge RequestProcessorModule
This will show the following result:
alt text

Test

Step 1. Head over to Azure Portal, select the IoT Edge device, click the "RequestProcessorModule".
Step 2. On the IoT Edge Module Details page, select "Direct Method" link. This will open up "Direct Method" page that is used to test.
Step 3. Execute the test as shown below:
alt text
Step 4. Head over to the device terminal and run the following command:
$ sudo iotedge RequestProcessorModule
This will show the following result:
alt text

Conclusion

In this tutorial we have seen how easy it is to create a new Azure IoT Edge module using python and deploy it using VS Code.

Monday, May 6, 2019

Deploying ASP.NET Core app to Kubernetes Cluster hosted on Azure’s AKS



How to leverage Microsoft Azure’s Kubernetes Service to host a containerized ASP.NET Core app




Introduction

In previous articles I have discussed how we create a containerized Entity Framework Core enabled ASP.NET Core application developed on Mac and hosted on Docker Hub’s repository. In this article, we will take a look at how to deploy that docker hub hosted docker image into AKS (Azure’s Kubernetes Service). This is the final step which mostly covers the deployment on AKS's Kubernetes cluster. Once the deployment is done, it would become apparent that how powerful Kubernestes 

Prerequisites

1.     AZ CLI 
2.     Containerized ASP.NET Core application deployed on Docker Hub. For details on how to create a containerized docker ASP.NET Core application, please refer to my previous article here: http://mnabeelkhan.blogspot.com/2019/04/developing-containerized-entity.html


Flow overview


 

The flow is pretty simple. It is a three-step process of first creating a containerized ASP.NET Core application in form of a docker image, then pushing that docker image to Docker Hub, and the last step is to deploy the docker image to Kubernetes cluster. The first two steps (1 . Creating a docker image for a containerized docker application and 2. Pushing that image to Docker Hub) has already been covered in the previous article. In this article we will focus on how to deploy a docker image to a Kubernetes cluster using Azure’s Azure Kubernetes Service (AKS).

Before delving into the steps, it is important to mention that the proceeding steps work for both GCP and Azure seamlessly. The only different is the step 1 where we are going to create a Kubernetes cluster. The rest of the steps are exactly the same for both GCP and Azure implementations of Kubernetes clusters. This shows the power of Kubernetes and how widely it is supported by cloud platforms.

Steps:

As our target deployment Kubernetes cluster is on Azure, we have to carry out a setup step of creating a cluster first. That would be our step 1.


1.     Create a Kubernetes Cluster
 “az aks create --resource-group [[YOUR_RESOURCE_GROUP]] --name [[YOUR_CLUSTER_NAME]] --node-count 2 --service-principal [[YOUR_APP_ID]] --client-secret [[Your_Password]]"

Once the above command completes, get the credentials of the newly created cluster by using the following command:

"az aks get-credentials --resource-group [[YOUR_RESOURCE_GROUP]] --name [[YOUR_CLUSTER_NAME]]"

For this blog post, I created "DockerHubAKSCluster" Kubernetes cluster on Azure with "DockerHubAKS" as resource group name. Here is the screenshot of what Azure portal shows me once I create a cluster.






2.     Create a Kubernetes Service
This can be done by deploying docker image into a newly created service.
Run the following command:
“kubectl run mac-api --image=[[YOUR-DOCKER_HUB_REPO_ID]]/mac-api --port 8080”


3.     Expose the deployed service on a port
Run the following command:

“kubectl expose deployment mac-api --type=LoadBalancer --port 80 --target-port 80”
The result of the above command should be:
“service/mac-api exposed”

Once the service has been deployed and exposed, you can run the following command that will give you the cluster IP and the external IP information:
“kubetctl get service”
           


 Once you deploy and expose the cluster, you will notice that Azure creates a separate resource group with "MC_" prefix to the original resourced group that you have used in step 1. Following screen shots shows the contents:


As you can see from the above screenshot that Microsoft Azure creates a separate resources for the infrastructure of Kubernetes cluster. As a user of Kubernetes cluster, we mostly work with the Kubernetes cluster that we have created and Azure automatically configures the other resource group to ensure the infrastructure is in place.



4.     Test
Since the mac-api is an API application, we are going to use “Postman” to test the application.

Following screenshots show how Kubernetes cluster hosted containerized ASP.NET Core application is responding to “Get” and “Post” commands:






Conclusion

In this article we have seen how simple and straight forward is to take a docker hub hosted docker image and deploy that on Azure’s Kubernetes service AKS.