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相关推荐
- Knative 初体验:CICD 极速入门
Knative 社区很早就在讨论用 Tekton 替换 Build 模块的相关事宜.Knative Build 官方已经正式说明不再建议使用 Knative Build 了. 如果你知道 Knativ ...
- Knative 初体验:Serving Hello World
通过前面两章的学习你已经掌握了很多 Knative 的理论知识,基于这些知识你应该对 Knative 是谁.它来自哪里以及它要做什么有了一定的认识.可是即便如此你可能还是会有一种犹抱琵琶半遮面,看不清 ...
- 亚马逊云科技Build On - Serverless创新零售初体验
亚马逊云科技Build On - Serverless创新零售初体验 亚马逊云科技Build On - 基于 Serverless 构建零售创新应用 参与目的 参与过程 活动开始(直播) 活动实操 活 ...
- MapReduce编程初体验
需求:在给定的文本文件中统计输出每一个单词出现的总次数 第一步: 准备一个aaa.txt文本文档 第二步: 在文本文档中随便写入一些测试数据,这里我写入的是 hello,world,hadoop he ...
- 小程序 缩放_缩放流星应用程序的初体验
小程序 缩放 by Elie Steinbock 埃莉·斯坦博克(Elie Steinbock) 缩放流星应用程序的初体验 (First Experiences Scaling a Meteor Ap ...
- wxWidgets刚開始学习的人导引(3)——wxWidgets应用程序初体验
wxWidgets刚開始学习的人导引全文件夹 PDF版及附件下载 1 前言 2 下载.安装wxWidgets 3 wxWidgets应用程序初体验 4 wxWidgets学习资料及利用方法指导 5 ...
- Windows Embedded Standard开发初体验(二)
支持Silverlight的Windows Embedded Standard 好了,完成安装之后,我们就可以来做Windows Embedded Standard的第一个操作系统镜像了.在开始菜单中 ...
- Spring环境搭建,IoC容器初体验~
由于最近的任务是关于IoC配置文件格式的转换,所以需要从Spring的IoC容器开始学起,今天根据网上的介绍搭建了Spring环境,并对其IoC容器进行了初体验.文章中涉及到的软件以及推荐的一本关于S ...
- Flutter初体验(二)—— 创建第一个Flutter APP
Flutter初体验(二)--- 创建第一个Flutter APP 在第一篇文章 Flutter初体验(一)---Mac 安装配置,学习了配置 Flutter 开发环境,并运行了Demo项目,本篇根据 ...
最新文章
- android 网络编程--URL获取数据/图片
- Jenkins忘记密码的修复方法(Windows/Linux)
- pandas学习笔记二之pandas选择器
- 暑期训练日志----2018.8.19
- 【树莓派】树莓派(Debian)- root用户无法使用SSH登录
- edu汇编语言——实训课程
- Python-cvxopt库的使用(1)(解决LP问题)
- 的it生活_IT行业是干啥的
- Android OpenGL 开发
- 西安交大计算机技术考研初复试,西安交大计算机考研初试、复试信息
- bootstrap的表单验证 vue_分享几个基于Vue的UI库和开源项目
- debian:必须有官方源,难道国内镜像都是僵尸源?
- 线性判别分析LDA算法与python实现
- Mirth Connect 第一章 快速安装
- 【A星算法】--第四篇(A星算法)
- java gui 字体颜色练习
- python进阶day13
- EMW3080+STC15轻松实现设备上云4(阿里云物联网平台、智能生活开放平台)
- 计算器算贝塞尔公式_绝版应用!超级好用的计算器!
- 给定两个数组arrx和arry,长度都为N。代表二维平面上有N个点,第i个点的x 坐标和y坐标分别为arrx[i]和arry[i],返回求一条直线最多能穿过多少个点?