我正在尝试使用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
这是完整的日志。
至于更新的问题,现在您需要以某种方式对容器内的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
听起来不错!我会试试。但是
FROM
我的Dockerfile中将是什么?debian:9.11-slim
或者更确切地说gcloud-slim
?而且我可能不需要所有的gcloud组件,还是吗?
我想你可以用
gcloud-slim
。结果将比使用稍大debian-slim
,但可能更方便。Dockerfile的存储库gcloud-slim
位于同一仓库中:github.com/GoogleCloudPlatform/cloud-builders/blob/master/… 是的,您不需要所有gcloud组件,这只是一个例子。我收到一个错误:
/bin/sh: 0: Can't open gcloud container clusters get-credentials cents-ideas --zone=europe-west3-a --project=cents-ideas bazel run //:kubernetes.apply
使用此Dockerfile:github.com/flolude/cents-ideas/blob/feature / ...尝试更新的版本(我忘了-c标志),然后看看它如何进行。实际上,我没有检查此设置。