Compute Services in Azure App Services
Azure App Services provides a robust and scalable platform for hosting your web applications, APIs, and background jobs. This section focuses on the compute resources and capabilities available within App Services.
Core Compute Concepts
When you create an App Service plan, you are defining the underlying compute resources (like CPU, memory, and storage) that your app will run on. These plans are available in different pricing tiers, offering varying levels of performance and scalability.
App Service Plans and Tiers
An App Service plan determines the location, size, features, cost, and compute resources of the virtual machines your web apps run on. You can have multiple apps in a single App Service plan to save costs.
- Free/Shared: For development, testing, and small-scale applications. Limited resources and features.
- Basic: For development/test and some production apps. Offers custom domains and SSL.
- Standard: For production workloads. Offers auto-scaling, staging slots, and more powerful compute.
- Premium: For mission-critical workloads. Offers more compute power, VNet integration, and deployment options.
- Isolated: For highly sensitive and secure workloads. Runs on dedicated infrastructure.
Scaling Compute Resources
Azure App Services offers two primary methods for scaling your compute resources:
- Manual Scaling: You manually adjust the number of instances or the tier of your App Service plan.
- Auto-scaling: You configure rules based on metrics (CPU usage, memory, HTTP queue length, etc.) to automatically scale your app up or down.
Auto-scaling Benefits
Auto-scaling helps ensure your application performs well under varying loads while optimizing costs. It automatically adds or removes instances based on predefined rules.
Compute Features
Virtual Machine Instances
Your App Service runs on one or more virtual machine instances. The number and size of these instances are determined by your App Service plan. You can view and manage these instances within the Azure portal.
Instance Health
Azure App Services monitors the health of your compute instances. If an instance becomes unhealthy, App Services can automatically restart it or replace it to maintain application availability.
Web Workers and Processes
Within each VM instance, your application runs as one or more worker processes. You can configure settings related to these processes, such as the number of ASP.NET Core threads or maximum allowed memory.
Deployment Slots
Deployment slots allow you to manage different versions of your application. You can deploy to a staging slot, test it thoroughly, and then "swap" it with the production slot with zero downtime. This is a critical compute feature for safe deployments.
Zero Downtime Deployments
Utilizing deployment slots is the recommended approach for achieving zero-downtime deployments. This ensures that your users experience uninterrupted service.
Customization and Configuration
Runtime Stacks
App Services supports a wide range of runtime stacks, including .NET, .NET Core, Java, Node.js, PHP, Python, and custom runtimes. You select the appropriate runtime when creating your app.
Environment Variables
You can configure application settings and connection strings as environment variables, which are accessible to your application code. This allows for flexible configuration without recompiling.
Web.config / App.config
For .NET applications, you can leverage standard configuration files like web.config for advanced settings, routing, and module configurations.
Code Samples
Configuring Auto-scaling Rules
Here's a conceptual example of how you might configure auto-scaling rules using Azure CLI:
az monitor autoscale create \
--resource-group MyResourceGroup \
--resource MyWebApp \
--min-count 2 \
--max-count 10 \
--scale-out cpu-percentage 60 \
--scale-in cpu-percentage 20
Accessing Environment Variables in Code (Node.js Example)
const express = require('express');
const app = express();
const port = process.env.PORT || 3000; // Get port from environment variable
const appName = process.env.APP_NAME || 'My App';
app.get('/', (req, res) => {
res.send(`Hello from ${appName}!`);
});
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
Related Services
Compute in App Services often works in conjunction with other Azure services:
- Azure Functions: For serverless compute.
- Azure Container Instances: For running containers without managing VMs.
- Azure Kubernetes Service (AKS): For orchestrating containerized applications.