惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
U
Unit 42
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
G
Google Developers Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Jina AI
Jina AI
量子位
宝玉的分享
宝玉的分享
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
美团技术团队
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
博客园 - 司徒正美
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Forbes - Security
Forbes - Security
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Privacy International News Feed
G
GRAHAM CLULEY
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
V2EX - 技术
V2EX - 技术

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
Tekton 如何接入物理机进行构建
微信公众号 · 2021-04-28 · via 陈少文的网站

1. 为什么需要物理构建机

在文章《如何接入远程 macOS 物理机进行 Jenkins 流水线构建》中,我描述了在 Jenkins 中添加物理构建机的方法。这并不是我拍脑袋想的需求,而是当时真的有 ToB 的商业客户在咨询方案。

对于多端开发商来说,构建 Android、IOS、macOS、Arm 、Windows、X86 应用是常见的需求。

好的方面是 GitHub Actions 提供了 macOS 构建环境、AWS 提供了 macOS 虚拟机,而华为提供了 ARM 主机。在云原生背景下,更多使用的是 Kubernetes 运行时,在 Kubernetes 不支持的处理器架构和操作系统面前,持续集成 (CI) 显得很无力。持续集成需要支持物理构建机运行时。

本文希望讨论的问题是在 Kubernetes 下,如何接入物理机进行 CI 的构建。本文以 Tekton 为例,其他引擎在处理逻辑上类似。

2. Tekton 如何与物理机交互

Kuberntes 对物理机或者虚拟机的管理,实际上是一个典型的 Operator 场景。我们可以定义一个 CRD 用来描述相关字段,通过写 Controller 处理 Pod 与构建机之间的逻辑。

也可以写 Tekton 的 Task 封装,本文将使用这种方式。由此也给我带来另一个疑问,Tekton 能否代替部分 Operator 的场景,在后续的文章中我会给出思考。

这里仅做原型验证,不会太关注产品化的细节。

在 Tekton 中,每个流水线由很多个 Task 构成,Task 可以并行。一个 Task 包含很多个串行的 step 步骤,对应着一个 Pod 包含很多个容器。

这里的关键是要将 Pod 与构建机关联起来。我选择的是使用 rsync 同步 Pod 与构建机之间的文件,在 Pod 中使用 sshpass 执行物理机的构建命令。

主要分为如下步骤 (以下命令都是在容器中执行):

  1. 克隆代码
  2. 执行 rsync 将代码同步到构建机
  3. 执行 sshpass 在构建机上执行构建命令
  4. 执行 rsync 将构建机中的构建产物同步到容器
  5. 归档构建产物(示例中, 这一步会被省略,仅验证能拿到构建产物)

可以看到整个过程其实和 Tekton 没有直接关系,对于任意容器与构建机直连的环境都是可行的。下面以 Tekton 为例进行演示。

3. 资源准备清单

  • 一个 Kubernetes 集群。用来运行 Tekton,最新的 Tekton 0.23 要求 Kubernetes 不低于 1.17
  • 一台物理机或虚拟机。用于构建应用

3.1 查看 Kubernetes 版本

1
2
3
4
kubectl version

Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.7", GitCommit:"1dd5338295409edcfff11505e7bb246f0d325d15", GitTreeState:"clean", BuildDate:"2021-01-13T13:23:52Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-21T01:11:42Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

3.2 物理机准备

  • 操作系统是 CentOS 7.6
1
2
3
uname -a

Linux test 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • 预装 Golang 的编译环境

原计划是选择一个 macOS 的构建示例,但是无法提供直通的网络环境,因此换成 Golang 的构建示例。

1
2
3
go version

go version go1.13 linux/amd64

4. 准备 Tekton 以及 Pipeline 资源

4.1 部署 Tekton Pipeline

  • 创建负载

Tekton 默认使用的是 gcr.io 镜像,如果是国内环境可以替换为 gcr.azk8s.cn 镜像。

