WAF - Operational Excellence Hands On Activity

The operational excellence pillar covers the operations processes that keep an application running in production. Deployments must be reliable and predictable. Automated deployments reduce the chance of human error. Fast and routine deployment processes won't slow down the release of new features or bug fixes. Equally important, how does your application respond to spikes in demand, as well as how do you monitor and alert on your applications?

Resources

Why are we doing this?

The goal of this activity to give you some hands on experience with several principles Operational Excellence:

Activities

Architecture

Once this lab is completed you should have an Architecture that looks something like the diagram below.

image

Prerequisites

  • An Azure Subscription.

Deploy Azure Monitor Log Analytics Workspace and App Insights

Back to Activities List

  1. Navigate to the Azure Portal https://portal.azure.com

  2. Click on the Cloud Shell. You may be prompted to create a storage account if this is your first time to use the cloud shell.

    image

  3. Ensure that you use Bash for the Cloud Shell

    image

  4. Set the context to the subscription you want to use.

    az account set --subscription "<your subscription name or Id>"
    
  5. Create a Resource Group and Log Analytics Workspace by typing these commands in the Cloud Shell.

    az group create --name rg-opex --location northcentralus
    
    az monitor log-analytics workspace create -g rg-opex -n la-ws-opex
    
  6. Run the following commands to get the WorkspaceId and the Workspace Key, alternatively you can get them from the Portal as well.

    az monitor log-analytics workspace show -g rg-opex --workspace-name la-ws-opex --query customerId
    
    az monitor log-analytics workspace get-shared-keys -g rg-opex --workspace-name la-ws-opex
    
  7. Create App Insights for the Web app

    az extension add -n application-insights
    
    wsID=$(az monitor log-analytics workspace show --workspace-name la-ws-opex -g rg-opex --query id --output tsv)
    
    az monitor app-insights component create --app ai-opex \
    --location northcentralus \
    --kind web \
    --resource-group rg-opex \
    --application-type web \
    --workspace $wsID
    

Deploy Azure App Service and Web App

Back to Activities List

  1. Deploy an App Service Plan and create a Web App
    az appservice plan create -g rg-opex -n asp-opex-1 --location northcentralus --sku S1
    
    let randomNum=$RANDOM*$RANDOM
    webAppName=waopex$randomNum
    az webapp create -g rg-opex -p asp-opex-1 -n $webAppName
    
    webAppId=$(az webapp show -g rg-opex -n $webAppName --query id --output tsv)
    
  2. Create a deployment to the Web App, using a sample application. This is manual, to automatically sync you would need a Git Token. For the lab, we'll just use the manual integration.
    gitRepo=https://github.com/Azure-Samples/php-docs-hello-world
    
    az webapp deployment source config --name $webAppName \
    --resource-group rg-opex \
    --repo-url $gitRepo --branch master \
    --manual-integration
    
  3. Verify the web app by getting the Url and opening the web site. You should see Hello World!
    echo http://$webAppName.azurewebsites.net
    
  4. Verify deployment settings in the Portal.
    1. In the portal, navigate to the rg-opex resource group and click on the app service you created.

      image

    2. Click on the Deployment Center

      image

    3. Verify that the Repository and Branch are set.

      image

Configure App Insights and Diagnostics

Back to Activities List

  1. Configure App Insights for the Web app

    az monitor app-insights component connect-webapp -a ai-opex \
    -g rg-opex \
    --app ai-opex \
    --web-app $webAppId
    
  2. Let's verify that the Web App has been configured with your Application Insights you just created.

    1. In the portal, navigate to the rg-opex resource group and click on the app service.

    image

    1. Click on 'View Application Insights data -->' this will take you to the Application Instights instance.

    image

  3. Create Diagnostic Settings for the Web app

     wsID=$(az monitor log-analytics workspace show --workspace-name la-ws-opex -g rg-opex --query id --output tsv)
    
    az monitor diagnostic-settings create --resource $webAppId --name diagWebApp --workspace $wsID --logs '[{"category":"AppServiceHTTPLogs","enabled":true,"retentionPolicy":{"enabled":false,"days":0}},{"category":"AppServiceConsoleLogs","enabled": true,"retentionPolicy":{"enabled":false,"days":0}}]' --metrics '[{"category":"AllMetrics","enabled":true,"retentionPolicy":{"enabled":false,"days":0}}]'
    
  4. Let's verify the Diagnostic settings were created in the portal.

    1. In the portal, navigate to the rg-opex resource group and click on the app service.

    image

    1. Click on Diagnostic Settings in the Monitoring Section

    image

    1. Click on the Edit Settings of the diagWebApp setting

    image

    1. You should see the settings as we created them in the CLI.

    image

  5. Create some load from the Cloud Shell. You may leave this running while you complete the lab.

    while true
    do
        curl http://$webAppName.azurewebsites.net
    done
    

    Type Ctrl+C when finished with load test after the lab.

