Running ASP.NET Core Application In Azure Service Fabric Cluster

In my previous C# Corner article we learned to install the Azure Service Fabric SDK and we configured a 1 node cluster locally. After that, we created a Microservice application with MVC using Service Fabric and saw how it is running in the local cluster. In this article, we will create an Azure Service Fabric Cluster with 5 nodes using the Azure Portal and we will deploy our existing ASP.NET Core application in this cluster.

Azure Service Fabric is a distributed systems platform which makes it easy to package, deploy, and manage scalable and reliable microservices and containers. Please refer to this URL to get the basic idea about Azure Service Fabric.

We can create an Azure Service Fabric cluster first.
 
Login to Azure Portal
 
Create a resource -> Containers -> Service Fabric Cluster
 
Running ASP.NET Core Application in Azure Service Fabric Cluster 
 
You can give the cluster name and VM credentials.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
You can choose the Node type now. There are 3 types of node type counts. We chose node type count as 1. Also, give the node type name. Currently, node type name is restricted to 9 characters.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can choose the VM size now. Currently, there are 9 types of VM available. I chose the basic type. I will get 2 cores per node and 8GB per VM.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster 
 
We can choose a number of VMs. I chose the default 5. It will create 5 nodes in the Fabric cluster.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster

After choosing the VM count, you can click the OK button to proceed.

We can create a Key vault for security purpose. This key vault and Certificate will be used later.

 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can give a name to the key vault and click “Create” button to proceed.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
After creating the key vault you must edit the access policies.
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can select the first two access policies and click the “Save” button to proceed. Azure Disk Encryption is not needed currently.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can give a certificate name and click the “OK” button to proceed.
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
It will take some time to create our certificate and we can download this certificate for future usage. This certificate will be used later with our cluster. Please click the provided link to download the certificate.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
The link to download certificate will be opened in a separate tab and you can download the certificate by clicking the “Download as a certificate” button.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster

After downloading the certificate please come to the previous page and click the “Create” button to create our Service Fabric Cluster.

It will take some time to complete the deployment of our Cluster.
 
Meantime, we can install the certificate to our windows machine. Double click the certificate file which we already downloaded from Azure Portal.
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
Choose store location as “Current User” and click “Next” button to proceed.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
Keep password as empty and click “Next” button to proceed.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
Click “Next” button to proceed and in the next page press “Finish” button to complete the certificate import.
Running ASP.NET Core Application in Azure Service Fabric Cluster

Cluster deployment will be finished soon. You can see all the resources listed in the Azure. Please note for one cluster, there are a total of 8 resources created in Azure. 1 Public IP Address, 1 Load balancer, 1 Virtual machine scale set, 1 Key vault, 2 Storage accounts, 1 Virtual network along with our cluster.

Running ASP.NET Core Application in Azure Service Fabric Cluster 
You can see there are 5 active nodes available in the cluster.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can copy URL of the Service Fabric Explorer and open the cluster dashboard in a browser. While opening the Service Fabric Explorer, it will ask your certificate. Please choose the existing certificate we already installed to our system. 
 
Running ASP.NET Core Application in Azure Service Fabric Cluster 

Please note that 5 nodes are active and no application is hosted yet in this cluster.

We can open Visual Studio 2017 in Administrator mode (It is very important, we need administrator rights later for PowerShell commands) and open our existing .NET core application. (We already created a sample .NET Core application and I explained in my previous C# Corner article). We can publish the application by right-clicking the microservice project and choosing “Publish”.

Running ASP.NET Core Application in Azure Service Fabric Cluster
 
You can choose the Azure profile and we can see the recently created Service Fabric Cluster available there. Please choose the Cluster and click “Publish” button to proceed.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster

It will take some time to finish the deployment.

If you go to service fabric explorer, you can see that the new application is listed there now.

Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can open the MVC project (SarathMicroService) in the solution and under PackageRoot folder and Config folder there isServiceManifest.xml file available. Please open that configuration file.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
Inside that file Input, Port is mentioned automatically by the system. It varies from project to project. Please note down this Port number. We will use this Port number in Load balancer rule.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
ServiceManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ServiceManifest Name="SarathMicroServicePkg" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  3.     <ServiceTypes>  
  4.         <!-- This is the name of your ServiceType.  
  5. This name must match the string used in RegisterServiceType call in Program.cs. -->  
  6.         <StatelessServiceType ServiceTypeName="SarathMicroServiceType" />  
  7.     </ServiceTypes>  
  8.     <!-- Code package is your service executable. -->  
  9.     <CodePackage Name="Code" Version="1.0.0">  
  10.         <EntryPoint>  
  11.             <ExeHost>  
  12.                 <Program>SarathMicroService.exe</Program>  
  13.                 <WorkingFolder>CodePackage</WorkingFolder>  
  14.             </ExeHost>  
  15.         </EntryPoint>  
  16.         <EnvironmentVariables>  
  17.             <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="" />  
  18.         </EnvironmentVariables>  
  19.     </CodePackage>  
  20.     <!-- Config package is the contents of the Config directoy under PackageRoot that contains an  
  21. independently-updateable and versioned set of custom configuration settings for your service. -->  
  22.     <ConfigPackage Name="Config" Version="1.0.0" />  
  23.     <Resources>  
  24.         <Endpoints>  
  25.             <!-- This endpoint is used by the communication listener to obtain the port on which to listen
  26. . Please note that if your service is partitioned, this port is shared with replicas of different partitions that are placed in your code. -->  
  27.             <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8281" />  
  28.         </Endpoints>  
  29.     </Resources>  
  30. </ServiceManifest>  
We can go to Azure Portal again and choose Load balancer from resource list. Please open it by double clicking and again click “Load balancing rules” tab.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
It will open existing rules inside the load balancer. Currently, there are two rules available for this load balancer. You can open the first rule.
 
Running ASP.NET Core Application in Azure Service Fabric Cluster
 
We can modify the Port number with “80” and Backend port with “8281” (We already noted this Port number from our ASP.NET Core application)
 
Running ASP.NET Core Application in Azure Service Fabric Cluster

You can click the “Save” Button to apply these new port numbers to load balancer.

You can check the running application by opening in a browser. (Without https) Our application is running fine.

Running ASP.NET Core Application in Azure Service Fabric Cluster
 
In this article, we saw how to create a Service Fabric Cluster in the Azure portal and we created the Cluster with 5 nodes. We also saw how to create Azure Keyvault and Certificate with Cluster. Later we opened our existing ASP.NET Core application in Visual Studio 2017 and published to Azure Service Fabric Cluster. We configured the Load balancer rule with our application’s Input Port and we successfully executed our application in Service Fabric Cluster.