1
kubectl apply -f https://github.com/tektoncd/pipeline/releases/download/v0.23.0/release.notags.yaml
  • 查看资源
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
kubectl -n tekton-pipelines get all

NAME                                               READY   STATUS    RESTARTS   AGE
pod/tekton-pipelines-controller-86c487c965-p6s5t   1/1     Running   0          51s
pod/tekton-pipelines-webhook-7b775d9cd8-fzdrq      1/1     Running   0          51s

NAME                                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                              AGE
service/tekton-pipelines-controller   ClusterIP   10.233.61.46    <none>        9090/TCP,8080/TCP                    51s
service/tekton-pipelines-webhook      ClusterIP   10.233.46.233   <none>        9090/TCP,8008/TCP,443/TCP,8080/TCP   51s

NAME                                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tekton-pipelines-controller   1/1     1            1           51s
deployment.apps/tekton-pipelines-webhook      1/1     1            1           51s

NAME                                                     DESIRED   CURRENT   READY   AGE
replicaset.apps/tekton-pipelines-controller-86c487c965   1         1         1       51s
replicaset.apps/tekton-pipelines-webhook-7b775d9cd8      1         1         1       51s

NAME                                                           REFERENCE                             TARGETS          MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/tekton-pipelines-webhook   Deployment/tekton-pipelines-webhook   <unknown>/100%   1         5         1          51s

4.2 资源规划

需要的流水线资源清单:

  • 一个 task, 用于克隆代码
  • 一个 pv, 用于共享 task 之间的文件
  • 一个自定义的 task, 用于将代码同步到构建机,构建完成之后,再同步回来
  • 一个 pipeline, 用于描述流水线,编排 task
  • 一个 pipelinerun, 用于实例化 pipeline, 提供构建时必要的参数

4.2 编写同步文件、执行脚本的 Task

如上图,这里的 Task 就是用于打通 container 和 vm 之间的文件和进程,实现类似交叉编译的效果。

 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
35
36
37
38
39
40
41
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: remote-shell
  labels:
    app.kubernetes.io/version: "0.1"
  annotations:
    tekton.dev/pipelines.minVersion: "0.12.1"
    tekton.dev/tags: git
    tekton.dev/displayName: "remote shell"
spec:
  description: >-
        This task can be used to run shell in remote machine
  workspaces:
    - name: source
  params:
    - name: remote-ip
      type: string
    - name: remote-port
      type: string
    - name: remote-username
      type: string
    - name: remote-password
      type: string
    - name: remote-workspace
      type: string
    - name: remote-script
      type: string
  steps:
    - name: remote-shell
      image: shaowenchen/rsync-sshpass:v1
      workingDir: $(workspaces.source.path)
      script: |
        sshpass  -p "$(params.remote-password)" ssh -o StrictHostKeyChecking=no "$(params.remote-username)"@"$(params.remote-ip)" -p "$(params.remote-port)" "mkdir -p $(params.remote-workspace)"

        rsync -ratlz --progress --rsh="sshpass -p $(params.remote-password) ssh -o StrictHostKeyChecking=no -l $(params.remote-username)" ./ "$(params.remote-ip)":"$(params.remote-workspace)"

        sshpass  -p "$(params.remote-password)" ssh -o StrictHostKeyChecking=no "$(params.remote-username)"@"$(params.remote-ip)" -p "$(params.remote-port)" "$(params.remote-script)"

        rsync -ratlz --progress --rsh="sshpass -p $(params.remote-password) ssh -o StrictHostKeyChecking=no -l $(params.remote-username)" "$(params.remote-ip)":"$(params.remote-workspace)"/ .        

在写法上,可以参考 Tekton 提供的示例。主要分为几步:

  • 定义参数
  • 编写 step 流程
  • 写 script

这就是一个串脚本的过程,只不过借助容器镜像,省去了安装各种工具的步骤。

