日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Knative 初体验:CICD 极速入门

發(fā)布時間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Knative 初体验:CICD 极速入门 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


Knative 社區(qū)很早就在討論用 Tekton 替換 Build 模塊的相關(guān)事宜。Knative Build 官方已經(jīng)正式說明不再建議使用 Knative Build 了。

如果你知道 Knative Build 是什么,相信你理解起 Tekton 就是一件很容易的事兒了。

  • Knative Build 對自己的一句話概述是:A Kubernetes-native Build resource.
  • Tekton 對自己的一句話概述是: A K8s-native Pipeline resource. https://tekton.dev

    可以看到兩者的定位非常相近,但在功能上 Tekton 的設(shè)計更加豐富、完整,這也是社區(qū)最終采用 Tekton 的原因。接下來我們就看一下 Tekton 的核心概念。


Tekton 極速入門

Tekton 主要由以下五個核心概念組成:

  • Task
  • TaskRun
  • Pipeline
  • PipelineRun
  • PipelineResource

以上五個概念,每一個都是以 CRD 的形式提供服務(wù)的。下面分別簡述一下這五個概念的含義。

Task

Task 就是一個任務(wù)執(zhí)行模板,之所以說 Task 是一個模板是因為 Task 定義中可以包含變量,Task 在真正執(zhí)行的時候需要給定變量的具體值。Tekton 的 Task 很類似于一個函數(shù)的定義,Task 通過 inputs.params 定義需要哪些入?yún)?#xff0c;并且每一個入?yún)⑦€可以指定默認(rèn)值。Task 的 steps 字段表示當(dāng)前 Task 是有哪些子步驟組成的。每一個步驟具體就是一個鏡像的執(zhí)行,鏡像的啟動參數(shù)可以通過 Task 的入?yún)⑹褂媚0逭Z法進(jìn)行配置。

apiVersion: tekton.dev/v1alpha1 kind: Task metadata:name: task-with-parameters spec:inputs:params:- name: flagstype: array- name: someURLtype: stringsteps:- name: buildimage: registry.cn-hangzhou.aliyuncs.com/knative-sample/alpine:3.9command: ["sh", "-c"]args: [ "echo ${inputs.params.flags} ; echo ${someURL}"]

TaskRun

Task 定義好以后是不能執(zhí)行的,就像一個函數(shù)定義好以后需要調(diào)用才能執(zhí)行一樣。所以需要再定義一個 TaskRun 去執(zhí)行 Task。TaskRun 主要是負(fù)責(zé)設(shè)置 Task 需要的參數(shù),并通過 taskRef 字段引用要執(zhí)行的 Task。

apiVersion: tekton.dev/v1alpha1 kind: TaskRun metadata:name: run-with-parameters spec:taskRef:name: task-with-parametersinputs:params:- name: flagsvalue: "--set"- name: someURLvalue: "https://github.com/knative-sample"

Pipeline

一個 TaskRun 只能執(zhí)行一個 Task,當(dāng)需要編排多個 Task 的時候就需要 Pipeline 出馬了。Pipeline 是一個編排 Task 的模板。Pipeline 的 params 聲明了執(zhí)行時需要的入?yún)ⅰ?Pipeline 的 spec.tasks 定義了需要編排的 Task。Tasks 是一個數(shù)組,數(shù)組中的 task 并不是通過數(shù)組聲明的順序去執(zhí)行的,而是通過 runAfter 來聲明 task 執(zhí)行的順序。Tekton controller 在解析 CRD 的時候會解析 Task 的順序,然后根據(jù)設(shè)定的順序依次去執(zhí)行。Pipeline 在編排 Task 的時候需要給每一個 Task 傳入必需的參數(shù),這些參數(shù)的值可以來自 Pipeline 自身的 params。

apiVersion: tekton.dev/v1alpha1 kind: Pipeline metadata:name: pipeline-with-parameters spec:params:- name: contexttype: stringdescription: Path to contextdefault: /some/where/or/othertasks:- name: task-1taskRef:name: buildparams:- name: pathToDockerFilevalue: Dockerfile- name: pathToContextvalue: "${params.context}"- name: task-2taskRef:name: build-pushrunAfter:- source-to-imageparams:- name: pathToDockerFilevalue: Dockerfile- name: pathToContextvalue: "${params.context}"

PipelineRun

和 Task 一樣, Pipeline 定義完成以后也是不能直接執(zhí)行的,需要 PipelineRun 才能執(zhí)行 Pipeline。PipelineRun 的主要作用是給 Pipeline 設(shè)定必要的入?yún)?#xff0c;并執(zhí)行 Pipeline。

apiVersion: tekton.dev/v1alpha1 kind: PipelineRun metadata:name: pipelinerun-with-parameters spec:pipelineRef:name: pipeline-with-parametersparams:- name: "context"value: "/workspace/examples/microservices/leeroy-web"

