kubernetes - 云构建Bazel错误: "kubectl toolchain was not properly configured so apply cannot be executed"

标签 kubernetes bazel kubectl google-cloud-build

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

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

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.

P.S.:使用 //: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

Here是完整的日志。

最佳答案

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

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

看看我们关于 slim 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 后应该很容易。 总体云构建步骤应类似于以下内容:

- 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

关于kubernetes - 云构建Bazel错误: "kubectl toolchain was not properly configured so apply cannot be executed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60029288/

相关文章:

docker - 如何curl部署在Kubernetes上的容器?

Kubernetes RBAC 限制用户在 Kubernetes 仪表板上仅查看所需资源

python - 从 C++ 自动生成 python 模块的 Tensorflow 源代码

linux - 在 Intel Atom 上构建 Tensorflow 的错误

kubernetes - kubectl get pods 不显示 READY-STATUS-RESTARTS

kubernetes - kubectl - 如何通过命令行将服务规范类型编辑为 LoadBalancer?

nginx - 闲置一段时间运行 nginx 和 kubernetes 后网站无法访问

docker - 在k8s进程中, “kube-controller-manager”是来自docker容器的 “child process”。为什么k8s具有这种架构?

c++ - 如何在 Conan.io 中使用 Bazel 作为构建工具

proxy - 从另一台机器访问时未经授权的 kubectl 代理