forked from docs/doc-exports
Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com> Co-authored-by: Chen, Junjie <chenjunjie@huawei.com> Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
104 lines
19 KiB
HTML
104 lines
19 KiB
HTML
<a name="functiongraph_01_1047"></a><a name="functiongraph_01_1047"></a>
|
|
|
|
<h1 class="topictitle1">Deploying a Function Using a Container Image</h1>
|
|
<div id="body32001227"><div class="section" id="functiongraph_01_1047__en-us_topic_0000001298667445_section11281162684319"><h4 class="sectiontitle">Introduction</h4><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p133229587281">Package your container images complying with the Open Container Initiative (OCI) standard, and upload them to FunctionGraph. The images will be loaded and run by FunctionGraph. Unlike the code upload mode, you can use a custom code package, which is flexible and reduces migration costs. You can create event and HTTP functions by using a custom image.</p>
|
|
<p id="functiongraph_01_1047__en-us_topic_0000001298667445_p144081946111019">For details about how to develop and deploy an HTTP function using a container image, see <a href="functiongraph_04_0103.html">Developing an HTTP Function</a>.</p>
|
|
<p id="functiongraph_01_1047__en-us_topic_0000001298667445_p10521125162810">For details about how to develop and deploy an event function using a container image, see <a href="functiongraph_04_0104.html">Developing an Event Function</a>.</p>
|
|
<div class="p" id="functiongraph_01_1047__en-us_topic_0000001298667445_p14413174110716">The following features are supported:<ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul1014620123414"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li191418203348"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b161132103520">Downloading images</strong><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p55351850105213">Images are stored in SoftWare Repository for Container (SWR) and can only be downloaded by users with the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b912369164218">SWR Admin</strong> permission. FunctionGraph will call the SWR API to generate and set temporary login commands before creating instances.</p>
|
|
</li></ul>
|
|
<ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul18282195091313"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li918794653413"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b1146644818397">Setting environment variables</strong><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p198712364017">Encryption settings and environment variables are supported. For details, see <a href="functiongraph_01_0154.html#functiongraph_01_0154">Configuring Environment Variables</a>.</p>
|
|
</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li4488153013512"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b15427191519409">Attaching external data disks</strong><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p1112717418582">External data disks can be attached. For details, see <a href="functiongraph_01_0402.html#functiongraph_01_0402">Configuring Disk Mounting</a>.</p>
|
|
</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1187872116363"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b183789149397">Reserved instances</strong><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p96162317366">For details, see the description about reserved instances.</p>
|
|
</li></ul>
|
|
<div class="note" id="functiongraph_01_1047__en-us_topic_0000001298667445_note119572599358"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p1170810123618">User containers will be started using UID 1003 and GID 1003, which are the same as other types of functions.</p>
|
|
</div></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="functiongraph_01_1047__en-us_topic_0000001298667445_section313719675517"><h4 class="sectiontitle">Prerequisites</h4><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p4726163318108">You have created an agency with the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b5902528173816">SWR Admin</strong> permission by referring to <a href="functiongraph_01_0920.html#functiongraph_01_0920">Configuring Agency Permissions</a>. Images are stored in SWR, and only users with this permission can invoke and pull images.</p>
|
|
</div>
|
|
<div class="section" id="functiongraph_01_1047__en-us_topic_0000001298667445_section29749266282"><h4 class="sectiontitle">Procedure</h4><ol id="functiongraph_01_1047__en-us_topic_0000001298667445_ol1758316312454"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li175831331114511">Log in to the FunctionGraph console. In the navigation pane, choose <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_en-us_topic_0000001251907924_b033610517502">Functions</strong> > <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_en-us_topic_0000001251907924_b183361950502">Function List</strong>.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1661111214614">On the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_en-us_topic_0000001251907924_b659423110517">Function List</strong> page, click <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_en-us_topic_0000001251907924_b1594331155116">Create Function</strong> in the upper right corner.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1040883174617">Select <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b20561730205112">Container Image</strong>. For details, see <a href="#functiongraph_01_1047__en-us_topic_0000001298667445_table6474815173514">Table 1</a>.<div class="fignone" id="functiongraph_01_1047__en-us_topic_0000001298667445_fig10875142222416"><span class="figcap"><b>Figure 1 </b>Creating a function using a container image</span><br><span><img id="functiongraph_01_1047__en-us_topic_0000001298667445_image1087513229245" src="en-us_image_0000001678918577.png"></span></div>
|
|
|
|
<div class="tablenoborder"><a name="functiongraph_01_1047__en-us_topic_0000001298667445_table6474815173514"></a><a name="en-us_topic_0000001298667445_table6474815173514"></a><table cellpadding="4" cellspacing="0" summary="" id="functiongraph_01_1047__en-us_topic_0000001298667445_table6474815173514" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameter description</caption><thead align="left"><tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row5472191543516"><th align="left" class="cellrowborder" valign="top" width="15.85%" id="mcps1.3.3.2.3.4.2.3.1.1"><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p164721153356">Parameter</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="84.15%" id="mcps1.3.3.2.3.4.2.3.1.2"><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p15472101511350">Description</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row647351523510"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p9472111511353">*Function Type</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p1647214157351">Select a function type.</p>
|
|
<p id="functiongraph_01_1047__en-us_topic_0000001298667445_p09118461423"></p>
|
|
<div class="p" id="functiongraph_01_1047__en-us_topic_0000001298667445_p107946498428"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b18170610111416">HTTP function</strong>: triggered once HTTP requests are sent to specific URLs.<div class="note" id="functiongraph_01_1047__en-us_topic_0000001298667445_note124731415103518"><span class="notetitle"> NOTE: </span><div class="notebody"><ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul144729151351"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1046744915279">The custom container image must contain an HTTP server with listening port 8000.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li164721615183514">HTTP functions support APIG and APIC triggers only.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1839714919261">When calling a function using APIG, <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b1654014294510">isBase64Encoded</strong> is valued <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b185401298519">true</strong> by default, indicating that the request body transferred to FunctionGraph is encoded using Base64 and must be decoded for processing.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1574044172716">The function must return characters strings by using the following structure.<pre class="screen" id="functiongraph_01_1047__en-us_topic_0000001298667445_screen2028202712279">{
|
|
"isBase64Encoded": true|false,
|
|
"statusCode": httpStatusCode,
|
|
"headers": {"headerName":"headerValue",...},
|
|
"body": "..."
|
|
}</pre>
|
|
</li></ul>
|
|
</div></div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row4575184013157"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p85751740121512">*Region</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p3575740181519">Select a region where you will deploy your code.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row047391510352"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p16473315153517">*Function Name</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p3473121513359">Name of the function, which must meet the following requirements:</p>
|
|
<ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul54731315113516"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li16473161518354">Consists of 1 to 60 characters, and can contain letters, digits, hyphens (-), and underscores (_).</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li24731715123517">Starts with a letter and ends with a letter or digit.</li></ul>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row9474131511358"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p1047319152357">*Enterprise Project</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p647319159351">Select a created enterprise project and add the function to it. By default, <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b1707237131016">default</strong> is selected.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row34742015173516"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p11474315163517">Container Image</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p12474191553517">Enter an image URL, that is, the location of the container image. You can click <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b12826201791217">View Image</strong> to view private and shared images. </p>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row547451553516"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p15474101553510">Container Image Override</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul14741215183511"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1474181533515"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b4700202851312">CMD</strong>: container startup command. Example: <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b18374162181320">/bin/sh</strong>. If no command is specified, the entrypoint or CMD in the image configuration will be used. Enter one or more commands separated with commas (,).</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li94742156356"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b109381521411">Args</strong>: container startup parameter. Example: <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b205155463335">-args,value1</strong>. If no argument is specified, CMD in the image configuration will be used. Enter one or more arguments separated with commas (,).</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li2474201520354"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b268890205511">Working Dir</strong>: working directory of the container. The folder path cannot be created or modified and must start with a slash (/). If no directory is specified, the directory in the image configuration will be used.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li84745156353"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b3989039181512">User ID</strong>: user ID for running the image. If no user ID is specified, the default value <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b952735893319">1003</strong> will be used.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li5474141563511"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b20247105512155">Group ID</strong>: user group ID. If no user group ID is specified, the default value <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b399365918339">1003</strong> will be used.</li></ul>
|
|
</td>
|
|
</tr>
|
|
<tr id="functiongraph_01_1047__en-us_topic_0000001298667445_row747413155359"><td class="cellrowborder" valign="top" width="15.85%" headers="mcps1.3.3.2.3.4.2.3.1.1 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p1347431553516">Agency</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="84.15%" headers="mcps1.3.3.2.3.4.2.3.1.2 "><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p14741515103516">Select an agency with the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b19542124631713">SWR Admin</strong> permission. To create an agency, see <a href="functiongraph_01_0920.html#functiongraph_01_0920__en-us_topic_0000001298507433_section17872123319473">Creating an Agency</a>.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="note" id="functiongraph_01_1047__en-us_topic_0000001298667445_note8475015203512"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="functiongraph_01_1047__en-us_topic_0000001298667445_ul347541516357"><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li9474201512352"><strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b7711730115810">Command</strong>, <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b14742183485812">Args</strong>, and <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b2032163920587">Working dir</strong> can contain up to 5120 characters.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li116601045102414">When a function is executed at the first time, the image is pulled from SWR, and the container is started during cold start of the function, which takes a certain period of time. If there is no image on a node during subsequent cold starts, an image will be pulled from SWR.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li156992027184912">Public and private images are supported. For details, see <a href="https://docs.otc.t-systems.com/software-repository-container/umn/image_management/setting_image_attributes.html" target="_blank" rel="noopener noreferrer">Setting Image Attributes</a>.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li13891636172512">The port of a custom container image must be 8000.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li1390615422610">The image package cannot exceed 10 GB. For a larger package, reduce the capacity. For example, mount the data of a question library to a container where the data was previously loaded through an external file system.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li133681917192613">FunctionGraph uses LTS to collect all logs that the container outputs to the console. These logs can be redirected to and printed on the console through standard output or an open-source log framework. The logs should include the system time, component name, code line, and key data, to facilitate fault locating.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li127784396113">When an out of memory (OOM) error occurs, view the memory usage in the function execution result.</li><li id="functiongraph_01_1047__en-us_topic_0000001298667445_li118281827115118">Functions must return a valid HTTP response.</li></ul>
|
|
</div></div>
|
|
</li></ol>
|
|
</div>
|
|
<div class="section" id="functiongraph_01_1047__en-us_topic_0000001298667445_section10470141217569"><h4 class="sectiontitle">Sample Code</h4><p id="functiongraph_01_1047__en-us_topic_0000001298667445_p197632055122512">The following uses <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b107251133124110">Node.js Express</strong> as an example. During function initialization, FunctionGraph uses the POST method to access the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b77261933134110">/init</strong> path (optional). Each time when a function is called, FunctionGraph uses the POST method to access the <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b472643311417">/invoke</strong> path. The function obtains <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b123813174427">context</strong> from <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b162382170425">req.headers</strong>, obtains <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b1023911171422">event</strong> from <strong id="functiongraph_01_1047__en-us_topic_0000001298667445_b424041720422">req.body</strong>, and returns an HTTP response struct.</p>
|
|
<pre class="screen" id="functiongraph_01_1047__en-us_topic_0000001298667445_screen14751413265">const express = require('express');
|
|
const app = express();
|
|
const PORT = 8000;
|
|
|
|
app.post('/init', (req, res) => {
|
|
res.send('Hello init\n');
|
|
});
|
|
|
|
app.post('/invoke', (req, res) => {
|
|
res.send('Hello invoke\n');
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Listening on http://localhost:${PORT}`);
|
|
});</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="functiongraph_01_0150_0.html">Building Functions</a></div>
|
|
</div>
|
|
</div>
|
|
|