PipelineResource

前面已經(jīng)介紹了 Tekton 的四個核心概念。現(xiàn)在我們已經(jīng)知道怎么定義 task、執(zhí)行 task 以及編排 task 了。但可能你還想在 Task 之間共享資源,這就是 PipelineResource 的作用。比如我們可以把 git 倉庫信息放在 PipelineResource 中。這樣所有 Task 就可以共享這份數(shù)據(jù)了。

piVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata:name: wizzbang-gitnamespace: default spec:type: gitparams:- name: urlvalue: https://github.com/wizzbangcorp/wizzbang.git- name: revisionvalue: master

鑒權(quán)信息

git 倉庫、鏡像倉庫這些都是需要鑒權(quán)才能使用的。所以還需要一種設(shè)定鑒權(quán)信息的機(jī)制。Tekton 本身是 Kubernetes 原生的編排系統(tǒng)。所以可以直接使用 Kubernetes 的 ServiceAccount 機(jī)制實現(xiàn)鑒權(quán)。
實例如下:

  • 定義一個保存鏡像倉庫鑒權(quán)信息的 secret
apiVersion: v1 kind: Secret metadata:name: ack-cr-push-secretannotations:tekton.dev/docker-0: https://registry.cn-hangzhou.aliyuncs.com type: kubernetes.io/basic-auth stringData:username: <cleartext non-encoded>password: <cleartext non-encoded>
  • 定義 ServiceAccount ,并且使用上面的 secret
apiVersion: v1 kind: ServiceAccount metadata:name: pipeline-account secrets: - name: ack-cr-push-secret
  • PipelineRun 中引用 ServiceAccount
apiVersion: tekton.dev/v1alpha1 kind: PipelineRun metadata:generateName: tekton-kn-sample- spec:pipelineRef:name: build-and-deploy-pipeline ... ...serviceAccount: pipeline-account


Hello World

https://github.com/knative-sample/tekton-knative 這是一個完整的 Tekton 的 Hello World。下面我們一起體驗一下這個 Hello World。
在開始實戰(zhàn)之前,你需要有一個 Kubernetes 集群,并且還需要安裝 Knative 和 Tekton。tekton-knative 中的 release-v0.5.2.yaml 直接提交到 Kubernetes 集群即可完成 Tekton 的安裝。下面我們開始體驗使用 Tekton 從源碼到構(gòu)建再到部署的自動化過程。

clone 代碼

clone 代碼到本地。

git clone https://github.com/knative-sample/tekton-knative

創(chuàng)建 PipelineResource

主要內(nèi)容在 resources/picalc-git.yaml 文件中。如下所示主要是把 https://github.com/knative-sample/tekton-knative 保存在 resource 中給其他資源使用。

apiVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata:name: tekton-knative-git spec:type: gitparams:- name: revisionvalue: master- name: urlvalue: https://github.com/knative-sample/tekton-knative

創(chuàng)建 task

創(chuàng)建 task,這個例子中我們創(chuàng)建兩個 task:source-to-image 和 deploy-using-kubectl

  • source-to-image

    主要內(nèi)容在 tasks/source-to-image.yaml 文件中。此 task 的主要功能是把源代碼編譯成鏡像。

    主要是使用 kaniko 實現(xiàn)容器內(nèi)編譯 Docker 鏡像的能力。此 Task 的參數(shù)主要是設(shè)置編譯上下文的一些信息,比如:Dockerfile、ContextPath 以及目標(biāo)鏡像 tag 等。
apiVersion: tekton.dev/v1alpha1 kind: Task metadata:name: source-to-image spec:inputs:resources:- name: git-sourcetype: gitparams:- name: pathToContextdescription: The path to the build context, used by Kaniko - within the workspacedefault: .- name: pathToDockerFiledescription: The path to the dockerfile to build (relative to the context)default: Dockerfile- name: imageUrldescription: Url of image repository- name: imageTagdescription: Tag to apply to the built imagedefault: "latest"steps:- name: build-and-pushimage: registry.cn-hangzhou.aliyuncs.com/knative-sample/kaniko-project-executor:v0.10.0command:- /kaniko/executorargs:- --dockerfile=${inputs.params.pathToDockerFile}- --destination=${inputs.params.imageUrl}:${inputs.params.imageTag}- --context=/workspace/git-source/${inputs.params.pathToContext}env:- name: DOCKER_CONFIGvalue: /builder/home/.docker
  • deploy-using-kubectl

    主要內(nèi)容在 tasks/deploy-using-kubectl.yaml 文件中。

    如下所示這個 Task 主要的作用是通過參數(shù)獲取到目標(biāo)鏡像的信息,然后執(zhí)行一條 sed 命令把 Knative Service yaml 中的 __IMAGE__ 替換成目標(biāo)鏡像。再通過 kubectl 發(fā)布到 Kubernetes 中。
