Use Visual Studio and AWS Beanstalk to publish highly available web applications

Background introduction

What developers like to do is to manage and publish code directly through the IDE. The AWS Toolkit allows developers to publish applications directly to the AWS cloud platform, and easily implement deployment calls and development, and realize the integration of applications through the Elastic Beanstalk stack. Key deployment, code version management, database management, load balancing and automatic expansion, etc. AWS Elastic Beanstalk is an easy-to-use service for deploying and expanding on familiar servers (such as Apache, Nginx, Passenger, and IIS) using Java, .NET, PHP, Node.js, Python, Ruby, GO, and Docker Web applications and services developed.

We only need to upload the code, and Elastic Beanstalk can automatically handle the deployment from capacity provisioning, load balancing, and automatic scaling to application health monitoring. At the same time, it can fully control the AWS resources that support the application, and can access the underlying resources at any time.

Elastic Beanstalk does not charge extra, and only pays for the AWS resources required to store and run the application.

The AWS Toolkit for Visual Studio is a plug-in for the Visual Studio integrated development environment (IDE). The toolkit makes it easier for developers to use Amazon AWS to develop, call, and deploy .NET applications.

Experiment preparation

Official tool download address: https://aws.amazon.com/cn/visualstudio/

Alternate address:

AWS Toolkit for Visual Studio 2017和2019 https://s3testlq01.s3.cn-north-1.amazonaws.com.cn/AWSToolkitPackage.vsix
AWS Toolkit for Visual Studio 2013-2015 https://s3testlq01.s3.cn-north-1.amazonaws.com.cn/AWSToolsAndSDKForNet_sdk-3.3.606.0_ps-3.3.604.0_tk-1.14.5.0.msi

Experiment description

Involving AWS components:
AWS Elastic Beanstalk
AWS Toolkit for Visual Studio
Application Load Balance
experimental process
Environment preparation
Configure Visual Studio
configure Elastic Beanstalk
blue-green deployment
Automatic expansion and load balancing
Conversion load balancer

Environmental preparation

The environment chosen for this demonstration is Visual Studio 2019, you can refer to the official Visual Studio tutorial to install and download https://visualstudio.microsoft.com/zh-hans/vs/

After downloading the AWS Toolkit for Visual Studio 2019, execute the VSIX installer, and you need to close Visual Studio during execution.

Configure Visual Studio

To publish the project in the Explorer of Visual Studio, you can see Publish to AWS Elastic Beanstalk....

Go back to IAM in the AWS console and select users. In this test, users with admin permissions are selected. However, in the production environment, in accordance with the principle of minimizing permissions, you can set what permissions specific users have in the IAM Policy. .


Here we directly choose to import the csv file, of course, you can also manually fill in the Access Key and Secret Key into Visual Studio.

Account Number is our AWS account number.

Select China Region for Account Type, otherwise select global Region.

Note here: After clicking OK, close the operation interface of Publish to Amazon Web Services, and then republish the project Publish to AWS Elastic Beanstalk...

We can publish the project or application to Beijing (BJS) or Ningxia (ZHY). Here we choose the Region of Beijing.

—In this step, define the name of your own application: application name

—Define the name of your own application environment: Environment. It is recommended to define the application name -test or -dev or -prod. Because the blue-green deployment of Elastic Beanstalk will be introduced later, we can seamlessly switch our development and production environments.

—The last step is to check whether our domain name is occupied: Check availability...

Next, define the software environment. It mainly includes two operations: One is the OS environment for running the application. The second is the resource configuration that the application runs.

We will find that there are a lot of instance types to choose from. Since the environment running is windows server, it is recommended to at least C5.xlarge and above instance types 4vcpu, 8GiB RAM.

There is another configuration not to be ignored, that is, Key pair. This is the key for logging in to the instance virtual machine. Make sure this key file is in your own hands, if not, please create it.

The next step is the security side, which is the Role of Elastic Beanstalk. It means which AWS resources our deployed application server can interact with. For testing, we directly assign a default role to it.

The next step is to define the Build and IIS settings, such as App pool runtime, App path, etc.

If there is no problem, Deploy can be released.

All the states of ElasticBeanstalk released applications are traceable, as we can see on the Events log.

You can see that it stores the code on S3, creates a Security Group, creates an EIP, creates an EC2, and so on.

After the release is successful, we can directly click on the URL to verify whether the project is successfully released.

Next, we modify the View/Home/index.html file as another production environment.

When publishing, you can either overwrite the existing environment or create a new environment. In order to test, we first cover the existing environment. The blue-green deployment part will be introduced later.

In less than 1 minute, the updated code environment is deployed.

配置Elastic Beanstalk

