In my previous article, I explained the concepts and theory portion of Azure Service Fabric. In this article, I am going to show you a step by step demonstration of the same.
First of all, on your development machine, you have to install Azure Service Fabric SDK to work with the application. You can download and install Azure Service Fabric SDKs from the link mentioned
here.
We are going to use a sample web application provided by Microsoft in Azure samples. You can get that from the link
here.
In this Quickstart example, we have a Stateful service based on ASP.NET Core 2.0 template. As mentioned earlier, one should go with a Stateful service when they want to manage the persistent state within the service using reliable collection framework. This one is just for understanding. The actual demo follows.
When you click on OK, it will start creating a service fabric part of the project and will ask us to select ASP.NET Core Web Application templates. As we are creating a backend project which will persist the data that we are going to select, choose ASP.NET Web API template here.
Finally, you can see it will create Service Fabric Application and then the Service Project.
You can know more about the Azure Service Fabric programming model in my previous article - Demystifying Azure Service Fabric.
Now, if you open the .csproj file, you can see it is a standard .NET Core 2.0 based application with Service Fabric reference added to it.
If you look at the main method in Program.cs file, you can see that we do not have any ASP.NET Core 2.0 application specific code. We are actually bootstrapping and then registering the service with service runtime. This is called hosting and activation mechanism in Service Fabric. This process will spin up your service in the Service Fabric Cluster. So, this portion of code defines that these are the service types that are allowed to run in this process.
Now, open the VotingData.cs file which is actually our service class. We can see that our service class implements the stateful service.
Now, let us start the demo.
Go to Azure Portal --> Create a Resource Group --> SFCluster. Once the resource gets created, go to the newly created Resource Group --> SFCluster --> Add New Resource --> Key-Vault.
The reason we are going to add a key-vault here is that we need to use certificates with our Service Fabric Cluster. So, for the fully qualified domain name and the public endpoint we want to communicate with to manage the cluster, there needs to be a certificate there for that, and then we need certificates for internode communication and for secure communication between the cluster nodes as they're communicating to power all these applications.
And you can see that the Key-Vault will be created.
Now, go to Create a Resouce --> Compute --> Service Fabric Cluster.
Provision a new Service Fabric Cluster.
Next, we will set up Cluster Configuration.
For development purposes, we can select a Single Node Cluster. But we are not going to do that. We are going to set the initial VM scale set capacity to 3. The default is 5 and for production use, Microsoft recommends five nodes, but we are going to set this to 3. When you click out of that, you will see that choosing less than five initial VMs is kind of against their production.
Now, on the next screen, they are basically asking you what configuration you want to use for the certificates that are going to be used in the Service Fabric Cluster. Now, we could have this thing attached to a key vault that we created on the fly. The reason we pre-created it is because we wanted to be able to manage it outside of this window.
Basically, we want to pre-create the certificate because it is a little tough to do it after the fact. And what I mean by that initially, we actually defined a name for the cluster. It's going to be your cluster under the domain from Microsoft. But we need to have that name on the certificate if we want to be able to manage the Service Fabric Cluster using the Explorer. And this thing isn't going to actually set that up for us.
So, we are going to set that up manually and I am going to show you the kind of process that I use to make this a little bit easier. So, we are going to leave this here for now.
Next, go to PowerShell. We are going to create our own self-signed certificate for this.
Save and execute the below-mentioned script.
Now, open another tab and open Azure Portal. Go to Resource Group --> SFCluster.
Go to the Key-Vault and select Certificates.
Add New Certificates.
Select Import.
Select the certificate from C drive and provide the password 1234. That is defined in the script while we created it.
Now, you can see we have that available.
Now, let us go back to the provisioning process for the Service Fabric Cluster.
Instead of Basic, select Custom type here.
Now, go to Key-Vault --> Properties --> Copy the Resource ID.
Paste the Key-Vault ID.
Now, go to Certificates --> Click on Certificate's Name.
Copy the Certificate Identifier.
Paste it in the Certificate URL box.
Copy the certificate Thumb-Prints.
Paste it to the thumb-print.
Then, click OK. It will do the validation and once it gets done, click Create.
Once the deployment is completed, go to the resource that we just provisioned - Service Fabric Cluster in our case. You will see that the cluster status is still deploying. We have to wait until we get it ready.
Once it gets done, you will see that we have 3 nodes.
Now, click on Security. Here, you can see that we have Certificate Thumbprint available but Client Certificates is empty. We will add the same certificate thumbprint to the Client Certificates.
When you navigate to the endpoint of your fully qualified domain name cluster in the explorer, you will be prompted for a certificate, because that's how we're going to authenticate. And since we have built the certificate with the script, we are going to use the same one.
However, typically you will have a different one.
Click on Add.
Paste the Certificate Thumbprint for the Client Certificate.
It will take some time. When the configuration is updated, you will see your Client Certificate also.
Now you can click on Cluster Link. It will prompt you with the client certificate link.
Though your connection is not private, you would be able to explore it.
You will see that we have 3 nodes ready but don't have any application. That we will deploy next.
Now, open your solution in Visual Studio and change the port to 80 in the ServiceManifest.xml file.
Here instead of Load Cluster, we select the cluster we have created in Azure Portal and click on Publish.
While deployment is going on, go to Azure Portal --> Navigate to the Resource Group (SFCluster). Here, you will see we have a Load Balancer.
When inside Load Balancer --> Click on Health Probes.
Then add a new Health Probe --> 80 with all the default values.
Once the health probe is built, we will add a load balancer rule.
Now if you go to Visual Studio --> you can see deployment is succeeded.
Now, if you check the Service Fabric Cluster in explorer, you can see that the application is deployed successfully.
You can also scale up services. Another way to do that is to go to Azure Portal and write down auto-scale rules.
Well, that's it for this demo. Now, you know how to practically work with Azure Service Fabric.