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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Knative 初体验:Build Hello World

發布時間:2025/3/20 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Knative 初体验:Build Hello World 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作者 | 阿里云智能事業群技術專家 冬島

Build 模塊提供了一套 Pipeline 機制。Pipeline 的每一個步驟都可以執行一個動作,這個動作可以是把源碼編譯成二進制、可以是編譯鏡像也可以是其他的任何事情。Knative Build 執行編譯的時候并不需要我們提前準備編譯環境,所有這些都是直接在 Pod 中執行的。當有任務需要執行的時候 Build 模塊就自動創建 Pod 進行相應的處理。所以這一系列的動作都是 Kubernetes 原生的。

Knative Build 的幾個關鍵特性

  • 一個完整的 Build 是由多個 Builder 構成的 Pipeline,每一個 Builder 可以執行一個或多個操作
  • 一個 Builder 在執行的時候就是一個 container,而且容器的鏡像是聲明 Builder 的時候用戶自己指定的,所以可以在 container 里面執行任何指令
  • 基于 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 就是制定一個鏡像,在真正執行的時候啟動一個容器去做當前 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 沒有執行完成就會超時退出。但有可以通過 Timeout 字段自定義超時時間。

接下來分別對每一個關鍵字段進行詳細的解讀。

steps

下面這是一個設置 steps 的例子,這個例子中有三個 step。每一個 step 都通過一個鏡像執行一個容器完成自己的動作。

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.sock

Template

通過 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: Socket

Source

常見的 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 配置如下
apiVersion: build.knative.dev/v1alpha1 kind: Build metadata:name: test-build-with-serviceaccount-git-sshlabels:expect: succeeded spec:serviceAccountName: test-build-robot-git-sshsource:git:url: git@github.com:knative/build.gitrevision: mastersteps:- name: configimage: ubuntucommand: ["/bin/bash"]args: ["-c", "cat README.md"]
  • test-build-robot-git-ssh ServiceAccount 配置如下
apiVersion: v1 kind: ServiceAccount metadata:name: test-build-robot-git-ssh secrets:- name: test-git-ssh
  • ServiceAccount 關聯的 secret 如下
apiVersion: v1 kind: Secret metadata:name: test-git-sshannotations:build.knative.dev/git-0: github.com type: kubernetes.io/ssh-auth data:# Generated by:# cat id_rsa | base64 -w 0ssh-privatekey: LS0tLS1CRUdJTiBSU0EgUFJJVk.....[example]# Generated by:# ssh-keyscan github.com | base64 -w 0known_hosts: Z2l0aHViLmNvbSBzc2g.....[example]

Timeout

下面這個是自定義 Build 超時時間的例子。

spec:timeout: 20msource:git:url: https://github.com/knative/build.gitrevision: mastersteps:- image: ubuntuargs: ["cat", "README.md"]

總結

以上是生活随笔為你收集整理的Knative 初体验:Build Hello World的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。