Now all development and deployment work is achieved through Visual Studio, so what exactly does Elastic Beanstalk look like in the AWS console?

The configuration, monitoring, and version management of the application can be configured on the Elastic Beanstalk console.

On the monitoring page, you can see the resource utilization rate of the server where the application is located, as shown in the following figure:

In the in-app interface, you can roll back to any app version. For example, this time we released two versions of the application.

In the configuration page, you can see the detailed configuration of ElasticBenstalk, as shown below:

We can also add Amazon RDS SQL database to our deployment and testing environment. AWS Elastic Beanstalk provides connection information to the instance by setting the environment properties of the database host name, user name, password, table name, and port. When a database is added to the environment, its life cycle will be associated with the life cycle of the environment. For a production environment, it is recommended to configure the instance to connect to an external database. For details, please refer to: https://docs.amazonaws.cn/elasticbeanstalk/latest/dg/using-features.managing.db.html?icmpid=docs_elasticbeanstalk_console

For example, we add the Amazon RDS SQL Server EE database to the deployment and test environment, select the version and type of the database, etc. Here, in order to ensure the high availability of the database, we can choose Multi-AZ:

Blue-green deployment

Blue-green deployment concept:
Blue-green deployment is the most common method of 0 downtime deployment. Downtime can be avoided by performing blue/green deployment. The principle of blue-green deployment is very simple, which is to solve the problem through redundancy. Usually the production environment requires two environments, one is prod and the other is test or dev. When users access our application, the active server cluster prod (green environment) is often used. When you want to update your application, publish the updated application to test or dev (blue environment). After the test is completed, switch between prod and test. surroundings. If there is a problem with the operation, you can quickly roll back to the green environment through the load balancer.

Elastic Beanstalk exchanges the alias records of the old environment and the new environment to redirect traffic from the old version to the new version, and vice versa.

We can publish a new environment (blue environment) through Visual Studio. The specific steps are not repeated, and are consistent with the previous ones, as shown in the following figure:

Now the second environment is created, the blue environment dev, as shown below:

Back to the AWS console, there are two environments on Elastic Beanstalk,

The blue-green environment switch is completed by exchanging the URL, and the environment switch is completed in about 5 seconds. Do not terminate the old environment until the DNS changes are propagated and the old DNS records expire. The DNS server does not necessarily clear the old records in its cache based on the time-to-live (TTL) set in the DNS record where the application is located. In addition, when the application uses a production database, blue/green deployment requires your environment to run independently of the production database. If an Amazon RDS database instance is attached to the Elastic Beanstalk environment, data will not be transferred to the second environment, so the production environment recommends that the RDS database be independent of the Elastic Beanstalk stack. At this time, when we visit the previous test environment, we will find that we have switched to the dev environment, as shown in the following figure:

The next step is the high availability of the website, load balancing and elastic expansion and reduction

Automatic expansion and load balancing
. Modify the capacity in the Elastic Beanstalk console, switch the single instance to load balancing, and then set the trigger conditions. Here we take the CPU usage rate as an example, automatically expand 1 instance if it is higher than 70%, and it is lower than 30 % Reduce 1 instance.

Then apply and wait for Elastic Beanstalk to update the environment. At this time, the operating status will switch to gray, as shown in the following figure:

The next step is to access our application through ELB's DNS. The ELB load balancer finds the load balancer DNS name in the load balancer on the EC2 operation page and copies it to the browser.

Conversion load balancer

We may have noticed that the load balancer used by Elastic Beanstalk is CLB. We can migrate it to ALB by enabling the ALB migration wizard, but it should be noted that ALB must specify subnets in different availability zones. The specific operation steps are done with the migration wizard, so I won't repeat them. After the conversion is successful, we can access the .NET application through ALB, as shown in the following figure:

In this way, the entry point of our application is the load balancer ALB, even when the blue-green deployment environment is switched, there is no need to switch the entry URL.

to sum up

The combination of AWS Toolkit for Visual Studio and Elastic Beanstalk is easy to use and greatly improves development efficiency. Utilizing AWS's PaaS database and the elasticity of cloud computing improves application security and reduces the work of operation and maintenance deployment. In addition, you can also use the AWS SDK for .NET to help develop native .NET APIs, and use AWS Tools for PowerShell to directly access management and interact with AWS services in the form of PowerShell scripts. If our application is .NET Core, we can also implement the microservice architecture through AWS ECS.

Detailed video: https://edu.51cto.com/center/course/lesson/index?id=517074
Blog reference: https://amazonaws-china.com/cn/blogs/china/using-visual-studio-aws -elastic-beanstalk-publish-net-applications/

Guess you like

Origin blog.csdn.net/u010478127/article/details/106813613