偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Tekton 如何接入物理機(jī)進(jìn)行構(gòu)建

云計(jì)算 虛擬化
本文希望討論的問(wèn)題是在 Kubernetes 下,如何接入物理機(jī)進(jìn)行 CI 的構(gòu)建。本文以 Tekton 為例,其他引擎在處理邏輯上類似。

 [[396376]]

本文轉(zhuǎn)載自微信公眾號(hào)「問(wèn)其」,作者陳少文 。轉(zhuǎn)載本文請(qǐng)聯(lián)系問(wèn)其公眾號(hào)。

1. 為什么需要物理構(gòu)建機(jī)

在文章《如何接入遠(yuǎn)程 macOS 物理機(jī)進(jìn)行 Jenkins 流水線構(gòu)建》中,我描述了在 Jenkins 中添加物理構(gòu)建機(jī)的方法。這并不是我拍腦袋想的需求,而是當(dāng)時(shí)真的有 ToB 的商業(yè)客戶在咨詢方案。

對(duì)于多端開(kāi)發(fā)商來(lái)說(shuō),構(gòu)建 Android、IOS、macOS、Arm 、Windows、X86 應(yīng)用是常見(jiàn)的需求。

好的方面是 GitHub Actions 提供了 macOS 構(gòu)建環(huán)境、AWS 提供了 macOS 虛擬機(jī),而華為提供了 ARM 主機(jī)。在云原生背景下,更多使用的是 Kubernetes 運(yùn)行時(shí),在 Kubernetes 不支持的處理器架構(gòu)和操作系統(tǒng)面前,持續(xù)集成 (CI) 顯得很無(wú)力。持續(xù)集成需要支持物理構(gòu)建機(jī)。

本文希望討論的問(wèn)題是在 Kubernetes 下,如何接入物理機(jī)進(jìn)行 CI 的構(gòu)建。本文以 Tekton 為例,其他引擎在處理邏輯上類似。

2. Tekton 如何與物理機(jī)交互

Kuberntes 對(duì)物理機(jī)或者虛擬機(jī)的管理,實(shí)際上是一個(gè)典型的 Operator 場(chǎng)景。我們可以定義一個(gè) CRD 用來(lái)描述相關(guān)字段,通過(guò)寫 Controller 處理 Pod 與構(gòu)建機(jī)之間的邏輯。

也可以寫 Tekton 的 Task 封裝,本文將使用這種方式。由此也給我?guī)?lái)另一個(gè)疑問(wèn),Tekton 能否代替部分 Operator 的場(chǎng)景,在后續(xù)的文章中我會(huì)給出思考。

這里僅做原型驗(yàn)證,不會(huì)太關(guān)注產(chǎn)品化的細(xì)節(jié)。

在 Tekton 中,每個(gè)流水線由很多個(gè) Task 構(gòu)成,Task 可以并行。一個(gè) Task 包含很多個(gè)串行的 step 步驟,對(duì)應(yīng)著一個(gè) Pod 包含很多個(gè)容器。

這里的關(guān)鍵是要將 Pod 與構(gòu)建機(jī)關(guān)聯(lián)起來(lái)。我選擇的是使用 rsync 同步 Pod 與構(gòu)建機(jī)之間的文件,在 Pod 中使用 sshpass 執(zhí)行物理機(jī)的構(gòu)建命令。

主要分為如下步驟 (以下命令都是在容器中執(zhí)行):

  1. 克隆代碼
  2. 執(zhí)行 rsync 將代碼同步到構(gòu)建機(jī)
  3. 執(zhí)行 sshpass 在構(gòu)建機(jī)上執(zhí)行構(gòu)建命令
  4. 執(zhí)行 rsync 將構(gòu)建機(jī)中的構(gòu)建產(chǎn)物同步到容器
  5. 歸檔構(gòu)建產(chǎn)物(示例中, 這一步會(huì)被省略,僅驗(yàn)證能拿到構(gòu)建產(chǎn)物)

可以看到整個(gè)過(guò)程其實(shí)和 Tekton 沒(méi)有直接關(guān)系,對(duì)于任意容器與構(gòu)建機(jī)直連的環(huán)境都是可行的。下面以 Tekton 為例進(jìn)行演示。

3. 資源準(zhǔn)備清單

  • 一個(gè) Kubernetes 集群。用來(lái)運(yùn)行 Tekton,最新的 Tekton 0.23 要求 Kubernetes 不低于 1.17
  • 一臺(tái)物理機(jī)或虛擬機(jī)。用于構(gòu)建應(yīng)用

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 物理機(jī)準(zhǔn)備

  • 操作系統(tǒng)是 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 
  • 預(yù)裝 Golang 的編譯環(huán)境

原計(jì)劃是選擇一個(gè) macOS 的構(gòu)建示例,但是無(wú)法提供直通的網(wǎng)絡(luò)環(huán)境,因此換成 Golang 的構(gòu)建示例。

  1. go version 
  2.  
  3. go version go1.13 linux/amd64 

4. 準(zhǔn)備 Tekton 以及 Pipeline 資源

4.1 部署 Tekton Pipeline

  • 創(chuàng)建負(fù)載

Tekton 默認(rèn)使用的是 gcr.io 鏡像,如果是國(guó)內(nèi)環(huán)境可以替換為 gcr.azk8s.cn 鏡像。

  1. kubectl apply -f https://github.com/tektoncd/pipeline/releases/download/v0.23.0/release.notags.yaml 
  • 查看資源