apiVersion: tekton.dev/v1alpha1 kind: Task metadata:name: deploy-using-kubectl spec:inputs:resources:- name: git-sourcetype: gitparams:- name: pathToYamlFiledescription: The path to the yaml file to deploy within the git source- name: imageUrldescription: Url of image repository- name: imageTagdescription: Tag of the images to be used.default: "latest"steps:- name: update-yamlimage: alpinecommand: ["sed"]args:- "-i"- "-e"- "s;__IMAGE__;${inputs.params.imageUrl}:${inputs.params.imageTag};g"- "/workspace/git-source/${inputs.params.pathToYamlFile}"- name: run-kubectlimage: registry.cn-hangzhou.aliyuncs.com/knative-sample/kubectl:v0.5.0command: ["kubectl"]args:- "apply"- "-f"- "/workspace/git-source/${inputs.params.pathToYamlFile}"

定義 Pipeline

我們已經(jīng)有兩個 Task 了,現(xiàn)在我們就用一個 PIpeline 來編排這兩個 Task:

apiVersion: tekton.dev/v1alpha1 kind: Pipeline metadata:name: build-and-deploy-pipeline spec:resources:- name: git-sourcetype: gitparams:- name: pathToContextdescription: The path to the build context, used by Kaniko - within the workspacedefault: src- name: pathToYamlFiledescription: The path to the yaml file to deploy within the git source- name: imageUrldescription: Url of image repository- name: imageTagdescription: Tag to apply to the built imagetasks:- name: source-to-imagetaskRef:name: source-to-imageparams:- name: pathToContextvalue: "${params.pathToContext}"- name: imageUrlvalue: "${params.imageUrl}"- name: imageTagvalue: "${params.imageTag}"resources:inputs:- name: git-sourceresource: git-source- name: deploy-to-clustertaskRef:name: deploy-using-kubectlrunAfter:- source-to-imageparams:- name: pathToYamlFilevalue: "${params.pathToYamlFile}"- name: imageUrlvalue: "${params.imageUrl}"- name: imageTagvalue: "${params.imageTag}"resources:inputs:- name: git-sourceresource: git-source

鑒權(quán)信息

如下所示,定義一個 Secret 和 ServiceAccount。并且授予 ServiceAccount 綁定執(zhí)行 Knative Service 的權(quán)限。

apiVersion: v1 kind: Secret metadata:name: ack-cr-push-secretannotations:tekton.dev/docker-0: https://registry.cn-hangzhou.aliyuncs.com type: kubernetes.io/basic-auth stringData:username: <cleartext non-encoded>password: <cleartext non-encoded> --- apiVersion: v1 kind: ServiceAccount metadata:name: pipeline-account secrets: - name: ack-cr-push-secret --- apiVersion: v1 kind: Secret metadata:name: kube-api-secretannotations:kubernetes.io/service-account.name: pipeline-account type: kubernetes.io/service-account-token --- kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata:name: pipeline-role rules: - apiGroups: ["serving.knative.dev"]resources: ["services"]verbs: ["get", "create", "update", "patch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata:name: pipeline-role-binding roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: pipeline-role subjects: - kind: ServiceAccountname: pipeline-account

定義 PIpelineRun

ServiceAccount 對應(yīng)的鑒權(quán)信息是通過和 PIpelineRun 綁定的方式來執(zhí)行的。參見 run/picalc-pipeline-run.yaml 文件

apiVersion: tekton.dev/v1alpha1 kind: PipelineRun metadata:generateName: tekton-kn-sample- spec:pipelineRef:name: build-and-deploy-pipelineresources:- name: git-sourceresourceRef:name: tekton-knative-gitparams:- name: pathToContextvalue: "src"- name: pathToYamlFilevalue: "knative/helloworld-go.yaml"- name: imageUrlvalue: "registry.cn-hangzhou.aliyuncs.com/knative-sample/tekton-knative-helloworld"- name: imageTagvalue: "1.0"trigger:type: manualserviceAccount: pipeline-account

執(zhí)行 HelloWorld

kubectl apply -f tasks/source-to-image.yaml -f tasks/deploy-using-kubectl.yaml -f resources/picalc-git.yaml -f image-secret.yaml -f pipeline-account.yaml -f pipeline/build-and-deploy-pipeline.yaml -f run/picalc-pipeline-run.yaml

查看一下 pod 信息,可能是下面這樣:

└─# kubectl get pod NAME READY STATUS RESTARTS AGE tekton-kn-sample-45d84-deploy-to-cluster-wfrzx-pod-f093ef 0/3 Completed 0 8h tekton-kn-sample-45d84-source-to-image-7zpqn-pod-c2d20c 0/2 Completed 0 8h


歡迎加入 Knative 交流群

總結(jié)

以上是生活随笔為你收集整理的Knative 初体验:CICD 极速入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。