Azure Service Fabric - amitbhilagude/userfullinks GitHub Wiki
-
Options for Serverless computing
- AKS
- Support container
- Supports open-source framework
- Azure Service Fabric
- Support containers
- Good for Microservices or hosting .net application
- Azure Functions
- Good for event driven
- Options for scheduling
- Easy to complex-> Azure Functions -> Azure SF -> AKS
- AKS
-
Choose Between AKS vs SF
- AKS will support Containers only. SF will support containers, Native applications, and guest executables.
- AKS will support all languages. SF will support all languages but .Net and Java-based applications with deep integrations. E.g. programmatically control scaling, configurations, states, etc.
- SF Support of built-in programming model e.g Stateful Actor
- SF Support of Stateful application with no external dependency.
- SF supports docker only. AKS supports multiple.
-
SF is very powerful and almost Microsoft infra like Azure, DevOps, and O365 are hosted on SF but why is it not popular?
- Perception is that it doesn't support the other cloud than Azure.
- Perception is that vendor lock-in. But SF is open source now like AKS.
- Perception is that it only works on the Windows platform. But it supports Linux as well.
- Perception is that it is built for .Net. Yes, Net has deep integration but it supports other languages like Java, Python, etc.
- Perception is that it required Visual Studio IDE. But it supports other IDEs as well.
-
Installations
- Cloud
- On-Prem
- Dev workstations
- VS 2019 /17 service fabric Tooling feature. This will add the Service Fabric Project type for creating a new project
- Service fabric SDK ( Through Web Platform Installer)
-
Reliable Services
- Reliable Services are stateful or Stateless. Reliable Service is like a console application that can manage state or stateless.
- Stateful reliable service is used when you would like to store information in Service Fabric instead of external DB. It will reduce the latency.
- Recommended to use state less
- Ease for autos calling
- Web APIs are built as stateless service
- This service will be inherited from Statefulservice and you will have state manager object to interact with reliable collections.
- Visual Studio has multiple templates for this type.
- They are grouped based on.Net Framework and .Net Core.
- They are stateful service or ASP.Net Stateful service. ASP.Net stateful service has additional packages related to the asp.net to build web applications.
-
Reliable connection data structure
- Store data into disc
- Available data structures 1. Dictionary 2. Queue
- Best practices 1. Keep transactions short 2. Dictionary with more keys than single key with big values 3. Timeout exception handling
-
Actor
- Built using Actor pattern
- Build on top of Stateful Reliable services.
- This pattern is used to manage the multi-threading where each thread will be a single actor. This actor can perform tasks sequentially. If you want to create multi-threading then you can have multiple actors created.
- Actor is a stateful reliable service so it provides state manager objects however it has only a reliable dictionary so it is more lightweight compared with reliable services.
- Every actor will have a single thread which will have its own storage and reliable collection.
- This service will be inherited from Actor Service.
- In terms of managing the state(Reliable collection), It supports 3 options. This option is set as an attribute to the class. Three options for state management
- Persisted (Highly reliable but low performance). Similar to Strong consistency in Cosmos. it will update all replicas and then respond.
- Volatile: It will update active only and respond. Async it will update the replicas
- Not Persisted (Highest performance but low reliability). It will not data into the disc but in-memory only.
- Optimising performance of state management
- State management is a dictionary and you may need to de-serialize the entire dictionary. Deciding the key is important
- Good for one actor for one user
- No multi-threading is required as we can create actors for each user and requests will send in the queue.
-
Service Fabric Programming Models. The below types are available in Visual Studio for creating project
-
Guest Executable Application:
- Runs as exe in Service Fabric
-
Containers
- Create a docker image, upload it into Library and run it as container
-
Reliable Services
-
Overview
- More access to Service Fabric APIs
- Advantages: It is reliable means services stay up in an unreliable environment. Availability, Scalability, and Consistency
- Only two methods: One for Listening WebAPI port and RunAsync to write business logic.
- When to use Reliable services
- You want your service's code (and optionally state) to be highly available and reliable.
- You need transactional guarantees across multiple units of state (for example, orders and order line items).
- Your application’s state can be naturally modeled as Reliable Dictionaries and Queues.
- Your application's code or state needs to be highly available with low latency reads and writes.
- Your application needs to control the concurrency or granularity of transacted operations across one or more Reliable Collections.
- You want to manage the communications or control the partitioning scheme for your service.
- Your code needs a free-threaded runtime environment.
- Your application needs to dynamically create or destroy Reliable Dictionaries or Queues or whole Services at runtime.
- You need to programmatically control Service Fabric-provided backup and restore features for your service’s state.
- Your application needs to maintain a change history for its units of the state.
- You want to develop or consume third-party-developed, custom state providers.
-
Stateful
- Service manages the state of the Service.
-
Reliable collections(Only for Stateful services). Reliable collections are set of APIs mentioned in Microsoft.ServiceFabric.Data.Collections managing states. Reliable collection as Dictionary and Queue.
- Add Key into Dictionary requires to Create ITrasaction object from StateManager
- AddAsync will add into temp dictionary by using ITransaction object created. Keep lock for 4 s.
- CommitAsync() will commit record into log. Any change after AddAsync before commit for key will be not be committed as there is already lock.
- Read from reliable dictionary is immutable. If we wish to update kay again, it need be created as new object. TO avoid such mistakes use of System.Collections.Immutable class and seal class which is stored in a key.
- HasPersistedState is true for Volatile Reliable persistent. That means state will be not stored in disc and improve latency.
- Reliable queue can be used if there are multiple producers and consumers.Simmilar to dictionary it also have Enqueueasync and Dequeueasync then commitAsync.
- Serialisation and Deseriliastion will be done using IStatemanager serialise object(IStateSerialise). Custom serialisation will be done using by inheriting it.
- Reliable collection is specially designed to store data into disc and read it. it is advanced version system.collection and system.concurrentCollections.
-
Stateless
-
Similar like any other Application where state managed by external application like DB.
-
Common example of stateless service, WebAPI called by Front end application and later forward request to the Stateful Service. which response back later.
-
-
-
ASP.Net Core
- Service Fabric integrates with ASP.NET Core so you can write both stateless and stateful ASP.NET Core applications that take advantage of Reliable Collections and Service Fabric's advanced orchestration capabilities.
- Example Voting Web where Web API are built Stateless API and which will call Stateful service to store voting data into Dictinary.
- Create Sealed class by Inheriting from Web in stateless service which has CreateServiceInstanceListeners methods which return Kerstel communication listeners. Uses Microsoft.ServiceFabric.AspNetCore nugetpackage package includes the UseServiceFabricIntegration extension method on IWebHostBuilder that adds Service Fabric–aware middleware. It also have dependency injection statelessservicecontext.
- Create Sealed class by Inheriting from Date in statefulservice which has CreateServiceInstanceListeners methods which return Kerstel communication listeners. Uses Microsoft.ServiceFabric.AspNetCore nugetpackage package includes the UseServiceFabricIntegration extension method on IWebHostBuilder that adds Service Fabric–aware middleware. It also have dependency injection of statefulsrvice context and statemanager.
- Communcation between two services will be done using Reverseproxy which needs to be enabled during SF deployment and port.
- Web Projects will use proxyaddress and port to communicate with backend services. Using httpclient.
-
Reliable Actors
- Build on top of Reliable services. It is independent unit works in single threaded environment. If you have scenarios that some programs needs to run independently of each other. Actor concurrently run as many as tasks. It takes care of becoming active and garbage collection dispose whenever use. It requires to call proxy method to create a actor and call object.
- It built using Open sources framework Orleans which uses which uses Virtual Actor Pattern. It is Reliable actor Framework.
- It uses Actor Model. In response to a message it receives, an actor can: make local decisions, create more actors, send more messages, and determine how to respond to the next message received. Actors may modify their own private state, but can only affect each other indirectly through messaging
- The Reliable Actor framework provides built-in communication for actors and pre-set state persistence and scale-out configurations.
- Create class by inheriting Actor Class and expose that class by IActorInterface
-
-
Service Fabric Life cycle.
- All services within Service Fabric are managed by Service Fabric Runtime. It takes care of creating instances or deleting them.
- Every service fabric has Two Main methods and constructors in Program.cs
- Constructor to register this instance in Service Fabric Runtime.
- Listener method, this will help you to expose this service with port and namespace so that other can communicate.
- RunAsync is a method that will have your logic which gets executed when a request is received. This method has a cancellationToken parameter, this parameter will help you to cancel the execution and SF runtime can understand the status.
-
How storage works for stateful reliable services or Actors.
- Additional DB is not required as service fabric stores data in its own disc. It has a state manager instance that exposes Reliable dictionary and Queue to interact with this Data.
- Service Fabric creates replicas of this Data and stores it in multiple nodes to make it reliable. In that node one Primary store is active and the rest replicas are in passive mode and SF will decide which one will become based on health.
-
Scenario and its mapping with SF programming model
- Building an e-commerce site that has a couple of APIs.
- ProductCatalog API: CRUD operation of ProductCatalog.
- BucketAPI: Allow users to add products to their shopping carts.
- CheckouAPI: Check out the shopping card and proceed with the order.
- This requirement has a couple of storage requirements.
- Storage for product catalog
- Storage for shopping cart per user.
- Implementation
- Build a reliable stateless service called API Service to expose APIs to the front end. There is no difference between normal API service and this service except this service will be inherited from a stateless service class.
- Build a stateful reliable service called Product Catalog. With this stateful service, you will have a reliable dictionary and queue to store product catalogs. This service will have CRUD operation with a reliable dictionary to add, remove products, etc. it will expose the port and listener so that the API service can call it. It will expose the interface inherited from IService.
- Build a stateful Actor service called User Service. With this stateful service, you will have a reliable dictionary to store the user's shopping cart. it accepts Actor Id as User Id so that it will create one actor per user and store its own state. One-click uses this implementation per user to store the payment information.
- API Service will use the interfaces exposed to these services and use service fabric proxy to communicate. Note that to communicate between these services, SF provides multiple options like WCF, HTTP, and service remoting. Service remoting is the default option where you can only use service namespace e.g. servicefabric:/service name where SF can take care of routing requests.
- Building an e-commerce site that has a couple of APIs.
-
Service fabric explorer in workstations
- Create nodes
- Set replicas
- Explore partitions
- Create required partition key in base 64 format( to be explored)
-
Test disaster recovery
- Restart one of node in Service fabric explorer
- Use of service fabric controlled chaos to simulate the failure and test
-
Partition, Replicas, and Instances
- Service Fabric Explorer
- Allow you to test this with one node or 5 nodes.
- Allow you to see applications, services, partitions, replicas, etc and restart it.
- Replicas vs Instances
- Replica terminology used stateful services and Instances are used for stateless service. The replica is more toward DB and stateful had DB
- Both will set the replica\instances in different nodes for high availability.
- An instance of a stateless service is a copy of the service logic that runs on one of the nodes of the cluster. An instance within a partition is uniquely identified by its InstanceId
- A replica of a stateful service is a copy of the service logic running on one of the nodes of the cluster. In addition, the replica maintains a copy of the state of that service.
- Service Fabric Project
- Service Fabric has another project which keeps the configuration for Service Fabric.
- It has information about how many replica sets, and partitions for different nodes e.g. Local with 1 or 5 nodes and also the cloud.
- It also has information about which service is stateful and which is stateless.
- Service Fabric Explorer
-
Application deployment steps using Powershell
- Create SF package
- Connect to SF cluster. Note: It is easy connect to local cluster. however if you need to connect cluster created in Azure then certificate is used. This certificate was created and also used while creating cluster and same certificate is needed on locally to connect it. This is also applicable when you create a connection with Azure DevOps.
- Deploy package
- Register App
- Create an instance
-
Service Fabric native microservices vs Containerised Deployment
- Container required additional storage
- Need to manage using docker image
-
Hyper V containers
- This offering from Microsoft is for more security compared with others. Containers share the host OS kernel and if there is any security compromise in one container it may have a high impact on others. Hyper V container has a dedicated OK kernel per container.
-
App with container Deployment
- Steps for Container
- Create a package
- Upload into the image store
- Create\ upgrade instance 4. Note that the service fabric manifest will have a container host tag with an image to restore from the registry. SF will get to know it doesn't have binary.
- Initiate Instance 2. Azure File share enable for state management with storage account 1. With Powershell 2. Storing state Resource Governance
- Steps for Container
-
Orchestrations: Takes card deploying, upgrading applications\containers in nodes without human interaction. Takes cares of failover mechanims.
-
Service Fabric Application Life Cycle
- Follows same deployment life cycle like earlier application. Design, develop, Testing, Maintainance etc.
- Different roles are defined to manage application e.g. Service Developer(service.menifest), Application Developer(application.menifest), Application Adminstrator(Packaging, override changes from applcation tempates), Operator(Deployment)
-
Service Fabric Managed Cluster
- Currently in preview, Help to provision cluster easily. Existing deployment requires to add public IP, ILB, VMSS, Storage etc.
-
Service Fabric best practices
- There is already a production readiness checklist down which is recommended option.
- Application Design
- Use of API gateway like Azure APIM, Traefik, Application Gateway, etc.
- Recommended to use Stateless service and use of external DB.
- Application diagnostics will be done using App Insights
- Security
- Key Vaults is recommended option for secret management like Certificate
- ACL
- TLS 1.2
- Networking pattern e.g. Load balancer
- Capacity Planning
- Reliability level and Durability level
- Bronze, Silver, Gold, and Platinum which help to define Replicaset and instances node, count
- Autoscaling, Verticale scaling and Horizontal scaling
- Deployment should be Infrastructure as code i.e. ARM template
- Fabric Transport communication for Reliable actors
- Primary node should be D2V2 or highly SKU with at least 50 GB HDD capacity
- Production cluster must be secure
- Add resource constraints at the node level so that they don't consume more than 75% of nodes
- Load and Scale test to Identity the capacity requirements
- Logging and Monitoring for Service Fabric cluster, VMSS, and Application using Azure Monitor
- Cluster must have a primary and secondary certificate
- Separate cluster for Dev, staging and Prod
- Turn off automatic updates on Prod and turn on Dev and Stage and test
- CI-CD pipeline for deployment
-
Cluster Management
- Cluster management using vNet and NSG
- Cluster Monitor using Azure Monitor
- Cluster scale in,scale-out and Load balancing
- Cluster upgrade runtime.
- Disaster Recovery
-
Service Fabric deployment using Powershell
- log in to Azure
- Create a Resource Group
- Create Key Vault
- Create a self-signed certificate and upload the certificate in the key vault
- Virtual Machine Scale Set
- Load balancer
- Storage account
- Create Service Fabric cluster: Update Certificate thumbprint, Management Endpoint, Cluster Reliability and Durability level
-
Service Fabric Security
- Cluster and server certificate (required)
- This is required to communicate nodes securely.
- It uses Primary and Secondary(Optional) certificates.
- Authentication with management endpoint or Service fabric explorer same certificate will be used.
- Subject field used in the certificate cluster must be common name or the Certificate's Subject Alternative Name(SAN)
- The certificate must contain a private key. These certificates typically have extensions .pfx or .pem
- The certificate must be created for key exchange, which is exportable to a Personal Information Exchange (.pfx) file.
- Certificate's subject name must match the domain that you use to access the Service Fabric cluster.
- Application certificates (optional)
- Encryption and decryption of application configuration values
- Encryption of data across nodes during replication.
- Client authentication certificates (optional)
- Any number of additional certificates can be specified for admin or user-client operations. The client can use this certificate when mutual authentication is required. Client certificates typically are not issued by a third-party CA. Instead, the Personal store of the current user location typically contains client certificates placed there by a root authority. The certificate should have an Intended Purposes value of Client Authentication.
- By default the cluster certificate has admin client privileges. These additional client certificates should not be installed into the cluster, but are specified as being allowed in the cluster configuration. However, the client certificates need to be installed on the client machines to connect to the cluster and perform any operations
-
Cluster security
- Cluster consists of nodes and they are connected to each other so that they can communicate with each other. Security is important here so that any malicious activity like Someone will add a new risk node in the cluster or try to hack communication. To avoid this. Node to Node security is important. When a client tries to access the Cluster e.g., Open service explorer, it also needs to be secured. So it will also require Node-Client-Security. You also need the granular permission of the cluster e.g. Admin may have full privileges but someone only need read access to the cluster. The below types of security need to be considered.
- Node-to-Node Security
- Use of x.509 Certificate and needs to install it on every node. Use Azure Key Vaults.
- This certificate needs to be installed on every node and it uses an asymmetric key concept. The public key can be retrieved from Domain Authority.
- Node-to-Client Security
- Use of X.509 certificate.
- Network Security
- Create an Isolated environment and use a reverse proxy for public network communication
- Application Security
- Authentication and authorization.
- RBAC
- Read only user or Admin user to access cluster.
-
Recommendation for Cluster Deployment
- Not to use the azure portal for Cluster creation but ARM Template(IaaC)
- Create a new resource group
- Use Azure Key Vault for the certificate
- Separate vNet
- Use of ARM templates. Use PowerShell if not possible with ARM templates.
-
Steps
- Scripts download https://github.com/aloneguid/pluralsight-using-azureservicefabric-in-production
- Install Microsoft Service fabric SDK, PowerShell, Visual Studio code with Azure Resource Manager Tool (display ARM template as Tree) and Powershell extensions.
- Run Powershell Scripts from M2 folder. m2.ps1
- Checks if an azure account is already logged in. If not, Run Azure-rm-account to login.
- Checks if Resource group is already exist, If not, It create new resource group. Update resource group name.
- Checks if Key vault is already created, if not new Key vault will be created.
- Create self-signed certificate and upload in Key-vaults (Only recommended for Dev and Test). Need to purchase from certificate authority for Prod. It will upload certificate in Key vaults.
- Invokes ARM Templates minimal.json to create resources. (Use only for Single node)
- Creates Virtual Machine Set
- Creates storage accounts for logging and dignosis.
- Creates Virtual network for cluster.
- Creates load balancer
- Creates Public IP address.
- Create cluster with single node.
- Validate Cluster is created. Manual check and Run script to check.
- Two ports are opened in the load balancer. 19000 and 19080. The first port is the management endpoint port exposed for deploying applications in Service Fabric. The second port is the SF explorer port to open Service Fabric explorer.
- Plan Cluster Capacity
- Minimum 3 nodes in Prod scenario.
- Durability vs Reliability
- You can set both as Bronze, Silver, and Gold. Platinum is only available for Reliability.
- Durability level will give flexibility of infrastructure integration e.g. It will support autoscaling, maintenance updates etc. Reliability is basically how many instances/replicas will be created.
- Durability Bronze is a basic setup that will not support autoscaling and maintenance updates. Silver will support autoscaling. Gold will support both and it is the highest level.
- Reliability bronze will support 3 nodes, Silver 5 nodes, Gold 7 and platinum supports 9 nodes. VMSS instance count should match to this.
- Durability and Reliability types
- Need to decide during cluster creation. It has types of Silver, Bronze, Gold, and Platinum. It has predefined nodes count.
- Decide based on basic testing and keep 20% more.
- Decide node types and Number of Nodes.. Recommended to have multiple node types in Prod. Node types: identical machine with similar CPU, hardware and memory.
- Run Powershell and Minimal.json from M3 folder for Silver Durability and Reliability plan. (5 nodes)
- Test application 1. Download Test application from https://github.com/aloneguid/pluralsight-using-azureservicefabric-in- production/tree/master/TestApp/Pluralsight.SfProd 2. Run testapp.ps1 to deploy application in cluster.
- Create a reverse proxy to enable external access. It is needed because applications deployed in SF, need to open one port per app in ILB. To avoid this you can use a reverse proxy like Traefik or Ngnix which can router requests based on the Url. ILB will have standard 443 port opened. 1. Use Traefik Router to enable. It is another application that needs to host in Service Fabric. Download Traffic router package (officially supported by microsoft. 2. Update the latest package under https://github.com/aloneguid/pluralsight-using-azureservicefabric-in- production/tree/master/Traefik/TraefikPkg 3. Run traefik.ps1 from root path and it will deploy application in service fabric. Application can open using ports mentioned Service fabric. 4. Update the load balancer rule in mimimal.jsonto allow this application. 5. Update servicemenifest.xml in the test application. It allows to creation entry in Traffic application. 6. Run the application to check if it works.
-
RBAC: This is used to provide read-only and admin access to the service fabric cluster.
- Below script with register azure ad apps in AD. you can add users in a group to provide access.
- Download the latest AAD tool and copy it https://github.com/aloneguid/pluralsight-using-azureservicefabric-in-production/tree/master/M4
- Run prepare app script from M4 to register apps in Azure AD. prepareapps.ps1
- Add users in Admin and user roles for the cluster app.
- Update the ARM template under M4. refer onenode.json for single node. Refer to Production.json which has everything configured for PROD.
-
Cluster Monitoring
- Cluster monitoring is done using log analytics. All custom logs of Service fabric are stored in Windows events. These event needs to be ingested in log analytics. It is done by installing a Monitoring agent on each node.
- create a log analytics
- Add Service Fabric log analytics solutions. This plug will help you monitor in a better way.
- Once it is created. Go to advanced settings in log analytics and select windows performance counter. i.e. list of metric data can be ingested.
-
Application Monitoring using Application Insights
-
Scaling
- Manual Scaling of Virtual Machine Scale Sets
- Auto scaling of Virtual machine scale sets.