Tekton 如何接入物理机进行构建

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

 [[396376]]

本文转载自微信公众号「问其」,作者陈少文 。转载本文请联系问其公众号。

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. kubectl version 
  2.  
  3. 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"
  4. 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. uname -a 
  2.  
  3. 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. go version 
  2.  
  3. 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 
  • 查看资源

4.2 资源规划

  1. kubectl -n tekton-pipelines get all 
  2.  
  3. NAME                                               READY   STATUS    RESTARTS   AGE 
  4. pod/tekton-pipelines-controller-86c487c965-p6s5t   1/1     Running   0          51s 
  5. pod/tekton-pipelines-webhook-7b775d9cd8-fzdrq      1/1     Running   0          51s 
  6.  
  7. NAME                                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                              AGE 
  8. service/tekton-pipelines-controller   ClusterIP   10.233.61.46    <none>        9090/TCP,8080/TCP                    51s 
  9. service/tekton-pipelines-webhook      ClusterIP   10.233.46.233   <none>        9090/TCP,8008/TCP,443/TCP,8080/TCP   51s 
  10.  
  11. NAME                                          READY   UP-TO-DATE   AVAILABLE   AGE 
  12. deployment.apps/tekton-pipelines-controller   1/1     1            1           51s 
  13. deployment.apps/tekton-pipelines-webhook      1/1     1            1           51s 
  14.  
  15. NAME                                                     DESIRED   CURRENT   READY   AGE 
  16. replicaset.apps/tekton-pipelines-controller-86c487c965   1         1         1       51s 
  17. replicaset.apps/tekton-pipelines-webhook-7b775d9cd8      1         1         1       51s 
  18.  
  19. NAME                                                           REFERENCE                             TARGETS          MINPODS   MAXPODS   REPLICAS   AGE 
  20. horizontalpodautoscaler.autoscaling/tekton-pipelines-webhook   Deployment/tekton-pipelines-webhook   <unknown>/100%   1         5         1          51s 

