国产乱子轮XXX农村-精品久久国产字幕高潮-精品国产乱码久久久久久1区2区-国产AV一区二区三区传媒

云原生CI/CD框架Tekton初體驗

發(fā)布時間:2025-04-06 點擊:27
tekton 是一款功能非常強大而靈活的 ci/cd 開源的云原生框架。tekton 的前身是 knative 項目的 build-pipeline 項目,這個項目是為了給 build 模塊增加 pipeline 的功能,但是隨著不同的功能加入到 knative build 模塊中,build 模塊越來越變得像一個通用的 ci/cd 系統(tǒng),于是,索性將 build-pipeline 剝離出 knative,就變成了現(xiàn)在的 tekton,而 tekton 也從此致力于提供全功能、標(biāo)準(zhǔn)化的云原生 ci/cd 解決方案。
tekton 為 ci/cd 系統(tǒng)提供了諸多好處:
可定制:tekton 是完全可定制的,具有高度的靈活性,我們可以定義非常詳細的構(gòu)建塊目錄,供開發(fā)人員在各種場景中使用。 可重復(fù)使用:tekton 是完全可移植的,任何人都可以使用給定的流水線并重用其構(gòu)建塊,可以使得開發(fā)人員無需"造輪子"就可以快速構(gòu)建復(fù)雜的流水線。 可擴展:tekton catalog 是社區(qū)驅(qū)動的 tekton 構(gòu)建塊存儲庫,我們可以使用 tekton catalog 中定義的組件快速創(chuàng)建新的流水線并擴展現(xiàn)有管道。 標(biāo)準(zhǔn)化:tekton 在你的 kubernetes 集群上作為擴展安裝和運行,并使用完善的 kubernetes 資源模型,tekton 工作負載在 kubernetes pod 內(nèi)執(zhí)行。 伸縮性:要增加工作負載容量,只需添加新的節(jié)點到集群即可,tekton 可隨集群擴展,無需重新定義資源分配或?qū)艿肋M行任何其他修改。 組件
tekton 由一些列組件組成:
tekton pipelines 是 tekton 的基礎(chǔ),它定義了一組 kubernetes crd 作為構(gòu)建塊,我們可以使用這些對象來組裝 ci/cd 流水線。 tekton triggers 允許我們根據(jù)事件來實例化流水線,例如,可以我們在每次將 pr 合并到 github 倉庫的時候觸發(fā)流水線實例和構(gòu)建工作。 tekton cli 提供了一個名為 tkn 的命令行界面,它構(gòu)建在 kubernetes cli 之上,運行和 tekton 進行交互。 tekton dashboard 是 tekton pipelines 的基于 web 的一個圖形界面,可以線上有關(guān)流水線執(zhí)行的相關(guān)信息。 tekton catalog 是一個由社區(qū)貢獻的高質(zhì)量 tekton 構(gòu)建塊(任務(wù)、流水線等)存儲庫,可以直接在我們自己的流水線中使用這些構(gòu)建塊。 tekton hub 是一個用于訪問 tekton catalog 的 web 圖形界面工具。 tekton operator 是一個 kubernetes operator,可以讓我們在 kubernetes 集群上安裝、更新、刪除 tekton 項目。 安裝
安裝 tekton 非常簡單,可以直接通過 tektoncd/pipeline 的 github 倉庫中的 release.yaml 文件進行安裝,如下所示的命令:
kubectlapply--filenamehttps://storage.googleapis.com/tekton-releases/pipeline/previous/v0.24.1/release.yaml由于官方使用的鏡像是 gcr 的鏡像,所以正常情況下我們是獲取不到的,如果你的集群由于某些原因獲取不到鏡像,可以使用下面的資源清單文件,我已經(jīng)將鏡像替換成了 docker hub 上面的鏡像:
kubectlapply-fhttp://my-oss-testing.oss-cn-beijing.aliyuncs.com/k8s/tekton/release.yaml上面的資源清單文件安裝后,會創(chuàng)建一個名為 tekton-pipelines 的命名空間,在該命名空間下面會有大量和 tekton 相關(guān)的資源對象,我們可以通過在該命名空間中查看 pod 并確保它們處于 running 狀態(tài)來檢查安裝是否成功:
$kubectlgetpods-ntekton-pipelines namereadystatusrestartsage tekton-pipelines-controller-795dd94d96-lkbxt1/1running092s tekton-pipelines-webhook-6b8964445d-mp4k61/1running092stekton 安裝完成后,我們還可以選擇是否安裝 cli 工具,有時候可能 tekton 提供的命令行工具比 kubectl 管理這些資源更加方便,當(dāng)然這并不是強制的,我這里是 mac 系統(tǒng),所以可以使用常用的 homebrew 工具來安裝:
brewtaptektoncd/tools brewinstalltektoncd/tools/tektoncd-cli安裝完成后可以通過如下命令驗證 cli 是否安裝成功:
$tknversion clientversion:0.15.0 pipelineversion:v0.24.1 dashboardversion:v0.17.0還可以從 tknreleases 頁面下載安裝包,下載文件后,將其解壓縮到您的 path:
#replaceyour-downloaded-filewiththefilepathofyourown. sudotarxvzfyour-downloaded-file-c/usr/local/bin/tkn此外,還可以安裝一個 tekton 提供的一個 dashboard,我們可以通過 dashboard 查看 tekton 整個任務(wù)的構(gòu)建過程,直接執(zhí)行下面的命令直接安裝即可:
kubectlapply-fhttp://my-oss-testing.oss-cn-beijing.aliyuncs.com/k8s/tekton/dashboard.yaml安裝完成后我們可以通過 dashboard 的 service 的 nodeport 來訪問應(yīng)用。
概念
tekton 為 kubernetes 提供了多種 crd 資源對象,可用于定義我們的流水線。
主要有以下幾個資源對象:
task:表示執(zhí)行命令的一系列有序的步驟,task 里可以定義一系列的 steps,例如編譯代碼、構(gòu)建鏡像、推送鏡像等,每個 step 實際由一個 pod 執(zhí)行。 pipeline:一組有序的 task,pipeline 中的 task 可以使用之前執(zhí)行過的 task 的輸出作為它的輸入。表示一個或多個 task、pipelineresource 以及各種定義參數(shù)的集合。 taskrun:task 只是定義了一個模版,taskrun 才真正代表了一次實際的運行,當(dāng)然你也可以自己手動創(chuàng)建一個 taskrun,taskrun 創(chuàng)建出來之后,就會自動觸發(fā) task 描述的構(gòu)建任務(wù)。 pipelinerun:類似 task 和 taskrun 的關(guān)系,pipelinerun 也表示某一次實際運行的 pipeline,下發(fā)一個 pipelinerun crd 實例到 kubernetes 后,同樣也會觸發(fā)一次 pipeline 的構(gòu)建。 clustertask:覆蓋整個集群的任務(wù),而不是單一的某一個命名空間,這是和 task 大的區(qū)別,其他基本上一致的。 pipelineresource:表示 pipeline 輸入資源,比如 github 上的源碼,或者 pipeline 輸出資源,例如一個容器鏡像或者構(gòu)建生成的 jar 包等。每個任務(wù)都在自己的 kubernetes pod 中執(zhí)行,因此,默認情況下,管道內(nèi)的任務(wù)不共享數(shù)據(jù)。要在 tasks 之間共享數(shù)據(jù),你必須明確配置每個 task 以使其輸出可用于下一個 task 并獲取先前執(zhí)行的 task 的輸出作為其輸入。
示例
使用 tekton 后你的 ci/cd 工作流中的每個操作都變成了一個 step,使用指定的容器鏡像來執(zhí)行。steps 然后組織在 tasks 中,它在集群中作為 kubernetes pod 運行,還可以進一步組織 tasks 變成成 pipelines,還可以控制幾個 tasks 的執(zhí)行順序。
在這里我們使用一個簡單的 golang 應(yīng)用,可以在倉庫 https://github.com/cnych/tekton-demo 下面獲取應(yīng)用程序代碼,測試以及 dockerfile 文件。
首先第一個任務(wù)就是 clone 應(yīng)用程序代碼進行測試,要創(chuàng)建一個 task 任務(wù),就需要使用到 kubernetes 中定義的 task 這個 crd 對象,這里我們創(chuàng)建一個如下所示的資源文件,內(nèi)容如下所示:
#task-test.yaml apiversion:tekton.dev/v1beta1 kind:task metadata: name:test spec: resources: inputs: -name:repo type:git steps: -name:run-test image:golang:1.14-alpine workingdir:/workspace/repo command:['go'] args:['test']其中 resources 定義了我們的任務(wù)中定義的 step 所需的輸入內(nèi)容,這里我們的步驟需要 clone 一個 git 倉庫作為 go test 命令的輸入,目前支持 git、pullrequest、image、cluster、storage、cloudevent 等資源。
tekton 內(nèi)置的 git 資源類型,它會自動將代碼倉庫 clone 到 /workspace/$input_name 目錄中,由于我們這里輸入被命名成 repo,所以代碼會被 clone 到 /workspace/repo 目錄下面。
然后下面的 steps 就是來定義執(zhí)行運行測試命令的步驟,這里我們直接在代碼的根目錄中運行 go test 命令即可,需要注意的是命令和參數(shù)需要分別定義。
定義完成后直接使用 kubectl 創(chuàng)建該任務(wù):
$kubectlapply-ftask-test.yaml task.tekton.dev/testcreated現(xiàn)在我們定義完成了一個新建的 task 任務(wù),但是該任務(wù)并不會立即執(zhí)行,我們必須創(chuàng)建一個 taskrun 引用它并提供所有必需輸入的數(shù)據(jù)才行。當(dāng)然我們也可以直接使用 tkn 命令來啟動這個 task 任務(wù),我們可以通過如下所示的命令來獲取啟動 task 所需的資源對象:
$tkntaskstarttest--dry-run nopipelineresourceoftype"git"foundinnamespace:default pleasecreateanew"git"resourceforpipelineresource"repo" ?enteranameforapipelineresource:demo-git ?enteravalueforurl:https://github.com/cnych/tekton-demo ?enteravalueforrevision:master newgitresource"demo-git"hasbeencreated apiversion:tekton.dev/v1beta1 kind:taskrun metadata: creationtimestamp:null generatename:test-run- namespace:default spec: resources: inputs: -name:repo resourceref: name:demo-git serviceaccountname:"" taskref: name:test status: podname:""由于我們這里的 task 任務(wù)需要一個 git 代碼倉庫作為輸入,所以需要一個 pipelineresource 對象來定義輸入信息,上面的命令會自動創(chuàng)建一個名為 demo-git 的 pipelineresource 資源對象,如下所示:
$kubectlgetpipelineresource nameage demo-git3m37s $kubectlgetpipelineresourcedemo-git-oyaml apiversion:tekton.dev/v1alpha1 kind:pipelineresource metadata: name:demo-git namespace:default ...... spec: params: -name:url value:https://github.com/cnych/tekton-demo -name:revision value:master type:git當(dāng)我們不知道如何創(chuàng)建 pipelineresource 的時候我們就可以參考上面的方式來創(chuàng)建,當(dāng)然最后還需要創(chuàng)建 taskrun 對象才可以真正執(zhí)行這個 task 任務(wù),上面的 tkn task start 命令也為我們打印出對應(yīng)的 taskrun 資源,將其內(nèi)容添加到 taskrun.yaml 文件中:
#taskrun.yaml apiversion:tekton.dev/v1beta1 kind:taskrun metadata: name:testrun spec: resources: inputs: -name:repo resourceref: name:demo-git taskref: name:test這里的 taskref 引用上面定義的 task 和 git 倉庫作為輸入,resourceref 也是引用上面定義的 pipelineresource 資源對象?,F(xiàn)在我們創(chuàng)建這個資源對象過后,就會開始運行了:
$kubectlapply-ftaskrun.yaml taskrun.tekton.dev/testruncreatedtekton 現(xiàn)在將開始運行您的 task, 要查看最后一個 taskrun 的日志,可以使用以下 tkn命令:
tkntaskrunlogs--last-f此外我們還可以通過查看 taskrun 資源對象的狀態(tài)來查看構(gòu)建狀態(tài):
$kubectlgettaskrun namesucceededreasonstarttimecompletiontime testrununknownpending21s $kubectlgetpods namereadystatusrestartsage testrun-pod-l629c0/2init:1/2059s $kubectldescribepodtestrun-pod-l629c name:testrun-pod-l629c namespace:default ...... events: typereasonagefrommessage ------------------------- normalscheduled2m53sdefault-schedulersuccessfullyassigneddefault/testrun-pod-l629ctonode1 normalpulling2m52skubelet,node1pullingimage"cnych/tekton-distroless-base:v0.24.1" normalpulled2m27skubelet,node1successfullypulledimage"cnych/tekton-distroless-base:v0.24.1"in24.910571044s normalcreated2m27skubelet,node1createdcontainerworking-dir-initializer normalstarted2m27skubelet,node1startedcontainerworking-dir-initializer normalpulling2m27skubelet,node1pullingimage"cnych/tekton-entrypoint:v0.24.1" normalpulled2mkubelet,node1successfullypulledimage"cnych/tekton-entrypoint:v0.24.1"in27.120230223s normalcreated2mkubelet,node1createdcontainerplace-tools normalstarted2mkubelet,node1startedcontainerplace-tools normalpulling119skubelet,node1pullingimage"cnych/tekton-git-init:v0.24.1" normalpulled82skubelet,node1successfullypulledimage"cnych/tekton-git-init:v0.24.1"in36.318235738s normalcreated82skubelet,node1createdcontainerstep-git-source-repo-jg7vz normalstarted82skubelet,node1startedcontainerstep-git-source-repo-jg7vz normalpulling82skubelet,node1pullingimage"golang:1.14-alpine" normalpulled28skubelet,node1successfullypulledimage"golang:1.14-alpine"in54.587298174s normalcreated27skubelet,node1createdcontainerstep-run-test normalstarted27skubelet,node1startedcontainerstep-run-test我們可以通過 kubectl describe 命令來查看任務(wù)運行的過程,首先會通過 tekton-git-init 拉取代碼,然后會使用我們定義的 task 任務(wù)中的 steps 鏡像來執(zhí)行任務(wù)。當(dāng)任務(wù)執(zhí)行完成后, pod 就會變成 completed 狀態(tài)了:
$kubectlgetpods namereadystatusrestartsage testrun-r-n97ls-pod-7jvrd0/2completed04m27s $kubectlgettaskrun namesucceededreasonstarttimecompletiontime testrun-r-n97lstruesucceeded16m119s我們可以查看容器的日志信息來了解任務(wù)的執(zhí)行結(jié)果信息:
$kubectllogstestrun-r-n97ls-pod-7jvrd--all-containers 2021/06/0809:07:31copied/ko-app/entrypointto/tekton/tools/entrypoint {"level":"info","ts":1623144122.7787642,"caller":"git/git.go:169","msg":"successfullyclonedhttps://github.com/cnych/tekton-demo@c6c2a85091d538a13c44f85bcee9e861c362b0d3(grafted,head,origin/master)inpath/workspace/repo"} {"level":"info","ts":1623144122.796532,"caller":"git/git.go:207","msg":"successfullyinitializedandupdatedsubmodulesinpath/workspace/repo"} pass ok_/workspace/repo0.002s我們可以看到我們的測試已經(jīng)通過了。
docker hub 配置
為了能夠構(gòu)建 docker 鏡像,一般來說我們需要使用 docker 來進行,我們這里是容器,所以可以使用 docker in docker 模式,這種模式安全性不高,除了這種方式之外,我們還可以使用 google 推出的 kaniko 工具來進行構(gòu)建,該工具可以在 kubernetes 集群中構(gòu)建 docker 鏡像而無需依賴 docker 守護進程,之前我們已經(jīng)介紹過 kaniko 這種形式,這里我們就介紹 dind 這種模式。
使用 kaniko 構(gòu)建鏡像和 docker 命令基本上一致,所以我們可以提前設(shè)置下 docker hub 的登錄憑證,方便后續(xù)將鏡像推送到鏡像倉庫。登錄憑證可以保存到 kubernetes 的 secret 資源對象中,創(chuàng)建一個名為 harbor-auth.yaml 的文件,內(nèi)容如下所示:
#harbor-auth.yaml apiversion:v1 kind:secret metadata: name:harbor-auth annotations: tekton.dev/docker-0:http://harbor.k8s.local type:kubernetes.io/basic-auth stringdata: username:admin password:harbor12345記得將 username 和 password 替換成你的 harbor 倉庫登錄憑證。
我們這里在 secret 對象中添加了一個 tekton.dev/docker-0 的 annotation,該注解信息是用來告訴 tekton 這些認證信息所屬的 docker 鏡像倉庫。
然后創(chuàng)建一個 serviceaccount 對象來使用上面的 docker-auth 這個 secret 對象,創(chuàng)建一個名為 sa.yaml 的文件,內(nèi)容如下所示:
#sa.yaml apiversion:v1 kind:serviceaccount metadata: name:build-sa secrets: -name:harbor-auth然后直接創(chuàng)建上面兩個資源對象即可:
$kubectlapply-fharbor-auth.yaml secret/harbor-authcreated $kubectlapply-fsa.yaml serviceaccount/build-sacreated創(chuàng)建完成后,我們就可以在運行 tekton 的任務(wù)或者流水線的時候使用上面的 build-sa 這個 serviceaccount 對象來進行 docker hub 的登錄認證了。
創(chuàng)建鏡像任務(wù)
現(xiàn)在我們創(chuàng)建一個 task 任務(wù)來構(gòu)建并推送 docker 鏡像,我們這里使用的示例應(yīng)用根目錄下面已經(jīng)包含了一個 dockerfile 文件了,所以我們直接 clone 代碼就可以獲得:
fromgolang:1.14-alpine workdir/go/src/app copy.. rungoget-d-v./... rungoinstall-v./... cmd["app"]創(chuàng)建一個名為 task-build-push.yaml 的文件,文件內(nèi)容如下所示:
apiversion:tekton.dev/v1beta1 kind:task metadata: name:build-and-push spec: resources: inputs:#定義輸入資源 -name:repo#輸入資源,就是github的那個倉庫 type:git outputs:#定義輸出資源 -name:builtimage#輸出鏡像名字 type:image params: -name:pathtodockerfile#指明dockerfile在倉庫中的哪個位置 type:string default:/workspace/repo/dockerfile#repo資源的路徑 description:dockerfilepath -name:pathtocontext#指明dockerfile在倉庫中的哪個位置 type:string default:/workspace/repo#repo資源的路徑 description:thebuildcontextusedbydockerdaemon steps: -name:build-and-push image:docker:stable script:| #!/usr/bin/envsh dockerloginharbor.k8s.local dockerbuild-t$(resources.outputs.builtimage.url)-f$(params.pathtodockerfile)$(params.pathtocontext) dockerpush$(resources.outputs.builtimage.url)#這邊的參數(shù)都是在input和output中定義的 volumemounts: -name:dockersock#將docker.sock文件掛載進來,使用宿主機dockerdaemon構(gòu)建鏡像 mountpath:/var/run/docker.sock volumes: -name:dockersock hostpath: path:/var/run/docker.sock和前面的測試任務(wù)類似,這里我們同樣將 git 作為輸入資源,此外還定義了一個 dockerfile-path 的參數(shù),用來指定 dockerfile 的路徑,此外還定義了一個名為 builtimage 的鏡像輸出資源,用來定義 docker 鏡像的相關(guān)參數(shù)。然后定義了一個名為 build-and-push 的步驟,這里我們使用 dind 的方式,將宿主機的 /var/run/docker.sock 文件掛載到 docker:stable 的容器中,然后執(zhí)行 script 下面的 docker 鏡像構(gòu)建推送的操作。同樣直接創(chuàng)建上面的資源對象即可:
$kubectlapply-ftask-build-push.yaml task.tekton.dev/build-and-pushcreated創(chuàng)建了 task 任務(wù)過后,要想真正去執(zhí)行這個任務(wù),我們就需要創(chuàng)建一個對應(yīng)的 taskrun 資源對象。
執(zhí)行任務(wù)
和前面一樣,現(xiàn)在我們來創(chuàng)建一個 taskrun 對象來觸發(fā)任務(wù),不同之處在于我們需要指定 task 時需要的 serviceaccount 對象。創(chuàng)建一個名為 taskrun-build-push.yaml 的文件,內(nèi)容如下所示:
#taskrun-build-push.yaml apiversion:tekton.dev/v1beta1 kind:taskrun metadata: name:build-and-push spec: serviceaccountname:build-sa taskref: name:build-and-push#關(guān)聯(lián)定義好的task resources: inputs: -name:repo#指定輸入的倉庫資源 resourceref: name:demo-git outputs:#指定輸出的鏡像資源 -name:builtimage resourceref: name:harbor-image注意這里我們通過 serviceaccountname 屬性指定了 docker 認證信息的 serviceaccount對象,然后通過 taskref 引用我們的任務(wù),以及下面的 resourceref 關(guān)聯(lián)第一部分我們聲明的輸入資源,此外還需要定義一個關(guān)于輸出鏡像的 pipelineresource 資源:
#harbor-image-res.yaml apiversion:tekton.dev/v1alpha1 kind:pipelineresource metadata: name:harbor-image spec: type:image params: -name:url value:harbor.k8s.local/course/tekton-demo:latest#構(gòu)建完的鏡像名稱然后直接創(chuàng)建這個資源對象即可:
$kubectlapply-fharbor-image-res.yaml pipelineresource.tekton.dev/harbor-imagecreated $kubectlapply-ftaskrun-build-push.yaml taskrun.tekton.dev/build-and-pushcreated創(chuàng)建完成后就會觸發(fā)任務(wù)執(zhí)行了,我們可以通過查看 pod 對象狀態(tài)來了解進度:
$kubectlgetpods namereadystatusrestartsage build-and-push-pod-fl65m0/4podinitializing09s $kubectlgettaskrun namesucceededreasonstarttimecompletiontime build-and-pushunknownpending26s現(xiàn)在任務(wù)執(zhí)行的 pod 還在初始化容器階段,我們可以看到 taskrun 的狀態(tài)處于 pending,隔一會兒正常構(gòu)建就會成功了,我們可以查看構(gòu)建任務(wù)的 pod 日志信息:
$kubectlgetpods namereadystatusrestartsage build-and-push-pod-fl65m0/4podinitializing09s $tkntaskrunlogsbuild-and-push [git-source-repo-rsvcf]{"level":"info","ts":1623151584.9503093,"caller":"git/git.go:169","msg":"successfullyclonedhttps://github.com/cnych/tekton-demo@c6c2a85091d538a13c44f85bcee9e861c362b0d3(grafted,head,origin/master)inpath/workspace/repo"} [git-source-repo-rsvcf]{"level":"info","ts":1623151584.968812,"caller":"git/git.go:207","msg":"successfullyinitializedandupdatedsubmodulesinpath/workspace/repo"} [build-and-push]authenticatingwithexistingcredentials... [build-and-push]warning!yourpasswordwillbestoredunencryptedin/root/.docker/config.json. [build-and-push]configureacredentialhelpertoremovethiswarning.see [build-and-push]https://docs.docker.com/engine/reference/commandline/login/#credentials-store [build-and-push] [build-and-push]loginsucceeded [build-and-push]sendingbuildcontexttodockerdaemon12.99mb [build-and-push]step1/6:fromgolang:1.14-alpine ...... [build-and-push]9f9d00b69565:pushed [build-and-push]latest:digest:sha256:521a803fb15d2e05b6168cba36e6e31c548bdd369f274e86c8f5be2118cdb357size:2201 [image-digest-exporter-mpbwq]{"severity":"info","timestamp":"2021-06-08t11:26:43.642545898z","caller":"logging/config.go:116","message":"successfullycreatedthelogger."} [image-digest-exporter-mpbwq]{"severity":"info","timestamp":"2021-06-08t11:26:43.642786678z","caller":"logging/config.go:117","message":"logginglevelsetto:info"} [image-digest-exporter-mpbwq]{"severity":"info","timestamp":"2021-06-08t11:26:43.643090681z","caller":"imagedigestexporter/main.go:59","message":"noindex.jsonfoundfor:builtimage","commit":"7ca5d61"} $kubectlgettaskrun namesucceededreasonstarttimecompletiontime build-and-pushtruesucceeded15m2m24s我們可以看到 taskrun 任務(wù)已經(jīng)執(zhí)行成功了。這個時候其實我們可以在 harbor 上找到我們的鏡像了,當(dāng)然也可以直接使用這個鏡像進行測試:
創(chuàng)建流水線
到這里前面我們的兩個任務(wù) test 和 build-and-push 都已經(jīng)完成了,我們還可以創(chuàng)建一個流水線來將這兩個任務(wù)組織起來,首先運行 test 任務(wù),如果通過了再執(zhí)行后面的 build-and-push 這個任務(wù)。
創(chuàng)建一個名為 pipeline.yaml 的文件,內(nèi)容如下所示:
apiversion:tekton.dev/v1beta1 kind:pipeline metadata: name:test-build-push spec: resources: -name:repo type:git tasks: #運行應(yīng)用測試 -name:test taskref: name:test resources: inputs: -name:repo#task輸入名稱 resource:repo#pipeline資源名稱 #構(gòu)建并推送docker鏡像 -name:build-and-push taskref: name:build-and-push runafter: -test#測試任務(wù)執(zhí)行之后 resources: inputs: -name:repo#task輸入名稱 resource:repo#pipeline資源名稱首先我們需要定義流水線需要哪些資源,可以是輸入或者輸出的資源,在這里我們只有一個輸入,那就是命名為 repo 的應(yīng)用程序源碼的 github 倉庫。接下來定義任務(wù),每個任務(wù)都通過 taskref 進行引用,并傳遞任務(wù)需要的輸入?yún)?shù)。
同樣直接創(chuàng)建這個資源對象即可:
$kubectlapply-fpipeline.yaml pipeline.tekton.dev/test-build-pushcreated前面我們提到過和通過創(chuàng)建 taskrun 去觸發(fā) task 任務(wù)類似,我們可以通過創(chuàng)建一個 pipelinerun 對象來運行流水線。這里我們創(chuàng)建一個名為 pipelinerun.yaml 的 pipelinerun 對象來運行流水線,文件內(nèi)容如下所示:
apiversion:tekton.dev/v1beta1 kind:pipelinerun metadata: name:test-build-push-run spec: serviceaccountname:build-sa pipelineref: name:test-build-push resources: -name:repo resourceref: name:demo-git定義方式和 taskrun 幾乎一樣,通過 serviceaccountname 屬性指定 serviceaccount 對象,pipelineref 關(guān)聯(lián)流水線對象。同樣直接創(chuàng)建這個資源,創(chuàng)建后就會觸發(fā)我們的流水線任務(wù)了:
$kubectlapply-fpipelinerun.yaml pipelinerun.tekton.dev/test-build-push-runcreated $kubectlgetpods|greptest-build-push-run test-build-push-run-build-and-push-xl7wp-pod-hdnbl0/2completed05m27s test-build-push-run-test-4s6qh-pod-tkwzk0/2completed06m5s $kubectllogs-ftest-build-push-run-build-and-push-xl7wp-pod-hdnbl--all-containers {"level":"info","ts":1588908934.442572,"caller":"git/git.go:136","msg":"successfullyclonedhttps://github.com/cnych/tekton-demo@f840e0c390be9a1a6edad76abbde64e882047f05(grafted,head,origin/master)inpath/workspace/repo"} {"level":"info","ts":1588908934.577377,"caller":"git/git.go:177","msg":"successfullyinitializedandupdatedsubmodulesinpath/workspace/repo"} {"level":"info","ts":1588908927.469531,"caller":"creds-init/main.go:44","msg":"credentialsinitialized."} info[0004]retrievingimagemanifestgolang:1.14-alpine ...... app info[0281]takingsnapshotoffullfilesystem... info[0287]resolving11666paths info[0291]cmd["app"] $kubectlgettaskrun|greptest-build-push-run test-build-push-run-build-and-push-xl7wptruesucceeded6m21s65s test-build-push-run-test-4s6qhtruesucceeded6m58s6m21s到這里證明我們的流水線執(zhí)行成功了。我們將 tekton 安裝在 kubernetes 集群上,定義了一個 task,并通過 yaml 清單和 tekton cli 創(chuàng)建 taskrun 對其進行了測試。我們創(chuàng)建了由兩個任務(wù)組成的 tektok 流水線,第一個任務(wù)是從 github 克隆代碼并運行應(yīng)用程序測試,第二個任務(wù)是構(gòu)建一個 docker 鏡像并將其推送到 docker hub 上。到這里我們就完成了使用 tekton 創(chuàng)建 ci/cd 流水線的一個簡單示例,不過這個示例還比較簡單,接下來我們再通過一個稍微復(fù)雜點的應(yīng)用來完成我們的流水線。


東莞到丹東物流專線
福州到銀川物流專線
合肥企業(yè)官網(wǎng)在建設(shè)制作階段應(yīng)注意的幾個方面
西安到和縣物流專線
小程序的發(fā)展價值
贛州到中山物流專線
上海到麗江物流專線
青島到澤州物流專線