Serverless cloud migration of traditional single-node websites

What is a function? Not long after I finished the math test, I immediately thought of independent variables, dependent variables, and function values, that is, y=f(x). Of course, in a computer, a function often refers to a defined code program, and we can call this defined function by passing parameters to achieve the functions we need. So, what is functional computing FC today?

In the era of cloud computing, DevOps, which is developed with containerization technology and various tools, has carried out a new coordination of development and operation and maintenance. After the developers have finished writing the code, they do not need to consider the environment and directly submit them to various pipelines. Testing, development, and deployment can be completed, the project builds microservices, and the container completes the encapsulation of the environment. But often we still need to devote energy to the cluster where the business goes online. Is it a private cloud environment or a public cloud? Is it a bare metal server or a cloud instance ECS? Buy or rent?

Of course, the implementation of DevOps and the operation and maintenance of servers and clusters all require a lot of resources and energy. DevOps is a shortcut, but not the only way out. The emergence of function computing FC has brought a serverless architecture, so that developers no longer feel the complexity of deploying services when developing and deploying.

Indifferent to the server, developers can really focus on their own code. Alibaba Cloud's serverless functional minimalist programming can focus on business innovation, without procurement and deployment costs, and provides complete observability capabilities such as monitoring and alarming. Alibaba Cloud Function Compute FC is an event-driven fully managed computing service . There is really no need to consider the operation and maintenance management of the server. You only need to upload the developed code. Function Compute will use the role strategy to plan computing resources and execute functions in a flexible way. , and finally execute the deployment efficiently. grace!

What are the applicable scenarios for Serverless? Is it a person? Or production? So this time I will deploy two applications in different directions to evaluate Serverless!

1. Notification system and Webhook, Trigger trigger and Chat robot. Push functions involved in many systems, such as email, SMS, and Webhook. Of course, the ability of Webhook is not only information notification, but the notification function referred to here must require infrastructure, that is, a server to support the operation. If these functions are directly operated by Serverless, we do not need to pay for the operation and maintenance of a server, saving A lot of work and expense. Similarly, we can use the serverless event-driven model to achieve timing and automatic triggering of tasks, and automatic check-in and automatic sending.

Second, the second, of course, is the application of the Web class. Based on the application deployment of various web frameworks and the construction of sites based on languages ​​such as Java, Python, and PHP, Serverless can easily implement blog applications such as wordpress. In conjunction with other cloud products, Codeup, OSS, RDS, etc., high-availability and high-performance web applications can be realized, such as the official Kod cloud disk system.

Cloud transformation of serverless agricultural products e-commerce website

Here I take a large project of the next door software major (I am a network technology) as a deployment case. The project is very simple and has a single function , but it is also a very classic front-end and back-end separation project. Since I am not very good at development, the specific technology of the project will not be ugly. It was said before that there is any copyright, so I will not release the source code hhhh.

Before the transformation, I believe that it is the classic AllinOne structure of many small and medium-sized companies . All the service middleware involved in the business is run on a server/virtual machine. Although it may be completely a laboratory environment now, the actual There are still many projects that do this. Not to mention the disadvantages, such as difficult deployment, operation and maintenance, difficulty in resource expansion, complicated follow-up transformation, poor performance, and no high-availability technology applied. . .

Serverless's agricultural product e-commerce platform is on the cloud , the project is relatively simple, and the planning is in the same region. Mainly, the front-end HTML page and the back-end Jar package are run by two separate serverless functions to complete the running calculation. The rest of the supporting services are implemented on the cloud using the corresponding cloud products. Here, the Redis service runs on my own ECS, so I no longer need to purchase the ApsaraDB for Redis version . Of course, Alibaba Cloud's Redis cloud database product is recommended.

1. Serverless service creation

Enter the Function Compute console, select [Services and Functions], click [Create Service], enter the name and description of the created service, enable the log function, and click OK to complete the creation of the service

After the service is created, go to [Service Details], find [Network Configuration], click [Edit], select Allow access to VPC, select Custom Configuration, select VPC, vSwitch, Security Group, and other supported cloud products (databases) are required here. etc) remain under the same VPC. Therefore, it is necessary to do a good job in the planning of the network on the cloud, and also check whether the product is in stock in the region.

2. Front-end page serverless function creation

After the service is created, click [Create Function], enter the function creation page, select [Use Custom Runtime Smooth Migration WebServer], enter the function name, select the running environment as [Nginx], upload the front-end html code and nginx configuration file, Select the listening port as 80, which is the service port of the original nginx middleware.

After the function is created, enter the function details interface, and you can edit and modify the code in the function code. After the code is changed, you need to click the deploy code to re-upload. As mentioned above, the Nginx service configuration needs to be uploaded together, and a copy of the /etc/nginx/mime.types file needs to be copied to the current directory to avoid mime file type mapping errors .

