In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
What this article shares with you is about the development and configuration of Serverless function calculation. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Basic concept 1. Service
Service is the unit of function computing resource management. There are many functions under the same service. These functions share the network configuration, permission configuration, storage configuration and log configuration of the service.
A service can correspond to an "application", which consists of a number of functions that have the same access, network configuration, and log to the same logstore. The configuration of these functions can be different. For example, under the same service, some functions have 3G memory, some functions have 512m memory, some functions are written in Python, some functions are written in Node.js.
Of course, if the application is complex, the same application can also correspond to multiple services, and there is no strong binding relationship here.
1) Service configuration
Several core configurations of the service:
* * Log configuration: * * if the developer's code runs on the function computing platform, how to view the log generated by the function running? In the Server development mode, the logs are typed into a unified file, collected in ElasticSearch through the log collection tool of Logstash/Fluentd, and viewed the logs and metrics through the visualization tool of Kibana. However, in the function calculation, the machine running the code is dynamically allocated by the function calculation, the developer cannot collect the log by himself, and the function calculation needs to help the developer deliver the log. Log configuration plays this role. Configure LogConfig to set the Project and Logstore of CLS, and the function calculation will deliver the logs generated during the function operation to the developer's Logstore.
However, in order to successfully deliver logs, it is not enough to configure Logtore alone. Function calculation does not have the permission to deliver logs to the developer's Logstore. Users need to grant the function calculation permission to write data to the specified Logstore. With this authorization, function calculation can properly deliver logs to the developer's Logstore.
* * File storage configuration: * * each function in function calculation is independent and executed in a different execution environment. What if users have some common files that want multiple functions to be shared? In the traditional Server-based development method, it is easy to put the public files on disk and read them all to the same location on the disk. The machines in function calculation are dynamically allocated by function calculation, and developers cannot save files to disk beforehand. What should I do? You can mount the NAS, and after mounting the NAS in the service, the function can access the files on the NAS as if it were the local file system.
* * Network configuration: * * Network configuration is the network access capability of the setting function. There are two main types. One is whether the public network can be accessed in the function. This is a Boolean switch, which is enabled by default. If you do not need to access the public network, you can disable the switch. The other is whether the function can access the specified VPC,VPC. The data in the private network is confidential and cannot be accessed through the public Internet. If you need a function to access resources in the VPC, such as the function needs to access the RDS in the VPC, you need to grant the function the ability to access the specified VPC. The principle is that the user is authorized to grant the ENI ENI to access the VPC, and the ENI is inserted into the machine that executes the user function in the FC, so that the function can access the resources in the VPC.
* * permissions: * * functional computing is a native cloud architecture that interacts with many services on the cloud. Aliyun has very strict permission restrictions, and functional computing does not have the ability to access other cloud resources of developers. When developers need functional computing to access other cloud services, they need to show permission to grant functional computing.
There are two main application scenarios for permissions: one is to grant the function computing permission to access other services, such as the authorization function mentioned earlier to access the developer's log service, and the authorization function to calculate and create ENI. The other is that the authorization function can access the developer's cloud resources. what is this? For example, what if the function needs to access the OSS to get the object, but doesn't want to expose the AK? Developers can configure that Role in the service has access to OSS. During function execution, assumeRole generates a temporary AK and stores the AK in the function's context context.credentials. Developers can use context.credentials.access_key_id/context.credentials.access_key_secret/context.credentials.security_token to create an OSS Client in the code.
two。 Function
Function is the core concept in "function calculation". Function is the basic unit of management and operation. A function usually consists of a series of configuration and runnable code packages.
1) function configuration
The configuration of the function is shown in the figure above:
Runtime is the type of environment in which the function runs: function computation currently supports development environments such as Node.js/Python/Java/C#/PHP, as well as Custom Runtime custom runtime.
Code is a function code package: the back end of the function calculation only recognizes the code package, and each development tool will automatically package it for you. For example, you can write code directly on the console, the console will automatically create / update functions for you, you can write / debug functions locally, deploy to function calculations through the command line tool Funcraft, and Funcraft will help you package
Handler is an entry function: you write a lot of functions in the code package, so where exactly does the function calculation start to be executed? it starts with the entry function you specify.
Timeout is the function timeout: if the function executes beyond this time, the function will be forced to stop execution.
MemorySize is the execution environment memory allocated for the function: currently, the value range is 128M~3G. If the function consumes more memory than the allocated memory, it will OOM.
Initializer is the initialization function: what's the use? When we introduced the execution environment of function calculation earlier, we said that function calculation will assign the execution environment to the function, there will be a cold start when the first allocation is made, and the function calculation will not be released immediately after the execution of the current request is completed. If a new request arrives within a period of time, the execution environment will be reused. The logic in Initializer is executed after the execution environment is assigned, and the same execution environment is guaranteed to be executed only once, so some time-consuming operations such as establishing connections and loading dependencies can be executed in the Initializer function.
InitializerTimeout is the maximum running time of the Initializer function.
3. Trigger
In the previous course, we introduced the rich types of event sources supported by function calculation. in the event-driven computing model, the event source is the producer of the event and the function is the handler of the event. Triggers provide a centralized and unified way to manage different event sources. When an event occurs, if the rules defined by the trigger are met, the event source will automatically call the function corresponding to the trigger.
Typical usage scenarios include processing objects uploaded to OSS, such as image processing, audio and video transcoding, decompressing OSS zip packets, cleaning, processing, transferring logs in SLS, triggering function execution at a specified time, and so on.
4. Version & alias
With the introduction of services, functions and triggers, developers can build applications based on function computing, but there is a new problem: * * developers need to update the code when they have new requirements, so how to ensure that online applications will not be affected? smooth iteration online? * * to solve this problem, version and alias are introduced in function calculation.
The version is equivalent to a snapshot of the service, including the configuration of the service, the function code within the service, and the function configuration. When you complete the development and testing, release a version, the version is monotonous increment, after the release, the released version cannot be changed, you can continue to develop testing on the Latest version without affecting the released version. When you call a function, you only need to specify the version to call the specified version of the function.
The new problem arises again. The name of the version is monotonously increasing as specified by the function calculation. Every time a version is released, there will be a new version. * * does the client have to change the code to execute the latest version after each release? * * to solve this problem, we introduced an alias, which is a pointer to a specific service version. After release, you only need to point the alias to the released version, and after the release, switch the alias to point to the latest version. The client only needs to specify an alias to ensure that the latest online code is called. At the same time, aliases support grayscale publishing, that is, 10% of the traffic points to the latest version, and 90% of the theory points to the old version. Rollback is also very simple, just point the alias to the previous version to quickly complete the rollback.
Development process
As shown in the figure above, developers first create services, set logs, permissions and other configurations, then create functions, write code development functions under the current version (Latest version), release the version after passing the test, release the version for the first time, and create an alias prod to point to version 1 to provide services.
The log of the function called by the client is recorded in the Logstore configured by the developer, and the function calculation provides a complete monitoring chart. After the application is launched, the developer can check the health status of the application through the monitoring chart and log.
When developers have new requirements, continue to change the code development function in the Latest version, and release the version after passing the test. This time, the release version is version 2. Switch 10% of the alias traffic to version 2 to achieve the gray release of the application. After observing that there is no problem for a period of time, you can switch 100% of the traffic to version 2.
The above is how the development and configuration of Serverless function calculation is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.