简体   繁体   English

配置 kubectl 以到达 docker 上的集群

[英]configure kubectl to reach cluster on docker

I'm facing an interesting challenge, I'm trying to run kubectl in a docker image with a proper configuration, to reach my cluster.我正面临一个有趣的挑战,我正在尝试在具有适当配置的 docker 映像中运行 kubectl 以到达我的集群。

I've been able to create the image, kubecod我已经能够创建图像, 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"]

When I run the image, the container is functionning correctly, giving me the expected answer.当我运行图像时,容器运行正常,给了我预期的答案。

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"}

However, my aim is to create an image with the kubectl connecting to my node.但是,我的目标是创建一个将 kubectl 连接到我的节点的图像。 Reading the doc , I need to add a configuration file in the following folder ~/.kube/config阅读文档,我需要在以下文件夹中添加一个配置文件~/.kube/config

I've created another Dockerfile to build another image, kubedock , with the proper config file and the creation of the requisite directory, .kube我创建了另一个 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"]

However, when I run the new image in a container, I have the following message但是,当我在容器中运行新图像时,出现以下消息


me@os:~/_projects/kubedock$ docker run --name kubecont kubedock
Error: unknown command "cluster-info dump" for "kubectl"
Run 'kubectl --help' for usage.

Not sure what I'm missing.不知道我错过了什么。

Any hints are welcomed.欢迎任何提示。

Cheers.干杯。

It's not clear to me where your K8s cluster is running, If you run your cluster in GKE you will need to run something like:我不清楚你的 K8s 集群在哪里运行,如果你在 GKE 中运行你的集群,你需要运行类似的东西:

gcloud container clusters get-credentials $CLUSTER_NAME --zone $CLUSTER_ZONE

Which will create the ~/.config/gcloud tree of files in the users home directory.这将在用户主目录中创建 ~/.config/gcloud 文件树。

On AWS EKS you will need to setup ~/.aws/credentials and other IAM settings.在 AWS EKS 上,您需要设置 ~/.aws/credentials 和其他 IAM 设置。

I suggest you post the details of where your K8s cluster is running and we can take it from there.我建议您发布您的 K8s 集群运行位置的详细信息,我们可以从那里获取。

PS maybe if you mount/copy the host home directory of a working user into the docker it will work. PS也许如果您将工作用户的主机主目录安装/复制到 docker 它将起作用。

The answer is that答案是

    CMD ["cluster-info","dump"]

Or when there is a space in the kubectl command line, separate it with a comma.或者当kubectl命令行中有空格时,用逗号分隔。

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

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