Kubernetes Podspec 仅下载容器镜像但不应安装

Kubernetes Podspec to download only container image but it should not install

我想下载容器镜像,但不想 deploy/install 镜像。 我如何部署 podspec 以仅下载图像但它不应该创建容器。 任何 podspec 快照吗?

据我所知,没有直接的 Kubernetes 资源可以只能下载您选择的图像。要在 Nodes 上显示您的应用程序图像,您可以考虑使用以下 solutions/workarounds:

  • 使用 DaemonsetinitContainer('s)
  • 使用 Ansible 等工具通过 playbook 拉取镜像

使用 DaemonsetInitContainers

假设情况如下:

您已经创建了 2 张图片,您希望在所有 Nodes 上使用。

您可以使用 Daemonset(在每个 Node 上生成 Pod)和 initContainers(以图像为源),这将 运行所有节点并确保图像将出现在机器上。

此类设置的示例如下:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: pull-images
  labels:
    k8s-app: pull-images
spec:
  # AS THIS DAEMONSET IS NOT SUPPOSED TO SERVE TRAFFIC I WOULD CONSIDER USING THIS UPDATE STRATEGY FOR SPEEDING UP THE DOWNLOAD PROCESS
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 100
  selector:
    matchLabels:
      name: pull-images
  template:
    metadata:
      labels:
        name: pull-images
    spec:
      initContainers:
      # PUT HERE IMAGES THAT YOU WANT TO PULL AND OVERRIDE THEIR ENTRYPOINT
      -  name: ubuntu
         image: ubuntu:20.04 # <-- IMAGE #1
         imagePullPolicy: Always # SPECIFY THE POLICY FOR SPECIFIC IMAGE
         command: ["/bin/sh", "-c", "exit 0"]
      -  name: nginx
         image: nginx:1.19.10 # <-- IMAGE #2 
         imagePullPolicy: IfNotPresent # SPECIFY THE POLICY FOR SPECIFIC IMAGE
         command: ["/bin/sh", "-c", "exit 0"]
      containers:
      # MAIN CONTAINER WITH AS SMALL AS POSSIBLE IMAGE SLEEPING
      - name: alpine
        image: alpine 
        command: [sleep] 
        args: 
        - "infinity" 

Kubernetes Daemonset 控制器将确保 Pod 将在每个 Node 上 运行。在图像 运行 之前, initContainers 将充当图像的占位符。您想要在 Nodes 上的图像将被拉取。 ENTRYPOINT 将被覆盖为不连续 运行 图像。之后主容器 (alpine) 将是 运行 和 sleep infinity 命令。

添加新 Nodes 后,此设置也将起作用。

根据该主题,我还会考虑查看有关 imagePullPolicy 的以下文档:

A side note!

I've set the imagePullPolicy for the images in initContainers differently to show you that you can specify the imagePullPolicy independently for each container. Please use the policy that suits your use case the most.


使用 Ansible 等工具通过 playbook 拉取镜像

假设您有 SSH 访问 Nodes 的权限,您可以考虑将 Ansible 与它的社区模块一起使用(假设您正在使用 Docker):

  • community.docker.docker_image

引用此模块的文档:

This plugin is part of the community.docker collection (version 1.3.0).

To install it use: ansible-galaxy collection install community.docker.

Synopsis

Build, load or pull an image, making the image available for creating containers. Also supports tagging an image into a repository and archiving an image to a .tar file.

-- Docs.ansible.com: Ansible: Collections: Community: Docker: Docker image module

您可以通过以下示例使用它:

  • hosts.yaml
all:
  hosts:
    node-1:
      ansible_port: 22
      ansible_host: X.Y.Z.Q
    node-2: 
      ansible_port: 22
      ansible_host: A.B.C.D
  • playbook.yaml
- name: Playbook to download images
  hosts: all
  user: ENTER_USER

  tasks:
  - name: Pull an image
    community.docker.docker_image:
      name: "{{ item }}"
      source: pull
    with_items:
    - "nginx"
    - "ubuntu"

A side note!

In the ansible way, I needed to install docker python package:

$ pip3 install docker


其他资源: