更新时间:2024-01-16 GMT+08:00
分享

镜像地址替换

使用场景

用户在线下IDC有自建的harbor镜像仓库,同时也会将镜像同步到华为云SWR镜像仓库。创建负载后,希望调度到自建Kubernetes集群节点上运行的Pod使用自建镜像仓库的镜像,弹性到CCI的Pod使用SWR的镜像,以提高镜像拉取效率。可通过在Pod.Annotations中加入注解可以实现弹性到CCI的Pod中容器使用的镜像与自建Kubernetes集群使用的镜像为不同地址。

通过Annotation "coordinator.cci.io/image-replacement"配置镜像替换策略。其值为如下所示json字符串:

[
    {
        "repositoryPrefix":"harbor.domain/a/b/c/d",
        "replaceWith":"swr.cn-north-4.myhuaweicloud.com/org"
    }
]

相关字段说明

表1 相关字段说明

字段

说明

repositoryPrefix

不为空时表示需要被替换的镜像名前缀,即镜像名中最后一个"/"字符之前的部分。此时该字段字符校验规则与容器镜像名的规则一致,且不能以"/"字符结尾。该字段也可为空。

replaceWith

待替换字符串。该字段字符校验规则与容器镜像名的规则一致,且不能以"/"字符结尾。该值不能与repositoryPrefix相同。

匹配与替换规则

  • 替换策略可配置多条。各条替换策略的原字符串(repositoryPrefix)不允许重复。每个容器至多只会执行一条替换策略。配置替换策略无需考虑先后顺序,不同的配置顺序结果一致。
  • 原Pod.spec中配置的container和initContainer镜像均支持替换。
  • 若repositoryPrefix不为空,则匹配Pod中所有镜像名中最后一个"/"字符之前的字符串与该值相同的容器,对其最后一个"/"字符之前的部分替换为replaceWith的内容。
  • 若repositoryPrefix为空,则对Pod中镜像名不包含前缀(即没有"/"字符)的容器,镜像名前加上replaceWith的内容和一个"/"字符。

配置场景一

原Pod中所有容器镜像均使用同一镜像仓库与组织,配置一条替换策略即可。

示例:将所有容器镜像"harbor.domain/a/b/c/d"前缀都替换为SWR的镜像前缀"swr.cn-north-4.myhuaweicloud.com/org"。

替换策略:

"coordinator.cci.io/image-replacement": '[{"repositoryPrefix":"harbor.domain/a/b/c/d","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org"}]'

替换前:

      containers:
        - name: container-0
          image: 'harbor.domain/a/b/c/d/ubuntu:latest'
        - name: container-1
          image: 'harbor.domain/a/b/c/d/nginx:latest'

替换后:

      containers:
        - name: container-0
          image: 'swr.cn-north-4.myhuaweicloud.com/org/ubuntu:latest'
        - name: container-1
          image: 'swr.cn-north-4.myhuaweicloud.com/org/nginx:latest'

示例Deployment:

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: test-vk 
  labels: 
    virtual-kubelet.io/burst-to-cci: 'auto' 
spec: 
  replicas: 20 
  selector: 
    matchLabels: 
      app: test-vk 
  template: 
    metadata: 
      labels: 
        app: test-vk
      annotations: 
        coordinator.cci.io/image-replacement: '[{"repositoryPrefix":"harbor.domain/a/b/c/d","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org"}]'
    spec: 
      containers: 
      - name: container-0 
        image: harbor.domain/a/b/c/d/ubuntu:latest 
        resources: 
          limits: 
            cpu: 500m 
            memory: 1024Mi 
          requests: 
             cpu: 500m 
             memory: 1024Mi 
        command: 
          - /bin/bash 
          - '-c' 
          - sleep 10000 
      - name: container-1 
        image: harbor.domain/a/b/c/d/nginx:latest 
        resources: 
          limits: 
            cpu: 500m 
            memory: 1024Mi 
          requests: 
             cpu: 500m 
             memory: 1024Mi 
        command: 
          - /bin/bash 
          - '-c' 
          - sleep 10000

配置场景二

原Pod中容器使用不同的镜像仓库,可配置多条替换策略。

示例:

  1. 第一个容器匹配到第一条策略,将"harbor.domain"替换为"swr.cn-north-4.myhuaweicloud.com/org1"。
  2. 第二个容器匹配到第二条策略,将镜像名前加上"swr.cn-north-4.myhuaweicloud.com/org1"和"/"。
  3. 第三个容器匹配到第三条策略,将前缀"harbor.domain/a/b/c/d"替换为"swr.cn-north-4.myhuaweicloud.com/org2"。

    因为需要完整匹配最后一个"/"之前的部分,第一条策略即便有与该镜像相同的前缀"harbor.domain",也不会生效。

替换策略:

"coordinator.cci.io/image-replacement": '[{"repositoryPrefix":"harbor.domain","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org1"},{"repositoryPrefix":"","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org1"},{"repositoryPrefix":"harbor.domain/a/b/c/d","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org2"}]'

替换前:

      containers:
        - name: container-0
          image: 'harbor.domain/ubuntu:latest'
        - name: container-1
          image: 'nginx:latest'
        - name: container-2
          image: 'harbor.domain/a/b/c/d/redis:latest'

替换后:

      containers:
        - name: container-0
          image: 'swr.cn-north-4.myhuaweicloud.com/org1/ubuntu:latest'
        - name: container-1
          image: 'swr.cn-north-4.myhuaweicloud.com/org1/nginx:latest'
        - name: container-2
          image: 'swr.cn-north-4.myhuaweicloud.com/org2/redis:latest'

分享:

    相关文档

    相关产品