红帽白皮书新鲜出炉!点击获取,让你的云战略更胜一筹! 了解详情
写点什么

Knative 初体验:Build Hello World

  • 2019-08-09
  • 本文字数:3511 字

    阅读完需:约 12 分钟

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/v1alpha1kind: Buildmetadata:  name: example-build-namespec:  serviceAccountName: build-auth-example  source:    git:      url: https://github.com/example/build-example.git      revision: master  steps:    - name: ubuntu-example      image: ubuntu      args: ["ubuntu-build-example", "SECRETS-example.md"]    - image: gcr.io/example-builders/build-example      args: ["echo", "hello-example", "build"]    - name: dockerfile-pushexample      image: gcr.io/example-builders/push-example      args: ["push", "${IMAGE}"]      volumeMounts:        - name: docker-socket-example          mountPath: /var/run/docker.sock  volumes:    - name: example-volume      emptyDir: {}
复制代码


关键字段解释:


  • 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-example      image: ubuntu      args: ["ubuntu-build-example", "SECRETS-example.md"]    - image: gcr.io/example-builders/build-example      args: ["echo", "hello-example", "build"]    - name: dockerfile-pushexample      image: gcr.io/example-builders/push-example      args: ["push", "${IMAGE}"]      volumeMounts:        - name: docker-socket-example          mountPath: /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: IMAGE      description: Where to publish the resulting image.
# These may be overridden, but provide sensible defaults. - name: DIRECTORY description: The directory containing the build context. default: /workspace - name: DOCKERFILE_NAME description: The name of the Dockerfile default: Dockerfile
steps: - name: dockerfile-build image: gcr.io/cloud-builders/docker workingDir: "${DIRECTORY}" args: [ "build", "--no-cache", "--tag", "${IMAGE}", "--file", "${DOCKERFILE_NAME}", ".", ] volumeMounts: - name: docker-socket mountPath: /var/run/docker.sock
- name: dockerfile-push image: gcr.io/cloud-builders/docker args: ["push", "${IMAGE}"] volumeMounts: - name: docker-socket mountPath: /var/run/docker.sock
# As an implementation detail, this template mounts the host's daemon socket. volumes: - name: docker-socket hostPath: path: /var/run/docker.sock type: 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.git      revision: master  steps:    - image: ubuntu      args: ["cat", "README.md"]
复制代码


  • volume 共享数据


下面这个例子是两个 step,第一个 step 下载文件并保存到 /var/my-volume 中,第二个 step 是使用 /var/my-volume 的内容。


spec:  steps:    - image: ubuntu      entrypoint: ["bash"]      args: ["-c", "curl https://foo.com > /var/my-volume"]      volumeMounts:        - name: my-volume          mountPath: /var/my-volume
- image: ubuntu args: ["cat", "/etc/my-volume"] volumeMounts: - name: my-volume mountPath: /etc/my-volume
volumes: - name: my-volume emptyDir: {}
复制代码


ServiceAccount

下面这个例子是使用了 test-build-robot-git-ssh 这个 ServiceAccount 去关联 clone 代码需要的 git ssh 认证信息。通过 ServiceAccount 和 secret 保存认证信息也可以做到在多个用户之间共享相同的数据,而且可以通过 RBAC 控制不同资源的可见范围,比较灵活。


  • Build 配置如下


apiVersion: build.knative.dev/v1alpha1kind: Buildmetadata:  name: test-build-with-serviceaccount-git-ssh  labels:    expect: succeededspec:  serviceAccountName: test-build-robot-git-ssh  source:    git:      url: git@github.com:knative/build.git      revision: master
steps: - name: config image: ubuntu command: ["/bin/bash"] args: ["-c", "cat README.md"]
复制代码


  • test-build-robot-git-ssh ServiceAccount 配置如下


apiVersion: v1kind: ServiceAccountmetadata:  name: test-build-robot-git-sshsecrets:  - name: test-git-ssh
复制代码


  • ServiceAccount 关联的 secret 如下


apiVersion: v1kind: Secretmetadata:  name: test-git-ssh  annotations:    build.knative.dev/git-0: github.comtype: kubernetes.io/ssh-authdata:  # Generated by:  # cat id_rsa | base64 -w 0  ssh-privatekey: LS0tLS1CRUdJTiBSU0EgUFJJVk.....[example]  # Generated by:  # ssh-keyscan github.com | base64 -w 0  known_hosts: Z2l0aHViLmNvbSBzc2g.....[example]
复制代码


Timeout

下面这个是自定义 Build 超时时间的例子。


spec:  timeout: 20m  source:    git:      url: https://github.com/knative/build.git      revision: master  steps:    - image: ubuntu      args: ["cat", "README.md"]
复制代码


相关文章:


《初识 Knative:跨平台的 Serverless 编排框架》


《Knative 初体验:Serving Hello World》


《Knative 初体验:Eventing Hello World》


2019-08-09 08:183697
用户头像
阿里云容器平台 ACK,企业云原生转型最佳搭档

发布了 43 篇内容, 共 22.5 次阅读, 收获喜欢 80 次。

关注

评论

发布
暂无评论
发现更多内容

作业三架构设计文档

大肚皮狒狒

模块三作业

梦寐凯旋

架构实战营

架构实战营 模块三作业

fazinter

架构实战营

消息队列的架构设计文档

Geek_bded54

架构实战营 模块三:课后作业

Ahu

#架构实战营

消息中间件详细架构设计文档

白发青年

架构实战营

架构训练营模块3作业-消息队列架构设计

架构实战营 模块三作业

Dylan

架构实战营

模块3作业-架构设计文档

yu

架构实战营

Go 语言垃圾回收

escray

学习 极客时间 Go 语言 5月日更

已收藏!2021年最新腾讯Android面经,超详细

欢喜学安卓

android 程序员 面试 移动开发

腾讯T3大佬亲自讲解!我的腾讯安卓面试经历分享,分分钟搞定!

欢喜学安卓

android 程序员 面试 移动开发

五月学习心得(二)

攻城先森

学习 音视频 5月日更

架构训练营模块三作业

Geek_e0c25c

架构训练营

学妹问我:OpenJDK是什么?作为师哥,必须万字详解屁颠屁颠奉上

牛哄哄的java大师

Java

智能家居控制原理

lenka

5月日更

架构实战营 模块3 作业

CR

Ansible Inventory

耳东@Erdong

ansible 5月日更

Dubbo 泛化引用

青年IT男

dubbo

最好的关系,是成就彼此

小天同学

爱情 日常感悟 个人思考 5月日更 相处之道

模块三 - 消息队列架构设计文档

华仔架构训练营

聊聊传统企业如何做好数字化转型

长沙造纸农

中台 数字化转型 企业 数字化 中台战略

ARTS - week 9

steve_lee

推荐一个GitHub的开源电子书仓库,值得收藏

C语言与CPP编程

Java c++ Python C语言 电子书

WebRTC下摄像头的采集分析

小辣条

ios WebRTC

音频变速变调-sonic源码分析

floer rivor

音视频

品牌认同感与鄙视链

ES_her0

5月日更

架构实战营 模块3作业

夏日

架构训练营

架构实战营模块 3 作业

阿体

打破思维定式(四)

Changing Lin

5月日更

模块三:课后作业

黄先生

架构实战营

Knative 初体验:Build Hello World_文化 & 方法_冬岛_InfoQ精选文章