简体   繁体   English

Cloud Build kubectl - 如何将上一步的输出应用到 Kubernetes 集群

[英]Cloud Build kubectl - How to Apply Output of Previous Step to Kubernetes Cluster

I have a simple cloudbuild.yaml file which runs a Bazel command.我有一个运行 Bazel 命令的简单cloudbuild.yaml文件。 This command returns a Kubernetes configuration in form as a log output.此命令以日志输出的形式返回 Kubernetes 配置。

My goal is to take the output of the first step and apply it to my Kubernetes cluster.我的目标是将第一步的输出应用到我的 Kubernetes 集群中。

steps:
  - name: gcr.io/cloud-builders/bazel
    args: ["run", "//:kubernetes"]

  - name: "gcr.io/cloud-builders/kubectl"
    args: ["apply", "<log output of previous step>"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
      - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

Update更新

I've tried the following:我尝试了以下方法:

- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " > kubernetes.yaml",
    ]

- name: "gcr.io/cloud-builders/kubectl"
  args: ["apply", "-f", "kubernetes.yaml"]
  env:
    - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
    - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

But then I get this error:但是后来我收到了这个错误:

Running: kubectl apply -f kubernetes.yaml
error: the path "kubernetes.yaml" does not exist

Volume mount the same directory (not file) into both steps.将相同的目录(不是文件)卷装入两个步骤。

Pipe the Bazel command output to a file.将 Bazel 命令输出通过管道传输到文件。

Reference that file in the kubectl apply --filename= stepkubectl apply --filename=步骤中引用该文件

Example例子

options:
  volumes:
    - name: test
      path: /test
steps:
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/test"
  - name: busybox
    entrypoint: "/bin/sh"
    args:
      - "-c"
      - "touch /test/freddie"
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/test"
  - name: busybox
    args:
      - "cp"
      - "/test/freddie"
      - "/workspace"
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/workspace"

Using options to define volumes applies the volume to all steps;使用options定义volumes将体积应用于所有步骤; you may alternatively simply repeat the volumes in each step.您也可以简单地重复每个步骤中的volumes

The example -- hopefully -- shows how to use both the default /workspace and a user-defined /test volume to create a file in a volume (and to copy this file to the default /workspace volume to prove that it is added.该示例(希望如此)展示了如何使用默认/workspace和用户定义的/test卷在卷中创建文件(并将此文件复制到默认/workspace卷以证明它已添加。

Output:输出:

BUILD
Starting Step #0
Step #0: Already have image: busybox
Step #0: total 0
Finished Step #0
Starting Step #1
Step #1: Already have image: busybox
Finished Step #1
Starting Step #2
Step #2: Already have image: busybox
Step #2: total 0
Step #2: -rw-r--r--    1 root     root             0 Feb  4 17:53 freddie
Finished Step #2
Starting Step #3
Step #3: Already have image: busybox
Finished Step #3
Starting Step #4
Step #4: Already have image: busybox
Step #4: total 4
Step #4: -rw-r--r--    1 1000     1000           460 Feb  4 17:53 cloudbuild.yaml
Step #4: -rw-r--r--    1 root     root             0 Feb  4 17:53 freddie
Finished Step #4
PUSH
DONE

Here's how to mount the volume:以下是安装卷的方法:

https://cloud.google.com/cloud-build/docs/build-config#volumes https://cloud.google.com/cloud-build/docs/build-config#volumes

Basically add:基本上添加:

  volumes:
  - name: 'vol1'
    path: '/persistent_volume'

Then reference full path /persistent_volume/filename/ when writing / reading to your file.然后在写入/读取/persistent_volume/filename/时引用完整路径/persistent_volume/filename/

As everyone already suggested here use volumes .正如每个人都已经在这里建议的那样使用

Tweak your cloudbuild.yaml file like this:像这样调整你的cloudbuild.yaml文件:

- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " > /workspace/kubernetes.yaml",
    ]

- name: "gcr.io/cloud-builders/kubectl"
  args: ["apply", "-f", "/workspace/kubernetes.yaml"]
  env:
    - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
    - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM