打包 Golang 微服务


打包 Golang 微服务

本节以使用 Golang 开发并运行于 SylixOS 的微服务为例介绍微服务打包方法。

操作步骤

步骤 1:安装基础环境

  1. 建议安装 Visual Studio Codeopen in new window 开发环境进行文本编辑。
  2. 安装 Node.jsopen in new window 运行环境以支持容器命令行打包工具。

步骤 2:安装打包工具

在 Node.js 安装完成后,使用如下命令安装 ECS Commander(简称 ECSC)命令行工具。

npm install -g ecsc

步骤 3:生成容器 Bundle

一个容器在打包前,应创建一个含有容器内 "根目录" 与 "config.json" 配置文件的目录结构,该目录结构的组合被称为容器 Bundle。用户可以使用交互方式或 Ecsfile 脚本方式生成容器 Bundle。

  • 使用交互方式

    1. 在用户自定义的工作目录中,使用如下命令生成容器 Bundle。

      D:\> cd ecs
      D:\ecs> ecsc create
      
    2. 命令执行后将启动容器 Bundle 生成交互流程,用户需回答以下问题:

      • 创建的容器 Bundle 名称。
      • 容器 Bundle 的目标设备架构,目前支持的架构有 x86-64、ARM64、ARM、MIPS64 和 LoongArch。
      • 是否使用宿主机中的 JSRE 运行环境。
      • 容器的默认启动参数。

      下面以创建一个目标设备为 "arm64"、名称为 "demo"、不使用宿主机中的 JSRE 运行环境、且启动参数为 /apps/HelloVSOA" 的容器 Bundle 为例,介绍容器 Bundle 的创建过程。

         _____________ _____                              __       
        / __/ ___/ __// ___/__  __ _  __ _  ___ ____  ___/ /__ ____
       / _// /___\ \ / /__/ _ \/  ' \/  ' \/ _ `/ _ \/ _  / -_) __/
      /___/\___/___/ \___/\___/_/_/_/_/_/_/\_,_/_//_/\_,_/\__/_/   
      
      ? What is name for the bundle (directory)? demo
      ? What is the architecture(s) of the bundle? arm64
      ? Would you mount and reuse JSRE from the container host? No
      ? What is the start parameter (process.args) of the image? /apps/HelloVSOA
      + created demo\config.json
      + created demo\rootfs\etc\startup.sh
      + created demo\rootfs\apps
      + created demo\rootfs\home
      + created demo\rootfs\bin
      + created demo\rootfs\qt
      + created demo\rootfs\boot
      + created demo\rootfs\dev
      + created demo\rootfs\lib
      + created demo\rootfs\proc
      + created demo\rootfs\root
      + created demo\rootfs\sbin
      + created demo\rootfs\tmp
      + created demo\rootfs\usr
      + created demo\rootfs\var
      + done!
      
  • 使用 Ecsfile 脚本

    ECSC 即将支持直接指定 Ecsfile 脚本生成容器 Bundle,敬请期待。

Bundle 将在当前工作目录中被创建成功。

说明:

  • 创建 Bundle 时,会自动在 /etc/startup.sh 中添加了如下命令设置容器的默认 Shell 栈大小,以满足运行 Go 应用默认 Shell 栈大小需大于 128 KB + 64 KB 的要求。
shstack 200000
  • 创建 Bundle 时,config.json 配置文件中的 "process.env" 将会自动添加容器内云原生 SDK 库的路径配置:
"process": {
  "env":  ["LD_LIBRARY_PATH=/qt/lib:/usr/lib:/lib:/usr/local/lib:/lib/jsre:/lib/vsoa", 
          ……],
},

步骤 4:选择打包文件

用户可以手动选择打包文件,也可以通过 Ecsfile 脚本自动选择打包文件,下面将分别针对两种方式进行介绍。

  • 手动选择打包文件

    将需打包的 Go 可执行文件手动拷贝至 Bundle 内,如将 HelloVSOA 拷贝至 "ecs/demo/rootfs/apps" 目录中。

    D:\ecs> dir demo\rootfs\apps
    驱动器 D 中的卷是 Data
    卷的序列号是 32DC-66F6
    
    D:\ecs\demo\rootfs\apps 的目录
    
    2023/03/21  16:28    <DIR>          .
    2023/03/21  16:20    <DIR>          ..
    2023/03/21  16:28             1,901 HelloVSOA
    
  • 使用 Ecsfile 脚本

    ECSC 即将支持通过 Ecsfile 的脚本自动拷贝文件至 Bundle 内,敬请期待。

步骤 5:配置配额参数

参考 容器配置open in new window 配置打包模板中 config.json 配置文件的其他配额信息。

步骤 6:完成打包操作

  1. 执行如下命令,完成 Golang 微服务打包。

    D:\ecs> ecsc pack demo -t demo:latest demo.tar
    

    ecsc pack 命令说明:

    ecsc pack <bundle> [-t name[:tag]] [tarball_path]
    

    参数说明

    参数说明
    bundle待打包的 Bundle 路径
    name打包的镜像包名称
    tag镜像的标签,如 latest
    tarball_path打包镜像生成路径,后缀必须为 ".tar"
  2. 打包完成后,可在当前工作目录中得到打包后的容器镜像 demo.tar。

    D:\ecs> dir
    驱动器 D 中的卷是 Data
    卷的序列号是 32DC-66F6
    
    D:\ecs 的目录
    
    2023/03/21  16:32    <DIR>          .
    2023/03/21  15:34    <DIR>          ..
    2023/03/21  16:20    <DIR>          demo
    2023/03/21  16:32            1,921  demo.tar