目录

新增Rust语言编译环境

Summary
  • 介绍如何让DORY支持Rust编译构建环境

新增Rust语言编译环境

任务演示视频

  • 演示如何让DORY支持Rust编译构建环境

创建Rust语言编译环境容器镜像

  • 执行以下命令,创建Rust语言编译环境容器镜像
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 拉取rust编译环境官方镜像
docker pull rust:1.58.0

# 获取sccache,用于跨项目共享依赖
# 编译的共享依赖会缓存在~/.cache/sccache目录
wget https://github.com/mozilla/sccache/releases/download/v0.2.15/sccache-v0.2.15-x86_64-unknown-linux-musl.tar.gz
tar zxvf sccache-v0.2.15-x86_64-unknown-linux-musl.tar.gz
mv sccache-v0.2.15-x86_64-unknown-linux-musl/sccache .
rm -rf sccache-v0.2.15-x86_64-unknown-linux-musl*
chmod a+x sccache

# 创建rust编译环境镜像
# 把sccache复制到特定目录,并且添加uid为1000的用户
cat << EOF > Dockerfile
FROM rust:1.58.0
LABEL maintainer="cookeem"
LABEL email="cookeem@qq.com"
LABEL version="1.58.0"
ADD sccache /usr/local/cargo/bin/
RUN useradd --uid 1000 --user-group --create-home --home-dir /home/dory dory
EOF

# 构建rust编译环境镜像
docker build -t ${HARBOR_DOMAIN_NAME}/public/rust:1.58.0-dory .

# 把rust编译环境镜像推送到镜像仓库
docker push ${HARBOR_DOMAIN_NAME}/public/rust:1.58.0-dory
rm -rf Dockerfile

修改配置文件,让DORY支持Rust编译环境

  • 修改dory-core目录下的配置文件config/config.yaml,搜索dockerEnvs,在下边新增以下Rust编译环境配置。这里添加了两个编译环境rust-1.58和rust,都使用同一个镜像。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    - name: rust
      mountHomeDir: true
      enableProxy: false
      mountExtraCacheDirs: []
      commandsBeforeBuild: ["pwd", "ls -al"]
      commandsAfterCheck: []
      buildEnvs:
        - buildEnvName: rust-1.58
          image: "public/rust:1.58.0-dory"
        - buildEnvName: rust
          image: "public/rust:1.58.0-dory"
  • 完成配置后重启dory-coredory-dashboard服务
1
2
3
4
5
6
# 重启dory-core和dory-dashboard
kubectl -n dory delete pods dory-core-0 dory-dashboard-0
kubectl -n dory logs -f dory-core-0

# 等待服务恢复正常
kubectl -n dory get pods -w
  • 完成配置后,在dory-dashboard的项目定义模块构建定义就可以看到新增的Rust编译环境

/docs/tasks/customize-build-env/images/add-rust-in-build-env.png

在测试项目中新增Rust微服务代码以及依赖配置

  • 在代码仓库新增Rust的Cargo包管理工具依赖配置: Codes/Backend/tp1-rust-demo/.cargo/config
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = "tuna"

[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

[build]
rustc-wrapper = "/usr/local/cargo/bin/sccache"
  • 在代码仓库新增演示程序的依赖配置: Codes/Backend/tp1-rust-demo/Cargo.toml
1
2
3
4
5
6
7
8
[package]
name = "tp1-rust-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "3"
env_logger = "0.9.0"
  • 在代码仓库新增演示程序的源代码: Codes/Backend/tp1-rust-demo/src/main.rs
  • rust微服务返回Hello world!,并且以8080为服务端口
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use actix_web::middleware::Logger;
use env_logger::Env;

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(Env::default().default_filter_or("info"));

    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(echo)
            .wrap(Logger::default())
            .wrap(Logger::new("%a %{User-Agent}i"))
            .route("/hey", web::get().to(manual_hello))
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}
  • Rust编译出来的演示程序使用debian容器镜像作为运行环境,拉取镜像并推送到harbor镜像仓库
1
2
3
docker pull debian:stable-20211220
docker tag debian:stable-20211220 ${HARBOR_DOMAIN_NAME}/hub/debian:stable-20211220
docker push ${HARBOR_DOMAIN_NAME}/hub/debian:stable-20211220

配置构建、打包、发布

新增模块构建定义tp1-rust-demo

  • 在dory-dashboard的项目定义中新增tp1-rust-demo模块的模块构建定义,样例如下图:

/docs/tasks/customize-build-env/images/rust-build-def.png

  • 详细配置参见下边定义:
1
2
3
4
5
6
7
8
9
# buildDef
    buildName: tp1-rust-demo
    buildPhaseID: 1
    buildPath: Codes/Backend/tp1-rust-demo
    buildEnv: rust-1.58
    buildCmds:
        - 'cargo build --release -v'
    buildChecks:
        - 'ls -alh target/release'

新增模块镜像打包定义tp1-rust-demo

  • 在dory-dashboard的项目定义中新增tp1-rust-demo模块的模块镜像打包定义,样例如下图:

/docs/tasks/customize-build-env/images/rust-package-def.png

  • 详细配置参见下边定义:
1
2
3
4
5
6
7
8
# packageDef
    packageName: tp1-rust-demo
    relatedBuilds:
        - tp1-rust-demo
    packageFrom: '${HARBOR_DOMAIN_NAME}/hub/debian:stable-20211220'
    packages:
        - 'COPY Codes/Backend/tp1-rust-demo/target/release/tp1-rust-demo /tp1-rust-demo/'
        - 'WORKDIR /tp1-rust-demo'

新增模块容器发布定义tp1-rust-demo

  • 在dory-dashboard的项目定义中新增tp1-rust-demo模块的模块容器发布定义,样例如下图:

/docs/tasks/customize-build-env/images/rust-deploy-def.png

  • 详细配置参见下边定义:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# deployContainerDef
    deployName: tp1-rust-demo
    relatedPackage: tp1-rust-demo
    deployNodePorts:
        -
            port: 8080
            nodePort: 30105
            protocol: http
    deployReplicas: 1
    hpaConfig:
        maxReplicas: 2
        memoryAverageValue: 100Mi
        cpuAverageValue: 100m
    deployCommand: 'sh -c "./tp1-rust-demo 2>&1 | sed \"s/^/[$(hostname)] /\" | tee -a /tp1-rust-demo/logs/tp1-rust-demo.logs"'
    deployResources:
        memoryRequest: 10Mi
        memoryLimit: 100Mi
        cpuRequest: '0.02'
        cpuLimit: '0.1'
    deployVolumes:
        -
            pathInPod: /tp1-rust-demo/logs
            pathInPv: tp1-rust-demo/logs
    deployHealthCheck:
        httpGet:
            path: /
            port: 8080
        readinessDelaySeconds: 15
        readinessPeriodSeconds: 5
        livenessDelaySeconds: 150
        livenessPeriodSeconds: 30

执行Rust应用的发布

  • 把演示项目的代码push到代码仓库,自动触发流水线
  • 可以看到rust程序的构建、打包、发布过程