Knative 初体验:Build Hello World
作者 | 阿里云智能事業(yè)群技術專家 冬島
Build 模塊提供了一套 Pipeline 機制。Pipeline 的每一個步驟都可以執(zhí)行一個動作,這個動作可以是把源碼編譯成二進制、可以是編譯鏡像也可以是其他的任何事情。Knative Build 執(zhí)行編譯的時候并不需要我們提前準備編譯環(huán)境,所有這些都是直接在 Pod 中執(zhí)行的。當有任務需要執(zhí)行的時候 Build 模塊就自動創(chuàng)建 Pod 進行相應的處理。所以這一系列的動作都是 Kubernetes 原生的。
Knative Build 的幾個關鍵特性
- 一個完整的 Build 是由多個 Builder 構成的 Pipeline,每一個 Builder 可以執(zhí)行一個或多個操作
- 一個 Builder 在執(zhí)行的時候就是一個 container,而且容器的鏡像是聲明 Builder 的時候用戶自己指定的,所以可以在 container 里面執(zhí)行任何指令
- 基于 Kaniko 可以在 Builder 中編譯鏡像以及把鏡像推送到鏡像倉庫等操作
- BuildTemplate 提供了可以重復使用的模板
- Build 過程可以從 git 倉庫 clone 代碼、向鏡像倉庫 push 鏡像。所有這些動作使用到的鑒權信息都可以通過 serviceAccount 進行關聯。直接使用 Kubernetes 原生的能力即可實現
Build 示例
既然是 Hello World 我們就從一個具體的例子談起。
apiVersion: build.knative.dev/v1alpha1 kind: Build metadata:name: example-build-name spec:serviceAccountName: build-auth-examplesource:git:url: https://github.com/example/build-example.gitrevision: mastersteps:- name: ubuntu-exampleimage: ubuntuargs: ["ubuntu-build-example", "SECRETS-example.md"]- image: gcr.io/example-builders/build-exampleargs: ["echo", "hello-example", "build"]- name: dockerfile-pushexampleimage: gcr.io/example-builders/push-exampleargs: ["push", "${IMAGE}"]volumeMounts:- name: docker-socket-examplemountPath: /var/run/docker.sockvolumes:- name: example-volumeemptyDir: {}關鍵字段解釋:
- steps
steps 字段和 template 字段互斥。如果未指定 template 就需要設置 steps 字段。此字段用于指定 Pipeline 的步驟。也可以把 steps 定義在 BuildTemplate 中,這樣就能通過模板來復用 Pipeline 的能力了。
每一個 step 就是制定一個鏡像,在真正執(zhí)行的時候啟動一個容器去做當前 step 的動作。
- Template
如果未設置 steps 就需要指定此字段。此字段通過引用 BuildTemplate 來設置 steps。
- Source
常用的 Source 就是 git repo,通過此字段指定引用的 git repo ,repo 的授權信息通過關聯的 ServiceAccount 進行設定。
- ServiceAccount
從 git repe 克隆代碼和向鏡像倉庫 push 鏡像都需要鑒權信息。這些鑒權信息可以通過 Kubernetes 的 ServiceAccount 進行關聯。
- Volumes
可以通過掛載 volume 的形式掛載 secret 或者 emptyDir 在多個 step 之間共享數據
- Timeout
整個 Build 過程默認超時時間是 10 分鐘,也就是如果在 10 分鐘內沒有還有 step 沒有執(zhí)行完成就會超時退出。但有可以通過 Timeout 字段自定義超時時間。
接下來分別對每一個關鍵字段進行詳細的解讀。
steps
下面這是一個設置 steps 的例子,這個例子中有三個 step。每一個 step 都通過一個鏡像執(zhí)行一個容器完成自己的動作。
spec:steps:- name: ubuntu-exampleimage: ubuntuargs: ["ubuntu-build-example", "SECRETS-example.md"]- image: gcr.io/example-builders/build-exampleargs: ["echo", "hello-example", "build"]- name: dockerfile-pushexampleimage: gcr.io/example-builders/push-exampleargs: ["push", "${IMAGE}"]volumeMounts:- name: docker-socket-examplemountPath: /var/run/docker.sockTemplate
通過 BuildTemplate 來定義可以重復使用的 steps,主要是對 steps 的復用。BuildTemplate 本身是 Kubernetes 中的一個 CRD。CRD 的好處就是可以在用戶之間共享,只要是在同一個 Kubernetes 集群內就可以相互共享,這樣效率更高。
BuildTemplate 除了定義 steps 以外還可以指定 parameters,用戶在使用 BuildTemplate 的時候可以基于 parameters 對 steps 做個性化的設置。而 BuildTemplate 的編寫者也可以通過 parameters 來共享變量。
spec:parameters:# This has no default, and is therefore required.- name: IMAGEdescription: Where to publish the resulting image.# These may be overridden, but provide sensible defaults.- name: DIRECTORYdescription: The directory containing the build context.default: /workspace- name: DOCKERFILE_NAMEdescription: The name of the Dockerfiledefault: Dockerfilesteps:- name: dockerfile-buildimage: gcr.io/cloud-builders/dockerworkingDir: "${DIRECTORY}"args:["build","--no-cache","--tag","${IMAGE}","--file","${DOCKERFILE_NAME}",".",]volumeMounts:- name: docker-socketmountPath: /var/run/docker.sock- name: dockerfile-pushimage: gcr.io/cloud-builders/dockerargs: ["push", "${IMAGE}"]volumeMounts:- name: docker-socketmountPath: /var/run/docker.sock# As an implementation detail, this template mounts the host's daemon socket.volumes:- name: docker-sockethostPath:path: /var/run/docker.socktype: SocketSource
常見的 source 就是指定一個 git repo 或者 emptyDir 共享數據,下面我們分別對這兩種場景進行說明。
- git repo 的例子
下面這個例子的意思是從 https://github.com/knative/build.git clone 代碼,并且指定一個 step 是 cat README.md
spec:source:git:url: https://github.com/knative/build.gitrevision: mastersteps:- image: ubuntuargs: ["cat", "README.md"]- volume 共享數據
下面這個例子是兩個 step,第一個 step 下載文件并保存到 /var/my-volume 中,第二個 step 是使用 /var/my-volume 的內容。
spec:steps:- image: ubuntuentrypoint: ["bash"]args: ["-c", "curl https://foo.com > /var/my-volume"]volumeMounts:- name: my-volumemountPath: /var/my-volume- image: ubuntuargs: ["cat", "/etc/my-volume"]volumeMounts:- name: my-volumemountPath: /etc/my-volumevolumes:- name: my-volumeemptyDir: {}ServiceAccount
下面這個例子是使用了 test-build-robot-git-ssh 這個 ServiceAccount 去關聯 clone 代碼需要的 git ssh 認證信息。通過 ServiceAccount 和 secret 保存認證信息也可以做到在多個用戶之間共享相同的數據,而且可以通過 RBAC 控制不同資源的可見范圍,比較靈活。
- Build 配置如下
- test-build-robot-git-ssh ServiceAccount 配置如下
- ServiceAccount 關聯的 secret 如下
Timeout
下面這個是自定義 Build 超時時間的例子。
spec:timeout: 20msource:git:url: https://github.com/knative/build.gitrevision: mastersteps:- image: ubuntuargs: ["cat", "README.md"]總結
以上是生活随笔為你收集整理的Knative 初体验:Build Hello World的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Alibaba Cluster Data
- 下一篇: 云原生生态周报 Vol.9| K8s v