【问题标题】:Make Kubernetes cluster search for image in multiple places让 Kubernetes 集群多处搜索镜像
【发布时间】:2018-10-22 09:44:16
【问题描述】:

我只想通过名称和摘要而不是完整的 URI 来引用容器映像。

我有提到 docker 镜像的强散列 Kubernetes 对象规范文件。 我们有多个带有多个子目录的私有容器镜像仓库(prod、staging、dev)。

我需要一种能够在不修改 Kubernetes 对象规范文件的情况下指定 docker 映像“搜索前缀”的方法。

示例: 我有一个签入的哈希文件,其中包含以下行:

image: something@sha256:2635462354664526623546235645264

图片首先推送到gcr.io/dev-bucket/commit-hash/something。然后复制到gcr.io/staging-bucket/commit-hash/something,最后复制到gcr.io/prod-bucket/something

我希望能够告诉 Kubernetes 可能的图像搜索位置/前缀,以便我可以使用该目标文件而无需进行任何更改。 (当文件形成强哈希树时,修改文件就成了一个大问题。)

【问题讨论】:

    标签: image docker kubernetes containers


    【解决方案1】:

    我认为你可以使用imagePullSecrets

    您应该使用 URL 创建一个 docker-registry 密码以及每个阶段/存储桶所需的所有身份验证。

    kubectl create secret docker-registry dev-bucket --docker-server=https://hub.docker.com --docker-username=user --docker-email=user@example.com --docker-password=password
    

    然后在创建POD 时,您应该执行以下操作:

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
    - name: test-container
      image: <registry_name>/<image_name>:<tagname>
      imagePullSecrets:
      - name: dev-bucket
    

    请查看Google Cloud Registry (GCR) with external Kubernetes 指南以及如何使用Pull an Image from a Private Registry

    不要忘记在 gcloud 中创建服务帐户,并拥有 GCR 权限和服务帐户的密钥。

    您应该将tags 添加到您的图片中,这样您就可以将一张图片推送到不同的标签。

    编辑:

    这是一个例子:

    $ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    d1725b59e92d: Pull complete
    Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
    Status: Downloaded newer image for hello-world:latest
    

    现在我们将为其添加新标签

    $ docker tag hello-world gcr.io/project-for-x/hello-world:dev_latest
    $ docker push gcr.io/project-for-x/hello-world:dev_latest
    The push refers to repository [gcr.io/project-for-x/hello-world]
    428c97da766c: Layer already exists
    dev_latest: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524
    $ docker tag hello-world gcr.io/project-for-x/hello-world:stage_latest                                                                                                                    
    $ docker push gcr.io/project-for-x/hello-world:stage_latest                                                                                                                               
    The push refers to repository [gcr.io/project-for-x/hello-world]
    428c97da766c: Layer already exists
    stage_latest: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524
    $ docker tag hello-world gcr.io/project-for-x/hello-world:prod_latest                                                                                                                     
    $ docker push gcr.io/project-for-x/hello-world:prod_latest                                                                                                                                
    The push refers to repository [gcr.io/project-for-x/hello-world]
    428c97da766c: Layer already exists
    prod_latest: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524
    

    这在 GCR 中创建了一个 hello-world 文件

    General information
    Image type  Docker Manifest, Schema 2
    Media type  :application/vnd.docker.distribution.manifest.v2+json
    Virtual size    :977 B
    Uploaded time   :October 24, 2018 at 3:07:49 PM UTC+2
    Build ID    :—
    Container classification
    Digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 
    Tags:   dev_latest prod_latest stage_latest 
    Repository: hello-world
    Project:    project-for-x
    

    这样您就可以只使用一个yaml 文件来在不同的环境中部署相同的映像。

    【讨论】:

    • 我读过imagePullSecretsmyregistrykey,但我不完全确定它们会有所帮助。我看到在您的 Pod YAML 中有 image: gcr.io/&lt;registry_name&gt;/&lt;image_name&gt;:&lt;tagname&gt;。这看起来像一个完整的图像 URL。这是否意味着 Kubernetes 将始终从 gcr.io/&lt;registry_name&gt; 拉取而永远不会从 gcr.io/ 拉取?我还看到你有--docker-server=https://gcr.io/dev-bucket/commit-hash/something。这种语法真的有效吗?据我所知,这必须指向服务器 API 端点,不能指向某个存储桶或子目录。
    • 我已经检查过了,不需要在yaml 文件中提供地址(我已经编辑了我的答案),因为它已经在秘密中设置了。当您将图像推送到 GCR 或 Docker 时集线器,你需要有一个帐户。在 GCP 中,它是 Project ID,如果您将为每个 env 提供单独的项目 ID,那么它将按照您想要的方式工作。
    • 您作为示例提到的地址是无效的,它们在参考谷歌云注册表时永远不会存在,因为它们需要Project ID,并且没有子路径。
    • @Ark-kun 我已经编辑了答案并添加了如何向现有图像添加标签的示例,希望对您有所帮助。
    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2019-06-23
    • 2018-04-14
    • 2019-07-11
    • 2019-12-01
    • 1970-01-01
    相关资源
    最近更新 更多