Configure Azure Alerts and Autoscaling

Back to Activities List

Alerts

Back to Activities List

Now we will configure alerts to let us know when our Web App begins to consume a certain % of CPU Utilization.

  1. Navigate to https://portal.azure.com .

  2. Ensure you are in your subscription.

  3. Navigate or search for Alerts and click on it.

    image

  4. Click on + Create > Alert rule

    image

  5. Click on + Select Scope

    image

  6. Filter by App Service plans and select the asp-opex-1 item then click Done.

    image

  7. Click on Next: Condition > Then select the CPU Percentage signal

    image

  8. Configure the signal logic, for the lab utilize the graph to make a decision as to when this might trigger. In this scenario, my utilization is pretty low, so I will set it to 2

    image

  9. Click on Next: Action > + Create action group

    image

  10. Create the action group. Save it in rg-opex resource group and name it agEmail

    image

  11. Click on Next: Notifications >

    • Choose: Email/SMS message/Push/Voice
    • Name: EmailMe
    • Email: [ Use your email address ]

    image

  12. Click on Next: Actions >. We won't add any specific actions right now.

  13. Click Review + Create.

  14. Click Create to finish the action group creation.

  15. Click on Next: Details > Setting the following

    • Subscription: Choose your subscription

    • Resource group: rg-opex

    • Severity: Informational

    • Alert rule name: Alert CPU

    • Alert description: CPU Alert

    • Enable upon creation: On

    • Automatically resolve alerts: On

      image

  16. Click on Review + create. Then Create.

Autoscaling

Back to Activities List

Now we will configure autoscale for our Web App to scale out and scale in when our Web App begins to consume a certain % of CPU Utilization.

  1. Navigate to https://portal.azure.com .

  2. Ensure you are in your subscription.

  3. Navigate or search for Monitor anc click on it.

    image

  4. In the Settings Section click on Autoscale

    image

  5. Click on the rg-opex App Service Plan deployed previously.

    image

  6. Click on Custom autoscale.

    image

  7. Set the following Values

    1. Scale mode: Scale based on a metric

    2. Rules: + Add Rule - This will be the scale out rule

      image

      • Metric Source: Current Resource (asp-opex-1)

      • Time aggregation: Average

      • Metric namespace: App Service plans standard metrics

      • Metric name: CPU Percentage

      • Dimension Name: Instance

      • Operator: =

      • Dimension Values: All Values

      • Operator: Greater than

      • Metric threshold to trigger scale action: 20 (Set this value below the CpuPercentage in the screen, just to trigger a scale out event. This is just for demonstration purposes, you should carefully evaluate what the actual threshold should be)

      • Duration (minutes): 5

      • Time grain (minutes): 1

      • Time grain statistic: Average

      • Operation: Increase count by

      • Instance count: 1

      • Cool down (minutes): 5

        image

    3. Repeat the previous step with one minor change to make the Scale in rule.

      1. Set the Metric threshold to trigger scale action: 15

      2. Set the Operation: Decrease count by

        image

    4. Now you should have a Scale out and Scale in rule

    5. Instance Limits Minimum: 1

    6. Instance Limits Maximum: 1

    7. Instance Limits Default: 1

      image

    8. Click Save

      image

    9. After some time, you can click on the 'Run History' in the tool bar of the auto scale setting to see the observed instance count over time to observe when your app service plan scales out and in.

      image

Query Logs

Back to Activities List

Now lets learn how to write and execute a KQL Query against Log Analytics.

  1. Navigate to https://portal.azure.com

  2. Ensure you are in your subscription

  3. Navigate to the rg-opex resource group

  4. Click on your Log Analytics Workspace Item la-ws-opex

  5. In the GeneraL Section click on Logs

    image

  6. Close the 'Queries' dialog window.

    image

  7. Enter the following KQL in the query text box and click Run. Then view the results.

        // Find In AzureMetrics 
        // Find in AzureMetrics to search for a specific value in the AzureMetrics table. Note that this query requires updating the <SeachValue> parameter to produce results 
        // This query requires a parameter to run. Enter value in SearchValue to find in table.
        let SearchValue =  "RG-OPEX";
        AzureMetrics
        | where * contains tostring(SearchValue)
        | take 1000

    image

Clean Up

Back to Activities List

To clean up the lab, run the following command in the cloud shell.

az group delete --name rg-opex

What did we accomplish?

Back to Activities List

  • Automated infrastructure deployment using CLI script.
  • Configured Monitoring for Logs and Metrics with Diagnostic Settings.
  • Automated Web App Deployment from GitHub.
  • Configured Alerts and Autoscaling
  • Queried Log Analytics

References