4.3 准备 Tekton 的 pipeline 描述

  • 克隆代码 Task

Tekton 已经正式上线 Hub 服务,用于共享 Task,这里直接使用 https://hub.tekton.dev/tekton/task/git-clone

1
kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-clone/0.3/git-clone.yaml
  • 构建一个工具箱镜像 shaowenchen/rsync-sshpass:v1

Dockerfile 为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
ARG alpine_ver=3.13
FROM alpine:${alpine_ver}.5

RUN apk update \
 && apk upgrade \
 && apk add --no-cache \
            rsync \
            openssh-client \
            openssh \
            sshpass \
            ca-certificates \
 && update-ca-certificates \
 && rm -rf /var/cache/apk/*
  • pipeline
 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: remote-build-pipeline
spec:
  params:
    - name: repo-url
      type: string
    - name: branch-name
      type: string
    - name: remote-ip
      type: string
    - name: remote-port
      type: string
    - name: remote-username
      type: string
    - name: remote-password
      type: string
    - name: remote-workspace
      type: string
    - name: remote-script
      type: string
  workspaces:
    - name: shared-data
  tasks:
    - name: fetch-repo
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: shared-data
      params:
        - name: url
          value: $(params.repo-url)
        - name: revision
          value: $(params.branch-name)
    - name: remote-build
      taskRef:
        name: remote-shell
      runAfter: ["fetch-repo"]
      workspaces:
        - name: source
          workspace: shared-data
      params:
        - name: remote-ip
          value: $(params.remote-ip)
        - name: remote-port
          value: $(params.remote-port)
        - name: remote-username
          value: $(params.remote-username)
        - name: remote-password
          value: $(params.remote-password)
        - name: remote-workspace
          value: $(params.remote-workspace)
        - name: remote-script
          value: $(params.remote-script)

pipeline 包含两个 task,一个 task 克隆代码,一个 task 执行远程构建。

  • pipelinerun
 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
35
36
37
38
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: remote-build-pipelinerun-1
spec:
  pipelineRef:
    name: remote-build-pipeline
  workspaces:
    - name: shared-data
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 10Gi
  params:
    - name: repo-url
      value: https://github.com/shaowenchen/terraform-provider-qingcloud.git
    - name: branch-name
      value: master
    - name: subdirectory
      value: terraform-provider-qingcloud-001
    - name: remote-ip
      value: 0.0.0.0
    - name: remote-port
      value: "22"
    - name: remote-username
      value: root
    - name: remote-password
      value: YourPassword
    - name: remote-workspace
      value: ~/workspaces/terraform-provider-qingcloud-001
    - name: remote-script
      value: |
        cd ~/workspaces/terraform-provider-qingcloud-001
        make        

这里将克隆代码到 pv 的 terraform-provider-qingcloud-001 目录,同步到构建机的 ~/workspaces/terraform-provider-qingcloud-001 目录。也就是说,这两个目录最终的文件会保持一致,而构建的二进制是在构建机上生成的。

  • 查看 Tekton 资源定义

以上资源全部 apply 之后,就可以查看相关的资源和流水线状态了。

1
2
3
4
5
kubectl get tasks.tekton.dev

NAME           AGE
git-clone      18m
remote-shell   5m47s
1
2
3
4
kubectl get pipelines.tekton.dev

NAME                    AGE
remote-build-pipeline   4m21s
1
2
3
4
kubectl get pipelineruns.tekton.dev

NAME                         SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
remote-build-pipelinerun-1   True        Succeeded   6m15s       5m42s

在 pipelineruns 中可以通过 describe 拿到整个流水线执行的记录,用于展示执行步骤,查询构建日志。下面是截取的部分内容:

 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
Task Runs:
    remote-build-pipelinerun-1-fetch-repo-56ws8:
      Pipeline Task Name:  fetch-repo
      Status:
        Completion Time:  2021-04-27T13:22:08Z
        Conditions:
          Last Transition Time:  2021-04-27T13:22:08Z
          Message:               All Steps have completed executing
          Reason:                Succeeded
          Status:                True
          Type:                  Succeeded
        Pod Name:                remote-build-pipelinerun-1-fetch-repo-56ws8-pod-mgx77
        Start Time:              2021-04-27T13:21:54Z
        Steps:
          Container:  step-clone
          Image ID:   docker-pullable://gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init@sha256:db18a9c1607c8cbbcd72f61d0c4d795b9ff528669deacd5f8a1672e4ef198ffd
          Name:       clone
          Terminated:
            Container ID:  docker://e5258bc7b0770e0333a93395eda13514abbd293652c0c0494352407a3fbc1a7f
            Exit Code:     0
            Finished At:   2021-04-27T13:22:07Z
            Message:       [{"key":"commit","value":"d024b4deeb2f328098fed88eb702cb19dac8452f","type":"TaskRunResult"},{"key":"url","value":"https://github.com/shaowenchen/terraform-provider-qingcloud.git","type":"TaskRunResult"}]
            Reason:        Completed
            Started At:    2021-04-27T13:22:04Z
        Task Results:
          Name:   commit
          Value:  d024b4deeb2f328098fed88eb702cb19dac8452f
          Name:   url
          Value:  https://github.com/shaowenchen/terraform-provider-qingcloud.git

5. 功能验证

  • 查看相关负载
1
2
3
4
5
kubectl get pod

NAME                                                      READY   STATUS      RESTARTS   AGE
remote-build-pipelinerun-1-fetch-repo-56ws8-pod-mgx77     0/1     Completed   0          8m49s
remote-build-pipelinerun-1-remote-build-wxtms-pod-bcn6r   0/1     Completed   0          8m35s
  • 在物理构建机上,查看构建目录
1
2
3
4
5
6
7
8
pwd

/root/workspaces/terraform-provider-qingcloud-001

ls

CHANGELOG.md  glide.yaml  go.sum   main.go   qingcloud  scripts    terraform-provider-qingcloud  website
dev.md        go.mod      LICENSE  Makefile  README.md  terraform  vendor
  • 查看 Kubernetes PV 的构建目录
1
2
3
4
kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                              STORAGECLASS
pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8   10Gi       RWO            Delete           Bound    default/pvc-e7ceb0582a                                             openebs-hostpath            2m12s

查找 PV 存储路径

1
2
3
kubectl describe pv pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8 |grep Path

    Path:  /var/openebs/local/pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8

查看 PV 目录文件结构

1
2
3
4
ls /var/openebs/local/pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8

CHANGELOG.md  glide.yaml  go.sum   main.go   qingcloud  scripts    terraform-provider-qingcloud  website
dev.md        go.mod      LICENSE  Makefile  README.md  terraform  vendor

在两个目录中,都存在构建产物 terraform-provider-qingcloud,符合预期,也说明我们达成了目标。

6. 总结

传统的 CICD 引擎通常是一个 C/S 架构。它需要一个 S 端,用于解析流程,对流水线进行调度; 需要很多个 C 端,用于执行高负载的构建任务。这种方式的扩展性并不是线性的,在云原生下、业务量大时很容易遇到瓶颈。因此,我们需要更加云原生的构建引擎。在新的引擎下我们需要解决一些老的问题,支持物理机构建就是其中之一。

本文主要以 Tekton 为例,提供了一种利用 rsync 和 sshpass 接入物理机进行构建的思路。其中的关键点如下:

  • 使用 rsync\sshpass 的目的主要是将容器与物理机绑定,文件双向同步,进程空间互通。
  • 不限于 Tekton, 任意的引擎都可以使用这种方式。
  • 这里仅是作为方案验证,如果落地到产品,还需要考虑缓存、秘钥安全、数据安全、租户隔离等问题。

7. 参考