需要的流水线资源清单:

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

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

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

  1. --- 
  2. apiVersion: tekton.dev/v1beta1 
  3. kind: Task 
  4. metadata: 
  5.   name: remote-shell 
  6.   labels: 
  7.     app.kubernetes.io/version: "0.1" 
  8.   annotations: 
  9.     tekton.dev/pipelines.minVersion: "0.12.1" 
  10.     tekton.dev/tags: git 
  11.     tekton.dev/displayName: "remote shell" 
  12. spec: 
  13.   description: >- 
  14.     This task can be used to run shell in remote machine 
  15.   workspaces: 
  16.   - name: source 
  17.   params: 
  18.   - name: remote-ip 
  19.     type: string 
  20.   - name: remote-port 
  21.     type: string 
  22.   - name: remote-username 
  23.     type: string 
  24.   - name: remote-password 
  25.     type: string 
  26.   - name: remote-workspace 
  27.     type: string 
  28.   - name: remote-script 
  29.     type: string 
  30.   steps: 
  31.   - name: remote-shell 
  32.     image: shaowenchen/rsync-sshpass:v1 
  33.     workingDir: $(workspaces.source.path) 
  34.     script: | 
  35.       sshpass  -p "$(params.remote-password)" ssh -o StrictHostKeyChecking=no "$(params.remote-username)"@"$(params.remote-ip)" -p "$(params.remote-port)" "mkdir -p $(params.remote-workspace)" 
  36.  
  37.       rsync -ratlz --progress --rsh="sshpass -p $(params.remote-password) ssh -o StrictHostKeyChecking=no -l $(params.remote-username)" ./ "$(params.remote-ip)":"$(params.remote-workspace)" 
  38.  
  39.       sshpass  -p "$(params.remote-password)" ssh -o StrictHostKeyChecking=no "$(params.remote-username)"@"$(params.remote-ip)" -p "$(params.remote-port)" "$(params.remote-script)" 
  40.  
  41.       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. ARG alpine_ver=3.13 
  2. FROM alpine:${alpine_ver}.5 
  3.  
  4. RUN apk update \ 
  5.  && apk upgrade \ 
  6.  && apk add --no-cache \ 
  7.             rsync \ 
  8.             openssh-client \ 
  9.             openssh \ 
  10.             sshpass \ 
  11.             ca-certificates \ 
  12.  && update-ca-certificates \ 
  13.  && rm -rf /var/cache/apk/* 
  • pipeline
  1. apiVersion: tekton.dev/v1beta1 
  2. kind: Pipeline 
  3. metadata: 
  4.   name: remote-build-pipeline 
  5. spec: 
  6.   params: 
  7.   - name: repo-url 
  8.     type: string 
  9.   - name: branch-name 
  10.     type: string 
  11.   - name: remote-ip 
  12.     type: string 
  13.   - name: remote-port 
  14.     type: string 
  15.   - name: remote-username 
  16.     type: string 
  17.   - name: remote-password 
  18.     type: string 
  19.   - name: remote-workspace 
  20.     type: string 
  21.   - name: remote-script 
  22.     type: string 
  23.   workspaces: 
  24.   - name: shared-data 
  25.   tasks: 
  26.   - namefetch-repo 
  27.     taskRef: 
  28.       name: git-clone 
  29.     workspaces: 
  30.     - nameoutput 
  31.       workspace: shared-data 
  32.     params: 
  33.     - name: url 
  34.       value: $(params.repo-url) 
  35.     - name: revision 
  36.       value: $(params.branch-name
  37.   - name: remote-build 
  38.     taskRef: 
  39.       name: remote-shell 
  40.     runAfter: ["fetch-repo"
  41.     workspaces: 
  42.     - name: source 
  43.       workspace: shared-data 
  44.     params: 
  45.     - name: remote-ip 
  46.       value: $(params.remote-ip) 
  47.     - name: remote-port 
  48.       value: $(params.remote-port) 
  49.     - name: remote-username 
  50.       value: $(params.remote-username) 
  51.     - name: remote-password 
  52.       value: $(params.remote-password
  53.     - name: remote-workspace 
  54.       value: $(params.remote-workspace) 
  55.     - name: remote-script 
  56.       value: $(params.remote-script) 

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

  • pipelinerun
  1. --- 
  2. apiVersion: tekton.dev/v1beta1 
  3. kind: PipelineRun 
  4. metadata: 
  5.   name: remote-build-pipelinerun-1 
  6. spec: 
  7.   pipelineRef: 
  8.     name: remote-build-pipeline 
  9.   workspaces: 
  10.   - name: shared-data 
  11.     volumeClaimTemplate: 
  12.       spec: 
  13.         accessModes: 
  14.         - ReadWriteOnce 
  15.         resources: 
  16.           requests: 
  17.             storage: 10Gi 
  18.   params: 
  19.   - name: repo-url 
  20.     value: https://github.com/shaowenchen/terraform-provider-qingcloud.git 
  21.   - name: branch-name 
  22.     value: master 
  23.   - name: subdirectory 
  24.     value: terraform-provider-qingcloud-001 
  25.   - name: remote-ip 
  26.     value: 0.0.0.0 
  27.   - name: remote-port 
  28.     value: "22" 
  29.   - name: remote-username 
  30.     value: root 
  31.   - name: remote-password 
  32.     value: YourPassword 
  33.   - name: remote-workspace 
  34.     value: ~/workspaces/terraform-provider-qingcloud-001 
  35.   - name: remote-script 
  36.     value: | 
  37.         cd ~/workspaces/terraform-provider-qingcloud-001 
  38.         make 

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

  • 查看 Tekton 资源定义

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

  1. kubectl get task 
  2.  
  3. NAME           AGE 
  4. git-clone      18m 
  5. remote-shell   5m47s 
  1. kubectl get pipelinerun 
  2.  
  3. NAME                         SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME 
  4. remote-build-pipelinerun-1   True        Succeeded   6m15s       5m42s 

执行成功,接着继续验证功能是否符合预期。

5. 功能验证

  • 查看相关负载
  1. kubectl get pod 
  2.  
  3. NAME                                                      READY   STATUS      RESTARTS   AGE 
  4. remote-build-pipelinerun-1-fetch-repo-56ws8-pod-mgx77     0/1     Completed   0          8m49s 
  5. remote-build-pipelinerun-1-remote-build-wxtms-pod-bcn6r   0/1     Completed   0          8m35s 
  • 在物理构建机上,查看构建目录
  1. pwd 
  2.  
  3. /root/workspaces/terraform-provider-qingcloud-001 
  4.  
  5. ls 
  6.  
  7. CHANGELOG.md  glide.yaml  go.sum   main.go   qingcloud  scripts    terraform-provider-qingcloud  website 
  8. dev.md        go.mod      LICENSE  Makefile  README.md  terraform  vendor 
  • 查看 Kubernetes PV 的构建目录
  1. kubectl get pv 
  2.  
  3. NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                              STORAGECLASS        
  4. pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8   10Gi       RWO            Delete           Bound    default/pvc-e7ceb0582a                                             openebs-hostpath            2m12s 
  • 查找 PV 存储路径
  1. kubectl describe pv pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8 |grep Path 
  2.  
  3.     Path:  /var/openebs/local/pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8 
  • 查看 PV 目录文件结构
  1. ls /var/openebs/local/pvc-860016bb-14b6-414a-9c5a-1a71d7290ba8 
  2.  
  3. CHANGELOG.md  glide.yaml  go.sum   main.go   qingcloud  scripts    terraform-provider-qingcloud  website 
  4. 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, 任意的引擎都可以使用这种方式。
  • 这里仅是作为方案验证,如果落地到产品,还需要考虑缓存、秘钥安全、数据安全、租户隔离等问题。

 

责任编辑:武晓燕 来源: 问其
相关推荐

2021-06-25 09:54:49

GitLab Tekton Devops

2009-10-13 15:00:36

物理机虚拟机网络安全

2022-04-08 09:53:56

TektonJenkinsKubesphere

2010-02-24 14:41:16

WCF物理地址

2022-04-14 07:51:39

TektonTaskRun

2014-12-18 09:41:44

虚拟化迁移

2009-10-26 15:20:23

宽带接入网

2022-04-25 08:07:45

TektonArgocdCI和CD

2009-12-29 16:39:17

多业务接入网

2010-08-27 09:52:43

宽带无线接入系统

2022-03-21 09:40:48

TektonJenkinsPipeline

2009-12-25 09:13:42

ADSL接入网

2009-12-28 10:23:42

FTTx接入网

2009-12-30 10:56:32

2010-10-13 10:21:37

物理机虚拟机迁移

2011-09-01 19:20:02

Ubuntu

2021-01-26 09:30:32

加密虚拟机攻击

2013-10-10 09:53:25

VMwareVMware Mira

2022-08-11 16:29:32

Tekton流水线迁移工作流

2010-04-07 16:33:58

宽带无线接入系统
点赞
收藏

51CTO技术栈公众号