写点什么

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:184138
用户头像
阿里云容器平台 ACK,企业云原生转型最佳搭档

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

关注

评论

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

使用 Elasticsearch Python SDK 查询 Easysearch

极限实验室

Python elasticsearch easysearch

Marvelous Designer 11 for Mac(3D服装设计软件) v6.1.547中文激活版

Mac相关知识分享

智能运维新课程丨雅菲奥朗“可观测性国际认证课程”圆满结束

雅菲奥朗

可观测性 Observability SRE培训 SRE考试 SRE认证

汽车行业看板实战培训顺利举行

雅菲奥朗

看板 价值流 VSM

业务连续性专题:一致性和并发度的平衡

agnostic

高并发 一致性

IDEA插件—激活JRebel

源字节1号

小程序 开源 前端 后端

开启SRE技术新篇章:SRE Foundation认证培训圆满结课

雅菲奥朗

SRE SRE培训 SRE考试 SRE认证

游戏直播APP开发:如何实现低成本解决方案

软件开发-梦幻运营部

KORG Software VOX Super Continental for mac(组合风琴音乐插件)

Mac相关知识分享

Topaz Video AI for mac(视频增强和修复工具) 4.1.0激活版

Mac相关知识分享

Topaz Gigapixel AI for Mac(照片放大工具) v7.3.0激活版

Mac相关知识分享

spss统计软件:IBM SPSS Statistics 27 (Win/Mac) 中文激活版

你的猪会飞吗

spss数据统计 SPSS26破解版 spss统计软件

比亚迪王传福对话九牧林孝发,一场火出圈的“科技跨界”

Alter

Rust下SM4/AES/RSA加解密

dlzht

rust rsa AES SM4 RustCrypto

支出这么多?收入从何而来

源字节1号

小程序 开源 前端 后端

ByteSRC奖励再升级,抖音单个漏洞奖金提升至20万元

科技热闻

Loopback for Mac(mac虚拟音频设备) v2.4.2激活版

Mac相关知识分享

IoTDB 分段查询语句详解:GROUP BY + 时序语义

Apache IoTDB

硅纪元AI应用推荐 | 5分钟搞定会议纪要,一键生成脑图

硅纪元

通义听悟

行人、司机和“萝卜”的城市路权争夺战,谁能赢?

脑极体

AI

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