Original title: One article to understand the serverless cloud of traditional single node websites
Introduction: AliCloud Function Computing FC is an event driven fully hosted computing service. It really doesn't need to consider the operation and maintenance management of the server. It just needs to complete the developed code and upload it. Function computing will plan computing resources through role strategies, execute functions elastically, and finally implement deployment efficiently. Elegant!
Author: Tao Yuhang
This article is selected from the "Serverless function calculation solicitation order" solicitation, and the background replies "function" for details
background
What is a function? Just after finishing the math exam, I immediately thought of independent variable, dependent variable and function value, that is, y=f (x). Of course, in a computer, a function often refers to a defined code program. We can call this defined function by passing parameters to achieve the functions we need. So, what is FC in today's function calculation?
In the current era of cloud computing, DevOps, which has developed container technology and various tools, has made a new overall plan for development and operation and maintenance. After completing the code writing, developers can directly submit it to various pipelines to complete testing, development, and deployment. The project builds microservices, and the container completes the packaging of the environment. But often we still need to invest in 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? Is it self purchase or rent?
Of course, the landing of DevOps and the operation and maintenance of server clusters require a lot of resources and energy. DevOps is a shortcut, but not the only way out. The emergence of FC for functional computing has brought a serverless architecture, so that developers no longer feel the complexity of deploying services when developing and deploying.
The insensitivity to the server allows developers to really focus on their own code. Alibaba Cloud Serverless functional minimalist programming can focus on business innovation, without purchase and deployment costs, and provide complete observable capabilities such as monitoring and alarm. AliCloud function computing FC is an event driven fully hosted computing service. It really doesn't need to consider the operation and maintenance management of the server. It just needs to upload the developed code. Function computing will use role strategies to plan computing resources, execute functions flexibly, and finally implement deployment efficiently. Elegant!
What scenarios will Serverless have? 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 just information notification, but the notification function here must be supported by the infrastructure, that is, the server. If these functions are directly operated by Serverless, we do not need to pay for the operation and maintenance of a server, which saves a lot of work and costs. In the same way, we can use the Serverless event driven model to automatically trigger tasks on a regular basis, and automatically send check ins.
2、 The second, of course, is the application of Web classes.
Based on the application deployment of various Web frameworks, build websites based on Java, Python, PHP and other languages. Serverless can easily launch blog applications such as wordpress. Together with other cloud products, such as Codeup, OSS, RDS, etc., it can also realize highly available and high-performance web applications, such as the official Kod cloud disk system.
Cloud transformation on Serverless agricultural product e-commerce website
Here I take a big project of the software major next door (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 rear end separation project. Because I am not good at development, the specific technology of the project will not show shame. It is said that there is any copyright before, I will not put the source code hhhh.
Before the transformation, I believe that it is the classic AllinOne structure of many small and medium-sized companies' businesses. It runs all the service middleware involved in the business on a single server/virtual machine. Although it may be completely a laboratory environment now, it is still seen that many projects do so. There is no need to mention the disadvantages. It is difficult to deploy, operate and maintain, expand resources, carry out complex subsequent transformation, perform poorly, and do not apply high availability technology...
Serverless's agricultural products e-commerce platform is on the cloud. The project is relatively simple, and the planning is in the same region. It is mainly to run the front-end HTML page and the back-end Jar package by two separate Serverless functions. The rest of the support services are implemented in the cloud using the corresponding cloud products. Here, the Redis service is not purchased separately because I have the Redis service running on my own ECS. Of course, Alibaba Cloud's Redis cloud database product is recommended.
1. Serverless service creation
Enter the function calculation console, select Service and Function, and click Create Service
Enter the name and description of the service to be created, enable the log function, and click OK to complete the service creation
After the service is created, enter [Service Details], find [Network Configuration], and click [Edit]
Select Allow Access to VPC, select Custom Configuration, and select VPC, vSwitch, and Security Group. Here, you need to keep the same VPC with other cloud products (databases, etc.) supported later. Therefore, it is necessary to plan the cloud network and check whether the products are in stock in the region.
2. Create the serverless function on the front page
After creating the service, click [Create Function] to enter the function creation page
Select [Smooth migration of WebServer using custom runtime], enter the function name, select [Nginx] as the running environment, upload the front-end HTML code and Nginx configuration file, and select the listening port as 80, which is the service port of the original Nginx middleware.
After the function is created, you can enter the function details interface to edit and modify the code in the function code. After the code changes, you need to click Deploy Code to upload again. As mentioned above, the Nginx service configuration needs to be uploaded together, and a copy of the/etc/nginx/time.types file needs to be copied to the current directory to avoid incorrect mapping of the mime file type.
Here is the Nginx key configuration, which can be modified according to your own business situation. Note that the port listening in the configuration needs to be consistent with the function creation listening port, and the path of the web page code is set to/code
http {
include mime.types; # Pay attention to introducing 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 //serverless. Back end function access address.run;
#Nginx reverses the generation of the function passed to the backend. The address can be copied after the subsequent backend function is created
}
location /items {
proxy_pass //serverless. Back end function access address.run;
#Nginx reverses the generation of the function passed to the backend. 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. You can log in the instance manually here to check and troubleshoot the middleware service log and running environment.
3. Creation of backend serverless function
Return to the function service interface, click Create Function again to enter the function creation page
Select [Use custom runtime to migrate WebServer smoothly], enter the function name, select [Java] as the running environment, and upload the packed Jar package file.
Modify the [Start Command] and [Listening Port] according to the specific business. Here, I need to listen to the service port 8080 and pass in the database connection parameters at runtime. The database source user name, password, and connection address configured here will be set in the subsequent RDS.
After the back-end function is created, enter the function details interface. Other functions are the same as the front-end functions above, and will not be repeated
Click [Trigger Management], where the HTTP trigger provides the public and private network access addresses. We copy the private network address and fill in this domain name in the front-end middleware anti generation configuration to access the front-end trigger back-end functions
4. Creation, migration and import of RDS MySQL Serverless
The original Allione architecture did not achieve the independence and high availability of database services. In this cloud deployment, we will use the cloud database serverless version in Alibaba Cloud's public test. RDS MySQL Serverless provides the real-time flexibility of CPU and memory. It has the advantages of low resource consumption, simple and easy to use, flexible and low price. It optimizes the use cost reasonably to further reduce costs and increase efficiency.
RDS MySQL Serverless instance creation
Enter the RDS console, click [Create Instance] to start the process of creating an instance. On the [Basic Resources] setting interface, select Serverless, and select the rest according to the actual situation
When creating the Serverless RDS, the [Instance Configuration] needs to pay attention to the setting of the network VPC, which should be consistent with the VPC set by the Serverless service, to realize the interconnection of intranet data. After confirming the order, wait until the instance creation is completed
RDS database user settings
Complete the instance creation and select the management instance. Select [Account Management] on the left task bar, and click [Create Account] to create a database account and connect to the backend of the power supplier platform. Enter the account name and password, select a common account, and click OK to complete the user creation
RDS business database creation
Select Database Management in the left column and select Create Database
Enter the database name required for agricultural products e-commerce business, authorize the account to the user set in the previous step, and click Create to complete the database setting.
RDS service connection address
The address of the database to be connected 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 complete the configuration of the connection user, password, and library, which can be configured into the Serverless function or back-end code
Import and recovery of RDS business database
The key to the database cloud is the migration of data content. The deployed business databases are very few and small, so a simple backup SQL script file is used as the migration method. There are many ways to export MySQL/Mariadb data, which can be backed up and exported according to actual needs. Of course, large-scale business databases have special backup migration tools, which will not be 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 for backup and submit an application to start importing data
Data import and data migration are completed
5. Cloud and static resource CDN on Redis service
The Redis service is on the cloud. As mentioned earlier, since the Redis service is running on my own ECS, I will no longer purchase the Redis version of the cloud database separately. The ECS is also under the same VPC and can achieve intranet interoperability. Of course, Alibaba Cloud's Redis cloud database product is recommended.
The CDN of static resources, including the acceleration of cssjs images, has been stored in Alibaba's CDN service in the original architecture, so I won't make more changes here.
6. Domain name configuration for public network service access
In the last step, the user finally accesses the serverless function on the front end. As Alibaba Cloud prompts, accessing the default public network address will not do any middleware analysis, but directly download the HTML static file on the home page. Therefore, we need to configure our own access domain name.
Return to the function calculation console, select Domain Name Management, and click Add Custom Domain Name
Enter the customized domain name, configure the route, and select the service name, function name, and version number of the corresponding function LATEST
Copy the CNAME value to be resolved
Copy the CNAME record value, click [Cloud Resolution DNS Console], enter the resolution settings, and click [Add Record]
Select the record type [CNAME], enter the host header, fill in the copied record value, and confirm that the addition is complete
7. Cloud effect of agricultural product e-commerce project Serverless
Home page, access effect, front end function is correct
Visit agricultural product details page
User registration function test, database connection and writing are correct
Data in RDS has been successfully written
User login test
Order submission test, the back-end function is correct
Use the Serverless application module to quickly build the lite mall e-commerce application system
1. Create an application based on the official template
The serverless application provides a large number of official application templates. We can modify our business according to the templates provided, so it is also important to be familiar with template deployment.
Enter the console page of function calculation FC, click [Application], select [Create an application through template], and select [Mall case]
View the deployment template information and query the source code in details. Click Create Now to quickly experience the creation of Serverless applications. Local deployment can be deployed through the Serverless Devs tool
2. Configure the created application
Click Create Now to initialize the application.
There are two deployment types: 1. Deploy through a third-party code repository, 2. Deploy directly
The difference between the two is that you can use your own warehouse code to update and publish projects later through push, while direct deployment requires manual configuration.
Here we can see that our delivery trigger is also based on the Git warehouse 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:
//www.serverless-devs.com/fc/yaml/readme
Here I choose gitee warehouse for deployment, but the third-party application of the warehouse needs authorization
Click to authorize, jump to gitee's site to make OAuth authorization request, and click to agree to authorize
If Alibaba users need to add role policies when using FC functions to calculate for the first time, I have used Alibaba Cloud functions to calculate FC. If you are prompted to add policies, click Add.
Other advanced configurations need to be modified according to the business. Pay attention to the region selection here. Other subsequent elastic resources will be in this region. Here I choose local Hangzhou.
After completing the configuration, click Create. The code has been created and uploaded to my gitee repository. The s.yaml provided here can be used as a reference for configuration. Later, you can modify the yaml according to the deployed business
3. Application deployment goes online
After the application creation is completed, it will be deployed automatically for the first time. You can see the deployment status here
View the deployment log. If an error occurs in the deployment, you can also query the error message from the log information.
After four steps of deployment: front environment, resource synchronization, resource check, and deployment execution, our e-commerce application is deployed
The first deployment is completed, which is also the first deployment version of the latest, and can be rolled back freely through the deployment history
4. Access to deployed online e-commerce applications
By visiting the domain name tested, we can see our online Litemall e-commerce system and enter the background management of e-commerce applications
Litemall e-commerce system is an open source e-commerce system with WeChat applet at the front and rear ends. It has member management, mall management, commodity management, promotion management, system management, configuration management and statistical report based on e-commerce platform.
Litemall e-commerce system needs to be configured with the minimum development environment: MySQL, JDK1.8 or above, Maven, Nodejs
5. Change business domain name access
Similarly, when we normally launch the FC service, Serverless uses the default access address
Alibaba Cloud function calculates that the domain name provided by FC online is based on Fc. aliyuncs. com//proxy///[action? Queries] is the default. For normal business access, we must modify the domain name.
Go to the home page of AliCloud function computing FC, click domain name management under advanced functions, and you can see the routing information of the default domain name when we launched e-commerce applications
We choose to add a custom domain name
Enter the name of the domain name, that is, the user-defined secondary domain name under the domain name purchased for filing
Click the route configuration and select the service name. Here is our deployed e-commerce system lite all. Select the function name and version
Copy the public network CNAME address, and then add the resolution in the DNS domain name management office
Enter the domain name management, add a record, select CNAME as the record type, enter the host record, paste the public network CNAME address just copied to the record value, and click Add
Back to Alibaba Cloud function to calculate FC, click Create at the end, and return to the home page to see our newly bound domain name
Finally, use your mobile phone to access the public network address configured by me
The e-commerce service is normally online, and the public address is normally accessed to the home page
Product details purchase page:
last
Compared with other solutions, Serverless is also a very easy and efficient technical solution. In fact, there are still many areas to be improved in the above deployment test, but I want to focus on the serverless server insensitivity in the case. I have also used many Alibaba Cloud technology products, and I know the difficulty of operation and maintenance of the underlying infrastructure.
Although I have been busy studying and living in recent months, I am still very interested in various activities in the community and want to do some better tests. This Serverless is for both individual users and enterprise users. It is a very good choice. Elastic resources and pay as you go can save resources and money more and be more elegant!
This article is reprinted from: Tao Yuhang, the blogger of Function Calculation Evaluation Bureau
Original link: //developer.aliyun.com/article/980852
Official opening! On August 11, Alibaba Cloud Apsara Technology Summit invited you to explore cloud native best practices
Original link: //click.aliyun.com/m/1000356127/
This article is original content of Alibaba Cloud and cannot be reproduced without permission. Go back to Sohu to see more
Editor in charge: