Azure project construction-deploy highly available Python Web applications, azurepython

Source: Internet
Author: User
Tags app service dns entry dns names
Azure Project Build-Deploy highly available Python web applications, azurepython

Python is widely popular in the world for its beautiful, clear and simple features, and it has become one of the most mainstream programming languages. The Azure platform provides very complete support for Python. In this project, you will learn how to construct and deploy Python applications based on Azure Web applications. By using Azure Web applications, you can more easily develop Python applications. You can use any familiar IDE to quickly build your own Python applications, while supporting the latest 2.7.x and Python 3.x versions, and publish to Azure Web Application Services with one click. Azure web applications can dynamically adjust application service plans and perform horizontal and vertical scaling according to user needs to adapt to application adjustments and continuous cost savings.

The following is a very simple architecture diagram about Python applications:

Preparation

1. An Azure account (log in to the homepage, use your mobile phone number and add the necessary personal information to complete the registration. It is divided into a personal version and an enterprise version, currently supports 1 yuan for trial accounts, you can get 1,500 yuan Azure service credit Valid for one month, if you want to quickly understand the rights and interests of the service, poke this link: https://www.azure.cn/offers/ms-mc-azr-44p/ basic functions

2. Install the Python development environment. Click here to download Python. The current latest versions are Python 3.6.2 and Python 2.7.13. You can freely choose which version to use. If you are still struggling to choose which version, please read Should I use Python 2 or Python 3 for my development activity. Seeing the following information proves that your Python has been successfully installed.

3. Install Git. Git is one of the most popular version control tools. Support Windows, Mac, Linux and other platforms, click to download. Because our sample code is stored on GitHub, you need to install Git locally to download and upload the code. After the installation is complete, you can use the following command to verify success: git--help

 4. Install the Azure CLI. In this tutorial, we used the Mac operating system for installation. The installation steps are:

1) Use a curl command to install Azure CLI 2.0, enter in terminal:

Curl -L https://aka.ms/InstallAzureCli|bash

2) It may be necessary to restart the command shell for certain changes to take effect. Enter in terminal: exec-l $ SHELL

3) When you enter az in the command line terminal and the normal Azure information shows success, it proves that you have successfully installed

Implementation Step One: Create a Web Application

Sign in to Azure
Now, we will use Azure CLI 2.0 to create the required resources in the terminal window to host the Python application in Azure. Log in to your Azure subscription using the az login command and follow the instructions on the screen.

Azure CLI running: az login

note:
Before using Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud to change the cloud environment.
If you want to switch back to the international version of Azure, please run az cloud set -n AzureCloud again.

Configure deployment users
For FTP and local Git deployment, a deployment user needs to be configured on the server to authenticate the deployment. Creating a deployment user is a one-time configuration. Please write down your username and password, as they will be used in the following steps.

Use the az webapp deployment user set command to create account-level credentials.

Az webapp deployment user set --user-name <username> --password <password>

 

Create a resource group
Use az group create to create a resource group. Azure resource groups are logical containers in which Azure resources (such as web applications, databases, and storage accounts) are deployed and managed.

Az group create --name myResourceGroup --location chinanorth

Create Azure App Service
Use the az appservice plan create command to create a Linux-based application service plan.

Note:
Application service plan represents a collection of physical resources used to host applications. All applications assigned to the application service plan will share the resources defined by the plan, which can save costs when hosting multiple applications.

Application service plan definition:

Region (Northern China, Eastern China).
Instance size (small, medium, large).
Scale count (one, two, three instances, etc.).
SKU (free, shared, basic, standard, advanced).

In the following example, let ’s first create a free service plan quickStartPlan

Az appservice plan create --name quickStartPlan --resource-group myResourceGroup --sku FREE

After creating an application service plan, Azure CLI displays information similar to the following example.

Create a web application
After creating an application service plan, please create a web application in the quickStartPlan application service plan. The web application provides a hosting space for deploying code, and provides a URL for viewing deployed applications. Use the az webapp create command to create the web application.

In the following commands, replace the placeholders that appear with your own unique application name. It will be used as the default DNS site for web apps, so this name needs to be unique among all apps in Azure (if not unique, you will receive a "Website with given name already exists" error). Later, you can map any custom DNS entry to the web application and then expose the entry to users.

Az webapp create --name <app_name> --resource-group myResourceGroup --plan quickStartPlan

After creating the web application, the Azure CLI will display information similar to the following example.

Browse to the site to view the newly created Web application.

http: // <app_name> .chinacloudsites.cn

Now we have created an empty new web application in Azure. Next, configure the web application to use Python and deploy it to Python. You can also view it from the management portal:

 

Implementation Step 2: Deploy the Python application to the application service

Configure to use Python
Use the az webapp config update command to configure the web application to use Python version 3.x (please use the python--help command to view the currently installed version by yourself).

note:

Setting the Python version in this way uses the default container provided by the platform. If you want to use your own container, please refer to the CLI reference article of the az webapp config container update command.

Az webapp config update --name <app_name> --resource-group myResourceGroup

At the same time, you can also use the az webapp config set command to set the corresponding version, such as 3.4

Configure local Git deployment
It can be deployed to web applications through different methods, including FTP, local Git, as well as GitHub, Visual Studio Team Services and Bitbucket.

Use the az webapp deployment config-local-git command to configure local Git access to the web application.

Az webapp deployment config-local-git --name <app_name> --resource-group myResourceGroup --query url --output tsv

Copy the output of the terminal, as the next step will be used.

Https: //JackMan@jackpython.scm.chinacloudsites.cn/jackPython.git

Push from Git to Azure
Add Azure remote capabilities to the local Git repository.

Git remote add azure <paste-previous-command-output-here>

Push to Azure remote capabilities to deploy applications. You will be prompted to enter the password you provided earlier during the creation of the deployment user.

Git push azure master

During deployment, Azure App Service informs Git of its progress.

Browse to the application
Use a web browser to browse to the deployed application.

http: // <app_name> .chinacloudsites.cn

At this point, the page displaying the Hello World message is running with Python code running as an Azure App Service web application.

Update and deploy code
Use a local text editor to open the main.py file in the Python application, and then slightly change the text in the string next to echo. Of course, you can also add more logic or push directly to other website examples.

Cho "Hello Azure, this is my python sample!";

Commit the changes made in git, and then push the code changes to Azure.

Git commit -am "updated output"
Git push azure master

After the deployment is complete, switch back to the browser window opened in "Browse to Application Steps" and click "Refresh".

Manage new Azure web apps
Go to the Azure portal to view the web application you just created.

To do this, log in to the Azure portal (https://portal.azure.cn).

Click "App Service" from the menu on the left, and then click the name of the Azure Web App.

Now you have entered the _Sidebar tab_ (the horizontally opened portal page) of the web application.

By default, the sidebar tab of the web application displays the Overview page. On this page, you can view the running status of the application. Here you can also perform basic management tasks, such as browsing, stopping, starting, restarting, and deleting. The tabs to the left of the sidebar tabs show different configuration pages that can be opened.

These tabs in the sidebar tabs show many powerful features that can be added to web applications. The following list only lists some of the available functions:

Mapping custom DNS names
Bind a custom SSL certificate
Configure continuous deployment
Expand and shrink
Add user authentication
congratulations! The first Python application is now deployed to the application service.

Step three: increase system availability

...

For more content, please click to see if the world is waiting for you

Welcome friends who are interested to communicate

A Graduate School Azurecommunity@qq.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.