做者 | 阿里云智能事业群技术专家 冬岛 git
Build 模块提供了一套 Pipeline 机制。Pipeline 的每个步骤均可以执行一个动做,这个动做能够是把源码编译成二进制、能够是编译镜像也能够是其余的任何事情。Knative Build 执行编译的时候并不须要咱们提早准备编译环境,全部这些都是直接在 Pod 中执行的。当有任务须要执行的时候 Build 模块就自动建立 Pod 进行相应的处理。因此这一系列的动做都是 Kubernetes 原生的。github
既然是 Hello World 咱们就从一个具体的例子谈起。docker
apiVersion: build.knative.dev/v1alpha1 kind: Build metadata: name: example-build-name spec: 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: {}
关键字段解释:ubuntu
steps 字段和 template 字段互斥。若是未指定 template 就须要设置 steps 字段。此字段用于指定 Pipeline 的步骤。也能够把 steps 定义在 BuildTemplate 中,这样就能经过模板来复用 Pipeline 的能力了。api
每个 step 就是制定一个镜像,在真正执行的时候启动一个容器去作当前 step 的动做。bash
若是未设置 steps 就须要指定此字段。此字段经过引用 BuildTemplate 来设置 steps。ssh
经常使用的 Source 就是 git repo,经过此字段指定引用的 git repo ,repo 的受权信息经过关联的 ServiceAccount 进行设定。curl
从 git repe 克隆代码和向镜像仓库 push 镜像都须要鉴权信息。这些鉴权信息能够经过 Kubernetes 的 ServiceAccount 进行关联。socket
能够经过挂载 volume 的形式挂载 secret 或者 emptyDir 在多个 step 之间共享数据ide
整个 Build 过程默认超时时间是 10 分钟,也就是若是在 10 分钟内没有还有 step 没有执行完成就会超时退出。但有能够经过 Timeout 字段自定义超时时间。
接下来分别对每个关键字段进行详细的解读。
下面这是一个设置 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
经过 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 就是指定一个 git repo 或者 emptyDir 共享数据,下面咱们分别对这两种场景进行说明。
下面这个例子的意思是从 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"]
下面这个例子是两个 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: {}
下面这个例子是使用了 test-build-robot-git-ssh 这个 ServiceAccount 去关联 clone 代码须要的 git ssh 认证信息。经过 ServiceAccount 和 secret 保存认证信息也能够作到在多个用户之间共享相同的数据,并且能够经过 RBAC 控制不一样资源的可见范围,比较灵活。
apiVersion: build.knative.dev/v1alpha1 kind: Build metadata: name: test-build-with-serviceaccount-git-ssh labels: expect: succeeded spec: 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"]
apiVersion: v1 kind: ServiceAccount metadata: name: test-build-robot-git-ssh secrets: - name: test-git-ssh
apiVersion: v1 kind: Secret metadata: name: test-git-ssh annotations: build.knative.dev/git-0: github.com type: kubernetes.io/ssh-auth data: # 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]
下面这个是自定义 Build 超时时间的例子。
spec: timeout: 20m source: git: url: https://github.com/knative/build.git revision: master steps: - image: ubuntu args: ["cat", "README.md"]