4.2 資源規(guī)劃

  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 

需要的流水線資源清單:

  • 一個(gè) task, 用于克隆代碼
  • 一個(gè) pv, 用于共享 task 之間的文件
  • 一個(gè)自定義的 task, 用于將代碼同步到構(gòu)建機(jī),構(gòu)建完成之后,再同步回來(lái)
  • 一個(gè) pipeline, 用于描述流水線,編排 task
  • 一個(gè) pipelinerun, 用于實(shí)例化 pipeline, 提供構(gòu)建時(shí)必要的參數(shù)

4.2 編寫同步文件、執(zhí)行腳本的 Task

如上圖,這里的 Task 就是用于打通 container 和 vm 直接的文件和進(jìn)程,實(shí)現(xiàn)類似交叉編譯的效果。

  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 提供的示例。主要分為幾步:

  • 定義參數(shù)
  • 編寫 step 流程
  • 寫 script

這就是一個(gè)串腳本的過(guò)程,只不過(guò)借助容器鏡像,省去了安裝各種工具的步驟。

4.3 準(zhǔn)備 Tekton 的 pipeline 描述

  • 克隆代碼 Task

Tekton 已經(jīng)正式上線 Hub 服務(wù),用于共享 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 
  • 構(gòu)建一個(gè)工具箱鏡像 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 包含兩個(gè) task,一個(gè) task 克隆代碼,一個(gè) task 執(zhí)行遠(yuǎn)程構(gòu)建。

  • 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 目錄,同步到構(gòu)建機(jī)的 ~/workspaces/terraform-provider-qingcloud-001 目錄。也就是說(shuō),這兩個(gè)目錄最終的文件會(huì)保持一致,而構(gòu)建的二進(jìn)制是在構(gòu)建機(jī)上生成的。

  • 查看 Tekton 資源定義

以上資源全部 apply 之后,就可以查看相關(guān)的資源和流水線狀態(tài)了。

  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 

執(zhí)行成功,接著繼續(xù)驗(yàn)證功能是否符合預(yù)期。

5. 功能驗(yàn)證

  • 查看相關(guān)負(fù)載
  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 
  • 在物理構(gòu)建機(jī)上,查看構(gòu)建目錄
  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 的構(gòu)建目錄
  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 存儲(chǔ)路徑
  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 目錄文件結(jié)構(gòu)
  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 

在兩個(gè)目錄中,都存在構(gòu)建產(chǎn)物 terraform-provider-qingcloud,符合預(yù)期,也說(shuō)明我們達(dá)成了目標(biāo)。

6. 總結(jié)

傳統(tǒng)的 CICD 引擎通常是一個(gè) C/S 架構(gòu)。它需要一個(gè) S 端,用于解析流程,對(duì)流水線進(jìn)行調(diào)度; 需要很多個(gè) C 端,用于執(zhí)行高負(fù)載的構(gòu)建任務(wù)。這種方式的擴(kuò)展性并不是線性的,在云原生下、業(yè)務(wù)量大時(shí)很容易遇到瓶頸。因此,我們需要更加云原生的構(gòu)建引擎。在新的引擎下我們需要解決一些老的問(wèn)題,支持物理機(jī)構(gòu)建就是其中之一。

本文主要以 Tekton 為例,提供了一種利用 rsync 和 sshpass 接入物理機(jī)進(jìn)行構(gòu)建的思路。其中的關(guān)鍵點(diǎn)如下:

  • 使用 rsync\sshpass 的目的主要是將容器與物理機(jī)綁定,文件雙向同步,進(jìn)程空間互通。
  • 不限于 Tekton, 任意的引擎都可以使用這種方式。
  • 這里僅是作為方案驗(yàn)證,如果落地到產(chǎn)品,還需要考慮緩存、秘鑰安全、數(shù)據(jù)安全、租戶隔離等問(wèn)題。

 

責(zé)任編輯:武曉燕 來(lái)源: 問(wèn)其
相關(guān)推薦

2021-06-25 09:54:49

GitLab Tekton Devops

2022-04-08 09:53:56

TektonJenkinsKubesphere

2009-10-13 15:00:36

物理機(jī)虛擬機(jī)網(wǎng)絡(luò)安全

2022-04-14 07:51:39

TektonTaskRun

2010-02-24 14:41:16

WCF物理地址

2022-04-25 08:07:45

TektonArgocdCI和CD

2014-12-18 09:41:44

虛擬化遷移

2022-03-21 09:40:48

TektonJenkinsPipeline

2009-12-29 16:39:17

多業(yè)務(wù)接入網(wǎng)

2009-10-26 15:20:23

寬帶接入網(wǎng)

2010-08-27 09:52:43

寬帶無(wú)線接入系統(tǒng)

2022-08-11 16:29:32

Tekton流水線遷移工作流

2009-12-28 10:23:42

FTTx接入網(wǎng)

2009-12-25 09:13:42

ADSL接入網(wǎng)

2009-12-30 10:56:32

2010-10-13 10:21:37

物理機(jī)虛擬機(jī)遷移

2013-10-10 09:53:25

VMwareVMware Mira

2021-01-26 09:30:32

加密虛擬機(jī)攻擊

2011-09-01 19:20:02

Ubuntu

2010-04-07 16:33:58

寬帶無(wú)線接入系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)