bazel google-cloud-build kubectl kubernetes

kubernetes - Cloud Build Bazel错误:“ kubectl工具链配置不正确,因此无法执行应用”

发布于 2020-04-12 19:10:04

我正在尝试使用Rules_k8s将Bazel部署到我的Kubernetes集群。

因此,我有此cloudbuild.yaml文件,文件由Google Cloud Build执行:

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

//:kubernetes只是一个k8s_objects


在我的本地计算机上,运行bazel run //:kubernetes.apply正常,但尽管Google Cloud Build成功,但仍记录了这些错误。因此,该配置不适用于我的Kubernetes集群:

Target //:kubernetes.apply up-to-date:
  bazel-bin/kubernetes.apply
INFO: Elapsed time: 29.863s, Critical Path: 0.14s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/kubernetes.apply
INFO: Build Event Protocol files produced successfully.
INFO: Build completed successfully, 1 total action
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so projection_database_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so projection_database_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.

我也从bazel缓存中得到警告:

DEBUG: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_k8s/toolchains/kubectl/kubectl_toolchain.bzl:28:9: No kubectl tool was found or built, executing run for rules_k8s targets might not work.

PS:使用时出现相同的错误 //:kubernetes.create

我的设定

部署

load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
k8s_object(
  name = "k8s_deployment",
  kind = "deployment",
  cluster = "gke_cents-ideas_europe-west3-a_cents-ideas",
  template = ":ideas.deployment.yaml",
  images = {
    "gcr.io/cents-ideas/ideas:latest": ":image"
  },
)

服务

k8s_object(
  name = "k8s_service",
  kind = "service",
  cluster = "gke_cents-ideas_europe-west3-a_cents-ideas",
  template = ":ideas.service.yaml",
)

集合体

load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")
k8s_objects(
    name = "k8s",
    objects = [
      ":k8s_deployment",
      ":k8s_service",
    ]
)

最终组成

k8s_objects(
    name = "kubernetes",
    objects = [
        "//services/ideas:k8s",
        # ...
    ]
)

更新资料

我现在尝试使用Bazel和kubectl制作自己的docker映像:

FROM gcr.io/cloud-builders/bazel

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl

我将其推送到GCR,并将其更改cloudbuild.yaml为:

steps:
  - name: eu.gcr.io/cents-ideas/bazel-kubectl
    args: ["run", "//:kubernetes.apply"]

我首先注意到,这一步骤比以前花费了更长的时间。但是最后它抛出一个错误:

$ /usr/local/bin/kubectl --kubeconfig= --cluster=gke_cents-ideas_europe-west3-a_cents-ideas --context= --user= apply -f -
error: cluster "gke_cents-ideas_europe-west3-a_cents-ideas" does not exist

是完整的日志。

查看更多

提问者
Florian Ludewig
被浏览
87
Ray 2020-02-04 20:14

至于更新的问题,现在您需要以某种方式对容器内的GKE进行身份验证。

首先,我建议将gcloud工具安装到您的容器中。顺便说一句,至于1.2 GB的巨大容器大小,那是因为cloud-builders/bazel它很大:)

看看我们的超薄bazel容器版本示例:https : //github.com/aspect-development/bazel-k8s-example/blob/master/tools/Dockerfile.dazel

这里是用于安装gcloud和kubectl的Dockerfile,因此您可以从两个文件中获取所需的部分:https : //github.com/GoogleCloudPlatform/cloud-builders/blob/master/gcloud/Dockerfile

第二件事是进行身份验证,在安装gcloud之后,它应该很容易。总体cloudbuild步骤应类似于以下内容:

- name: <link to your container>
  entrypoint: /bin/sh
  args:
  - -c
  - |
    gcloud container clusters get-credentials cents-ideas --zone europe-west3-a --project cents-ideas
    bazel run //:kubernetes.apply