Practice 3 - Platform as a Service
During this practice session, we'll look into Microsoft Azure's Platform as a Service (PaaS). We'll focus on deploying web applications as PaaS offerings and examining various features of this cloud platform.
Applications can be deployed onto PaaS in Multiple ways:
- Using CLI (Command Line Interface) - Manually deploying the code
- Git-based (BitBucket, GitHub) - Linking code from your app repository
- Using docker image registries - Linking a Docker image from a container registry
References
- Azure Cloud documentation: https://docs.microsoft.com/en-us/azure/?product=featured
- Azure App Service: https://learn.microsoft.com/en-us/azure/app-service/
- Azure Storage: https://learn.microsoft.com/en-us/azure/storage/common/storage-introduction
Exercise 3.1. Getting familiar with the Azure platform and deploying to App Service using docker images
MS Azure cloud can be used with Microsoft account, and students of UT should be able to log in with their existing university account, activate Azure for Students subscription, and use the cloud resources. In this exercise, you will learn about using MS Azure cloud services such as Microsoft Azure App Service.
We will create a docker image for the python flask message-board application (with data.json
version) and deploy the dockerized application image as a PaaS application in Azure App Service.
Task 3.1.1: Build and push the docker image of the flask message-board application.
- We need a docker-machine to perform this task.
- You can use your system if docker is installed, or you may create the VM as in Exercise 2.1 of Practise Session 2
- NB! Use ubuntu22.04 if you start a VM in this lab instead of ubuntu 20.04
- You can use your system if docker is installed, or you may create the VM as in Exercise 2.1 of Practise Session 2
- Fork the project to your Bitbucket or GitHub account. (We will update the source code in further exercises)
- Repos to fork (You can use either of one based on your choice):
- Keep project name as
lab3app
- PS!! Make your repository private.
- Clone your project to a local machine or VM
- PS!! Your account password for Bitbucket will not work, so create a separate password to access the projects using the link.
- Build and push the image to the docker hub with tag
your-dockerhub-id/lab3flask1.0
- It's nice to test the image locally in the VM or your system before pushing it to the Docker hub. This is to avoid run time errors while deploying container image into Azure App Service.
- NB! if you build in MacOS, make sure to build the Docker image for Linux/amd64 by adding
--platform linux/amd64
to the build command - Follow the steps in the previous "Containers: Working with docker" practice session
- Following the task "Task 2.3.1: Create a Docker image for the flask application" to prepare Dockerfile and build the Docker image.
- You can reuse the previous Dockerfile, but make sure it is NOT for the database version of the application created later in the previous practice session.
- Following the task "Task 2.3.1: Create a Docker image for the flask application" to prepare Dockerfile and build the Docker image.
Task 3.1.2: Deploying docker image to Azure App Service.
- Log into Azure cloud: https://azure.microsoft.com/en-us/
- Use your university email and password
- If asked which subscription type to use, select "Azure for Students subscription."
- Open the Azure Cloud portal: https://portal.azure.com/#home
- Familiarize yourself with the available services: https://portal.azure.com/#allservices
Azure App Service is a fully managed platform for building web applications. In this task, we will deploy the message-board application into the App service.
- Search for App Services in the azure portal.
- Click on Create --> Web App
- Subscription- Choose Azure for students
- Choose Resource Group --> Create New--> Name :
lab3
as name of group. - Instance Details
- Choose a name freely (don't use any special characters)
- Select Publish --> Docker Container
- Region --> North Europe
- Pricing plan : Free F1 (Shared Infrastructure)
- Click on Next: Database (Nonthing do here)
- Click on Next: Docker
- Image Source --> Docker Hub
- Docker hub options
- Image and tag --> your-dockerhub-id/lab3flask1.0
- Click on Review and Create and than Create.
- Now, it will take a few minutes to deploy the application and click on Go to Resource
- If errors occur during deployment, such as Error 429, it's advisable to cancel the deployment and start fresh by selecting the Region --> West Europe option.
- Go to Overview of your app service and copy the URL (Essentials-->URL) and open your application in the browser.
- It will take a few minutes for the application to run (Ex: 5 to 6 min for me).
- You can see the deployment logs of the application service Deployment--> Deployment Center-->View Logs
- You can see the container logs Monitoring--> Log Stream
- Once your application runs, test by inserting a few messages.
- Keep the application running and don't access it until you reach Ex 3.5 (PS!! You can continue with the next exercise, and this is to test the data persistence)
Deliverable: Take a screenshot of the application's web page. The web page address should be visible. Note down the URL of the deployed application, which is needed as part of the deliverable.
Exercise 3.2. Deploying Python Flask application in Azure using CLI
This task mainly helps you to learn the deployment of python applications on the Azure Cloud platform using CLI. We will deploy our text-file-based(data.json) Flask message board application.
PS!! You need to use your laptop as a developer machine or a VM to modify the code and to work with Azure services using Azure CLI.
- Let us deploy the Flask application into the Cloud using Azure CLI. Now, install the Azure CLI on your machine or VM as follows:
- On Windows:
- Install Azure CLI using the Windows Package Manager. However, you can use any of the methods used in this document
- Open Windows Power Shell in administrator mode and run the command
winget install -e --id Microsoft.AzureCLI
- On Linux:
- Install the Azure CLI on Linux machine based on the link document. The straightforward way is to use the command
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
.
- Install the Azure CLI on Linux machine based on the link document. The straightforward way is to use the command
- On Windows:
- Log into the azure from the command line:
- Use the command:
az login
- Read the response of the command and follow the steps.
- Use the command:
- Make sure that you're in the directory
lab3app
. - Check the names of the Resource group and the App Service plan that were generated in the previous exercise (lab3 for the group and something like ASP-lab3-825f for the plan).
- Group name is specified with
-g
in the following command - You will need to use plan name it in the following command as part of the
--plan
argument- Replacing the
APP_SERVICE_PLAN_NAME
in the command.
- Replacing the
- Group name is specified with
- Use az cli command to deploy your application into Azure App Service
az webapp up -n <APP_NAME> --sku F1 -g lab3 -l northeurope --runtime "PYTHON:3.10" --plan APP_SERVICE_PLAN_NAME --logs
- Replace
APP_SERVICE_PLAN_NAME
with the correct value of the service plan name generated in the previous exercise by Azure. You can get it by moving to App Services --> Select your previous web app --> Copy the App Service Plan value. The value looks likepoojara_asp_1855 (F1: 1)
and remove (F1: 1) while using in the command. - If you get an error, make sure the resource group name is the same as was created/used in the previous exercise:
-g lab3
. If your resource group name is different, change its name inside the command. - In this command, Freely choose the application name, which should be globally unique in azure cloud. Ex: shiva198701 (don't use any special characters)
- The
-sku
specific the link Azure App Service plan. Here, F1 indicates the Free subscription plan type. - The
--logs
indicates the logs to be displayed in the terminal. - The command
az web up
creates the required resource group and deploys in to the azure app service cloud. - Now, go to App Services.
- Move to your application. In Overview of your application, get the URL (as shown below in the figure) and open the application.
- Replace
- You should see the flask-based Message application running. Test the application by inserting a few messages.
Deliverable: Take a screenshot of the web page of the application. The web page address should be visible. Note down the URL of the deployed application, which is needed as part of the deliverable.
Exercise 3.3. Deploying python flask application in Azure using Bitbucket/GitHub
In this exercise, you will learn to deploy your flask application code from the Bitbucket repository to Azure App Service. Here are the following tasks to be performed:
1) Integrate with Azure App Service with Bitbucket/GitHub code.
2) Check for the deployment status.
3) Modify the application, commit to Bitbucket/GitHub code
4) Finally, you should see your application running with modified code.
Task 3.3.1: Integrate Azure App Service with Bitbucket/GitHub code.
- Create a New App Service application
- Resource group:
lab3
- Name:
FREELY CHOOSE
- Publish:
Code
- Runtime Stack:
Python 3.9
- Region:
North Europe
- Click on Review and Create
- Resource group:
- Now, integrate with Bitbucket/GitHub, Go to Deployment Center in left panel and set the following values
- For Bitbucket:
- Source: Select
bitbucket
- Organization : Provide your bitbucket username
- Provide permission to access by logging in to bitbucket.
- Repository: Choose your repo
lab3app
- Branch:
master
- Source: Select
- Finally, Save the configurations by Click on Save button at the top.
- For Bitbucket:
- For GitHub:
- Source: Select
github
- Sign in as" Sign in to your GitHub account
- Provide permission to access by logging in to GitHub.
- Organization : Provide your GitHub username
- Repository: Choose your repo
lab3app
- Branch:
main
- Workflow Option: Choose
Add Workflow...
- Authentication type: User/assigned identity
- Have Azure generate new identity automatically
- Source: Select
- Finally, Save the configurations by Click on Save button at the top.
- For GitHub:
Task 3.3.2: Check for the status of the deployment.
- Click on View Logs under Deployment Center
- Click on the URL of your project (In overview, as shown in the above figure.), and you should see your application running (It may take a few minutes to get up and running).
Task 3.3.3: Modify the application and commit to Bitbucket/GitHub code.
- Code can be committed using git commands from the system or using Bitbucket/GitHub web interface. However, to commit using git commands with your private repository, needed to setup Bitbucket ssh keys using this document or you can use the Bitbucket app password created in the step Ex 3.1 (PS!! Choice is yours)
- Edit the
home.html
, like Practice Session 1, to display your name on the web page (Make something interesting on your web page :)). - Commit the code.
- Check the logs in App Service in Deployment Center. You should see the commit status with an update on the application deployment, as shown below.
- You should see your application running with the modified code.
Deliverable: Take a screenshot of the web page of the application. The web page address should be visible. Note down the URL of the deployed application, which is needed as part of the deliverable.
Question: How long does it take for Azure to update the running application after you commit changes?
Exercise 3.4. Data persistence using Azure Files
In this task, you will learn to mount external folders and files to the application using the Azure Files service. In the Python Flask application, messages are stored in data.json
file. To make sure the data is persisted when the container is updated, we will mount an external cloud folder into the container and move the data.json
file there. The application can read and write into files in the mounted folder. Further, we investigate how the data persistence works for two of our applications: deployed in Ex 1 (Docker-based) and Ex 3.3 (bitbucket based).
- Let us create a new Azure Storage Account, which we will use for the persistent cloud folders
- To create the storage account, Search for storage account in services overview.
- Click on create (new storage account) and use the following properties:
- Resource group:
lab3
- Storage account name: FREELY CHOOSE a name (PS!! Note it down)
- Region: Should be same as bitbucket-based App Service application (By default: North Europe. PS!! This is important)
- Click Review and Create
- Resource group:
- Once the storage account is created, let us create a new File Share inside the storage account.
- Go to Overview of the just created Storage Account, choose Data Storage --> File shares
- Click + File Share at top, than add Name:
FREELY CHOOSE
(PS!! Note it down) and click on Create. - Once the file share is created, you must upload the
data.json
file into it. (PS!! data.json can be downloaded from the bitbucket code. It needs to have the same content as in the bitbucket repository. Otherwise, the app will give an error.)
- Now, we will mount the created File Share to the App Service application
- Move on to the bitbucket-based application in App Service and go to Overview page.
- On left panel, Click on Settings --> Configuration --> Path Mappings
- Click on + New Azure Storage Mount and use the following properties:
- Name:
FREELY CHOOSE one
- Configuration Options: Basic
- Storage Account: Choose the recently created one
- Storage Type: Azure Files
- Storage Container: Name of the File Share created in the previous step
- Mount Path:
/mnt
- Click Ok
- Name:
- Click Save
- Let us check the File Share mount in the App Service VM.
- In App Service Overview, go to Development Tools-->SSH.
- Click on Go
- This will open a new window with SSH Connection to App Service VM.
- In the terminal, list the /mnt files
ls /mnt
. You should see thedata.json
.
- In App Service Overview, go to Development Tools-->SSH.
- Now, update the bitbucket application source code to modify the path of the data file.
- In the bitbucket source code, change the path of the
data.json
in - ALL places inside the code - to/mnt/data.json
. - Commit the code and make sure the code is pushed/updated in Bitbucket.
- In the bitbucket source code, change the path of the
- Now check the application running with modified code and insert a few messages.
- SSH into the Web App Service, and check the content of
data.json
under /mnt. - Move to the storage account service view and check inside the File Share. Download the data.json for checking whether newly added messages were written there.
Deliverable: Take a screenshot of an overview of File Share created in Azure Storage Account.
Observing the data persistence of the app created in Ex1.
- Now, try to access the application of App Service Created in Ex1, and you may see the messages were deleted because of no new arrivals of user requests and shutting down the container in the background.
- When you access the application later, the container is created with a new data.json file, and previous data was lost.
Observing the data persistence of the app modified in Ex4.
- In this application,
data.json
is mounted to the host file system using Azure files. Hence, data is persistent even though containers were deleted. - You can observe the behavior by running the application by not accessing it for more than 30 minutes.
Deliverables
- Screenshots from exercises 3.1, 3.2, 3.3 and 3.4
- Answers to question in 3.4:
- How long does it take for Azure to update the running application after you commit changes?
- Provide application URLs (Task 3.1.2, Task 3.2 and Task 3.3.3) of your PaaS applications in the cloud (As a comment or as readme.txt file).
- Share your bitbucket/github repository with the TA (Shivananda, bitbucket (email): shivananda.poojara@ut.ee , github (email): shivu.poojar@gmail.com)
- Add TA as a member into your bitbucket project with read permissions
- If this results in an error, you can ignore it. Once TA accepts the invite, error should disappear.
- Delete your VM instance if created in OpenStack cloud.
In case of issues, check these potential solutions to common issues:
- If you get an error when inviting TA into your Bitbucket repository.
- You can ignore this error. TA most likely got the invite and needs to accept it before his name is visible there for you. Possibly something recently changed in Bitbucket code and resulting in an error before the invitee accepts the invite. YOu can ask TA by Zulip if you need confirmation your invite succeeded.
- If you get an Error about creating BitBucket code integration (
invalid token, user identity
, ...),- Create a brand new App Service from scratch (do not use the previously created CLI version of the App Service)
- Create a brand new App Service In a different region
- If you get an error after running
az webapp up
, like this:ValueError: invalid literal for int() with base 16: b''
- It might be because something failed when fetching app service logs.
- You should be able to ignore this error. Just check that the App Service was deployed successfully.
- If you can not choose the
Basic
option when creating aNew Azure Storage Mount
- Wait for a bit and try again
- Or use the Advanced option
- You will need to provide more information manually, including an access key from the Storage Account Azure service. You will find it under Storage account --> Access Keys --> Key1 --> Key (Show)
- If you get the error "We’re unable to validate your phone number"
- It might be a temporary technical issue or rate-limiting block to UT network. Please try again later.
- What is suggested by the MS portal to do in this case:
- Check phone number format: Make sure that the phone number is entered in the correct format, including the country code.
- Check for typos: Verify that there are no typos in the phone number.
- Verify the phone number: Make sure that the phone number you provided is valid and that you have access to the phone.
- Check for any blockages: Verify that your phone number is not blocked by your service provider or blocked by any third-party services.
- Try a different phone number: If the previous steps do not resolve the issue, try using a different phone number to validate your account.
- Contact Azure support: If you are still unable to validate your phone number, you can contact Microsoft Azure support for further assistance.
- Can also try Free Trial subscription, but this requires credit card number based validation.
- If nothing works, contact the lecturer.
- If you get an error when authorizing BitBucket access, something about "Forbidden. CSRF verification failed"
- Try the same process with a different browser. The issue may have something to do with ad/script blocking in your browser.