我正面临一个有趣的挑战,我正在尝试在具有适当配置的 docker 映像中运行 kubectl,以访问我的集群。
我已经能够创建图像,kubecod
FROM ubuntu:xenial
WORKDIR /project
RUN apt-get update && apt-get install -y \
curl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.21.0/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
ENTRYPOINT ["kubectl"]
#
CMD ["version"]
当我运行映像时,容器运行正常,给出了预期的答案。
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T13:41:02Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"18+", GitVersion:"v1.18.9-eks-d1db3c", GitCommit:"d1db3c46e55f95d6a7d3e5578689371318f95ff9", GitTreeState:"clean", BuildDate:"2020-10-20T22:18:07Z", GoVersion:"go1.13.15", Compiler:"gc", Platform:"linux/amd64"}
但是,我的目标是创建一个图像,将 kubectl 连接到我的节点。读取doc,我需要在以下文件夹~/.kube/config
我已经创建了另一个 Dockerfile 来构建另一个映像 kubedock
,使用正确的配置文件并创建必要的目录 .kube
FROM ubuntu:xenial
#setup a working directory
WORKDIR /project
RUN apt-get update && apt-get install -y \
curl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.21.0/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
#create the directory
RUN mkdir .kube
#Copy the config file to the .kube folder
COPY ./config .kube
ENTRYPOINT ["kubectl"]
CMD ["cluster-info dump"]
但是,当我在容器中运行新图像时,出现以下消息
me@os:~/_projects/kubedock$ docker run --name kubecont kubedock
Error: unknown command "cluster-info dump" for "kubectl"
Run 'kubectl --help' for usage.
不确定我错过了什么。
欢迎任何提示。
干杯。
答案 0 :(得分:1)
我不清楚你的 K8s 集群在哪里运行, 如果您在 GKE 中运行集群,则需要运行以下内容:
gcloud container clusters get-credentials $CLUSTER_NAME --zone $CLUSTER_ZONE
这将在用户主目录中创建 ~/.config/gcloud 文件树。
在 AWS EKS 上,您需要设置 ~/.aws/credentials 和其他 IAM 设置。
我建议您发布 K8s 集群运行位置的详细信息,我们可以从那里获取。
PS 也许如果您将工作用户的主机主目录挂载/复制到 docker 中,它会起作用。
答案 1 :(得分:0)
答案是
CMD ["cluster-info","dump"]
或者当 kubectl
命令行中有空格时,用逗号分隔。