Put the key configuration of nginx here, modify it according to your own business situation, note that the port monitoring in the configuration needs to be consistent with the monitoring port created by the function, and the path of the webpage code is set to /code

http { include mime.types; #Note the introduction of this file keepalive_timeout 900; server { listen 80; server_name localhost; location / { root /code; index index.html index.htm; proxy_set_header Host $host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /users { proxy_pass http://serverless.Backend function access address.run ; #Nginx is passed to the backend function in reverse, and the address can be created after the subsequent backend function is created Copy} location /items { proxy_pass http://serverless.backend function access address.run ; #Nginx reverses the function passed to the backend, and the address can be copied after the subsequent backend function is created}

You can query related logs in the call log

When the function is triggered to run, Serverless will automatically start the instance according to the access request. Here, you can manually log in to the instance to check and troubleshoot the logs of the middleware service and the running environment.

3. Back-end Serverless function creation

Go back to the function service interface, click [Create Function] again, enter the function creation page, select [Use Custom Runtime Smooth Migration WebServer], enter the function name, select the running environment as [Java], and upload the packaged Jar package file. Modify the [Startup Command] and [Listening Port] according to the specific business. I need to monitor the business port as 8080, and need to pass in the parameters of the database connection at runtime. The database source username and password connection address configured here will be set in the subsequent ApsaraDB for RDS.

After the back-end function is created, enter the function details interface. The rest of the functions are the same as the above-mentioned front-end functions, and will not be repeated.

Click [Trigger Management], where the HTTP trigger provides the public network and intranet access addresses. We copy the intranet address and fill in this domain name in the reverse generation configuration of the front-end middleware to access the front-end and trigger the back-end function.

4. ApsaraDB for RDS MySQL Serverless creation, migration and import

The original Allinone architecture did not achieve independence and high availability of database services. In this cloud deployment, we will use the serverless version of ApsaraDB for Alibaba Cloud's public beta . RDS MySQL Serverless provides real-time elastic capabilities of CPU and memory. It has the advantages of low resource consumption, ease of use, flexibility, and low price. It rationally optimizes usage costs and further reduces costs and increases efficiency.

RDS MySQL Serverless instance creation

Enter the ApsaraDB for RDS console, click [Create Instance] to start the process of creating an instance, select the Serverless version on the [Basic Resources] setting interface, and select the rest according to the actual situation

When a Serverless RDS is created [Instance Configuration] , you need to pay attention to the settings of the network VPC , which must be consistent with the VPC set by the Serverless service to achieve intranet data exchange. After confirming the order, wait for the instance creation to complete.

RDS database user settings

After the instance is created, select Manage Instance. Select [Account Management] on the left task bar, click [Create Account] to create a database account to connect to the backend of the supplier platform. Enter the account name and password, select the normal account, and click OK to complete the user creation.

RDS business database creation

Select [Database Management] in the left column, select [Create Database], enter the library name required for the e-commerce business of agricultural products, and authorize the account to the user set in the previous step, click Create to complete the library setting.

RDS service connection address

The address of the connection library required in our Serverless function can be found at the [Database] connection in the rds instance, but the whitelist needs to be set in advance .

We copy the intranet address, and also complete the configuration of the connection user, password, and library, and then configure it into the serverless function or the back-end code.

Import recovery of RDS business database

The key to the migration of databases to the cloud is the migration of data content . The deployed business databases are few and small, so a simple backup SQL script file is used as the migration method. There are various ways to export data from MySQL/Mariadb, and backup and export can be performed according to actual needs. Of course, large-scale business databases have dedicated backup and migration tools, which are not detailed here. Enter the DMS data management service, select [Database Development], and click [Data Import] under [Data Change]. Import the database according to the specific backup method, here I choose to upload the sql script of the backup, submit the application to start importing the data

Data import is complete, data migration is complete

5. Redis service on cloud and static resource CDN

The Redis service is migrated to the cloud. As mentioned above, the Redis service is running on my own ECS, so I no longer need to purchase the ApsaraDB for Redis version separately. The ECS is also under the same VPC, which enables intranet interoperability . Of course, Alibaba Cloud's Redis cloud database product is recommended.

The CDN of static resources, including the acceleration of css\js\ images, has been stored in Alibaba's CDN service in the original architecture, so I won't make any changes here.

6. Public network business access domain name configuration

In the last step, the user finally accesses the front-end serverless function. Just like the prompt given by Alibaba Cloud, accessing the default public network address will not do any middleware analysis, but directly download the home page html static file, so we need to configure the access domain name by ourselves. .

Go back to the Function Compute console, select [Domain Name Management], click [Add Custom Domain Name], enter a custom domain name, configure routing, and select the service name, function name, and version number of the corresponding function LATEST (latest) CNAME that needs to be resolved Value, copy Copy CNAME record value, click [Cloud DNS Console], enter the resolution settings, click [Add Record], select the record type [CNAME], enter the host header, fill in the copied record value, and confirm to complete the addition

7. The effect of serverless cloud migration of agricultural products e-commerce project

Home page, access effect, front-end functions are correct

Agricultural product details page visit

User registration function test, database connection and writing are correct

Data in RDS has been successfully written

User login test

Order submission test, the backend function is correct

Quickly build a litemall e-commerce application system using serverless application templates

1. Create an application based on the official template

Serverless applications provide a large number of official application templates. We can modify our own business according to the templates given, so it is also important to be familiar with the deployment of templates. Go to the console page of Function Compute FC, click [Apply], select [Create Application from Template], and select [Mall Case]

View the information of the deployment template through details, and query the source code. Click Create Now to quickly experience the creation of Serverless applications. Local deployment can be deployed through the ServerlessDev tool.

2. Configure the application creation

After clicking Create Now, we perform the initial configuration of the application. There are two types of deployment: 1. Deploy through a third-party code repository, and 2. Deploy directly. The difference between the two is that you can use your own repository code to update the project and publish it later, while direct deployment requires manual configuration. As you can see here, our delivery trigger is also based on the Git repository push submission, and each submission will automatically trigger the deployment. If you configure the application yourself, you need to configure the s.yaml file according to the business, refer to: https://www.serverless-devs.com/fc/yaml/readme

Here I choose the Gitee warehouse for deployment, but the authorization of the third-party application of the warehouse is required

Click to authorize, jump to the gitee site to make an OAuth authorization request, and click to agree to the authorization

When Ali users use FC function calculation for the first time, they need to add a role strategy. I have already used FC here. If you are prompted to add a strategy, click Add according to the prompt.

Other advanced configurations need to be modified according to the business. Here, pay attention to the selection of the region. Other elastic resources will be located in this region. I choose the local Hangzhou here.

After completing the configuration, click Create, and the code has been newly uploaded to my Gitee repository. The s.yaml provided here can be used as a reference for configuration, and then modify the yaml according to the deployed business

3. Application deployment goes online

The application is created and deployed automatically for the first time. Here you can see the deployment status is being deployed.

View the deployment log. If there is an error in the deployment, you can also query the log information to report the error. After the deployment has gone through the four steps of pre-environment, resource synchronization, resource inspection, and execution of deployment, our e-commerce application has been deployed.

The first deployment is completed, and it is also a deployment version of the latest latest, which can be rolled back freely through the deployment history

4. Access to deployed e-commerce applications

Visit the tested domain name, you can see our online litemall e-commerce system, and enter the background management of the e-commerce application

The litemall e-commerce system is an open source e-commerce system with WeChat applet separated from the front and back ends. It has membership management, mall management, commodity management, promotion management, system management, configuration management, and statistical reports based on the e-commerce platform.

The litemall e-commerce system needs to be configured with the following minimum development environments: MySQL JDK1.8 or above Maven Nodejs

5. Change business domain name access

Similarly, when we normally launched the FC business, Serverless used the default access address function to calculate the domain name provided online. The default is ..fc.aliyuncs.com//proxy///[action?queries]. For normal business access, we must modify the domain name of the visit.

Go to the home page of Function Compute FC, click Domain Name Management under Advanced Functions, here you can see that the default domain name when we launch the e-commerce application has already routed information, we choose to add a custom domain name

Enter the name of the domain name, that is, the custom second-level domain name under the domain name that you purchased for record. Click on the routing configuration, select the service name, here is the e-commerce system litemall we deployed, select the function name and version to copy the public network CNAME address, and then in the DNS Add resolution to the domain name management office

Enter the domain name management, add a record, select CNAME for the record type, enter the host record, paste the public network CNAME address just copied to the record value, and click Add.

Go back to Function Compute FC, click Create at the end, and go back to the home page to see our newly bound domain name

Finally, use the mobile phone to access the public network address of my custom configuration. The e-commerce service goes online normally, and the public network address accesses the homepage normally.

Product details purchase page

at last

Compared with other solutions, Serverless is also a very easy-to-use and efficient technical solution. In fact, there is still a lot of room for improvement in the above deployment test, but I want to focus on the serverless server inaction . I have also used a lot of Alibaba Cloud technology products, and I know how difficult it is to operate and maintain the underlying infrastructure. .

Although these months have been busy with study and life, I am still very interested in various activities in the community and want to do some better tests. This time Serverless is for both individual users and enterprise users. Both are a very good choice, flexible resources and pay-as-you-go, saving resources and Money, and more elegant!

This article is reproduced from the Function Calculation Call for Papers, author Tao Yuhang

For more content, pay attention to the Serverless WeChat official account (ID: serverlessdevs), which brings together the most comprehensive content of serverless technology, regularly holds serverless events, live broadcasts, and user best practices.

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4611872/blog/5560916