The goal of this lab is to take an existing web application and move it from a standard deployment using on premises web farms to a container infrastructure on Azure. This will enable a much more saleable environment with a lot less management. In this lab you will:
- Create a container registry in Azure
- Build an application for containers leveraging Azure compute
- Deploy the containers to scalable web instances.
You will need a few things in your environment setup for this lab.
- Source code for the Inventory and Product services.
- Source code for the front end web site.
- Azure Container Repository
The source code for all three projects are in this repo. We will first pull it all down locally in our Azure Bash Shell.
-
Launch a new Azure Command Shell. You can either:
- Press the shell icon in the Azure Portal, as in the setup for the Cosmos DB
- Open a new browser tab to: http://shell.azure.com for a full screen experience
-
Pull down the source code locally. Run the following git command
git clone https://github.com/chadgms/2019AzureMigrateYourApps
-
If you do an ls command you should see the repo on your local shell
- In the Azure portal click on the new resource button
- Type 'Container Registry' in the search bar - press enter
- Select Create
- Fill in properties
- Registry name: (prefix)acr
- Location: default
- Admin User: Enable
- SKU: Standard
- Creation should only take a few minutes, but you can review key features while you are waiting here: https://docs.microsoft.com/en-us/azure/container-registry/container-registry-intro
Now we get into the really exciting stuff! We have an existing code base for our two services and web front end. These steps will compile the code, put them in a docker container and put the container image into the container registry. Typically you would need Docker installed and configured. Azure Container Registry Service can do the build and containerization for us all on the cloud! We just need to point it to our source code.
-
Set an environment variable to the name of your Azure Container Registry (ACR). It is the name you put in the Registry Name property when you created the registry - (prfex)cr. (Do NOT use the full .azurecr.io UNC, only the name)
MYACR=<your ACR name>
-
Run the following command to build the code and push the product service image to the ACR.
az acr build -t product-service:latest -r $MYACR ./2019AzureMigrateYourApps/Lab2-ContainerizingApplications/src/product-service
-
When finished you can confirm that the container was deployed to ACR in the portal.
-
Same for the inventory service.
az acr build -t inventory-service:latest -r $MYACR ./2019AzureMigrateYourApps/Lab2-ContainerizingApplications/src/inventory-service/InventoryService.Api
-
Verify it exists in the ACR same as before
-
Now the front end web site.
-
Before we build the site we need to make sure that execute permission is set on the Docker configuration file:
chmod +x ./2019AzureMigrateYourApps/Lab2-ContainerizingApplications/src/frontend/docker-startup.sh
-
Now we build the code
az acr build -t frontend-service:latest -r $MYACR ./2019AzureMigrateYourApps/Lab2-ContainerizingApplications/src/frontend
-
-
Verify it exists in the ACR same as before
Now that we have compiled code in containers stored in the registry we now need to deploy them to a compute platform. In this lab we are going to use Azure Web App for Containers, but there are many ways to run containers in Azure. Your instructor should have explored other options. See reference links below for more information.
- Press the create resource button in the Azure portal
- Search for 'Web app for containers' and press enter
- Press Create
- Fill out parameters as follows
- App Name: (prefix)product
- Resource Group: (your resource group)
- OS: Linux
- Service Plan: Create New
- App Service Plan: (prefix)serviceplan
- Location: eastus2
- Pricing: Dev/Test - B1
- Configure Container:
- Pick Azure Container Registry
- Pick your ACR
- Select the product-service image
- latest tag
- Press Apply
- Press Create
- Press the create resource button in the Azure portal
- Search for 'Web app for containers' and press enter
- Press Create
- Fill out parameters as follows
- App Name: (prefix)inventory
- Resource Group: (your resource group)
- OS: Linux
- Service Plan: Pick the same plan you created for the product service app. You only need to create one plan that the web apps share.
- Configure Container:
- Pick Azure Container Registry
- Pick your ACR
- Select the inventory-service image
- latest tag
- Press Apply
- Press Create
- Press the create resource button in the Azure portal
- Search for 'Web app for containers' and press enter
- Press Create
- Fill out parameters as follows
- App Name: (prefix)frontend
- Resource Group: (your resource group)
- OS: Linux
- Service Plan: Pick the same plan you created for the service app. You only need to create one plan that the web apps share.
- Configure Container:
- Pick Azure Container Registry
- Pick your ACR
- Select the frontend-service image
- latest tag
- Press Apply
- Press Create
We now have web apps created for all our resources. The last thing we need to do is configure application environment variables like connections strings. When the services start up they can read the environment variables so we can make configurations at runtime.
The product service uses the NOSQL data that was in the on-premise MogoDB. We successfully migrated that data to Cosmos DB, so that is what we will configure our Product Service for.
-
Click on resource groups-> (your resource group)
-
Click on your Cosmos DB Account
-
Click on the Connection String option in the left toolbar
-
Copy the Primary Connection String (NOT the primary password)
-
Click on resource groups -> (your resource group)
-
Click on your product service resource of type 'App Service'
-
Click Configuration on the left nav bar
-
Here you will see some default application setting. We will add a few more.
-
Click + New application setting to add each of these NAME/VALUE pairs
-
Name: COLLECTION_NAME Value: inventory
-
Name: DB_CONNECTION_STRING Value: (paste in the Cosmos DB connection String)
-
-
Press Save
The inventory service needs to be pointed to the SQL Database that now lives in Azure SQL Azure
- Click on resource groups -> (your resource group)
- Click on the tailwind database (resource type: SQL database)
- Click on connection strings on the left navigation pane
- Copy the ADO.NET connection string
- Click on resource groups -> (your resource group)
- Click on your inventory service resource of type 'App Service'
- Click Configuration on the left nav bar
- Here we will add a Connection String
- Click + New connection string
- Press OK
- Press Save
The last thing we need to do is to tell our front end web site the URL's to our web services for product and inventory.
You can get the base URL's for inventory and product services by clicking on their overview page and looking at the URL property on the right hand side.
- Click on resource groups -> (your resource group)
- Click on your inventory or product service resource of type 'App Service'
- Take note / copy the URL
- Click on resource groups -> (your resource group)
- Click on your front end resource of type 'App Service'
- Click Configuration on the left nav bar
- Click + New application setting to add each of these NAME/VALUE pairs
- Name: INVENTORY_SERVICE_BASE_URL Value: (your inventory base url)
- Name: PRODUCT_SERVICE_BASE_URL Value: (your product service base url)
- Press Save
That is is it! We are done migrating the data and deploying a modern application through containers. The last thing to do is to run the app and make sure it works! (Note: the web page will render pretty quickly, but the data may take 2-3 minutes to show up. If it takes longer than 3-4 minutes the we should start to debug)
- Click on resource groups -> (your resource group)
- Click on your front end service resource of type 'App Service'
- Click on the URL in the right properties pane
Your web app should now be live! Congratulations!