更新时间:2024-05-23 GMT+08:00

镜像

简介

用户可以通过华为云镜像仓库服务SWR或者第三方镜像仓库来管理业务镜像。本章节将介绍CCE+bursting插件场景中,涉及到镜像相关的使用场景及用法。通过阅读本章用户可以在CCE+bursting插件场景中:

使用SWR拉取用户业务镜像

  • 方式一:通过console选取SWR镜像
    1. 用户镜像上传SWR。使用方式请参考:SWR官方文档
    1. 在华为云CCE控制台创建负载选择镜像。

    2. 对接到SWR中的镜像。请确保您的SWR仓库中已正确上传了镜像,SWR服务使用详情请参见:SWR官方文档
  • 方式二:通过在CCE集群node选取SWR镜像
    1. 登录CCE集群节点。
    2. 查看SWR镜像仓库中的镜像地址。

      获取镜像地址:swr.cn-north-7.myhuaweicloud.com/cci-test/nginx:1.0.0.x86_64_test。

    3. 配置工作负载yaml。
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: test
        namespace: default
        labels:
          virtual-kubelet.io/burst-to-cci: 'auto'    # 弹性到CCI
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: test
        template:
          metadata:
            labels:
              app: test
          spec:
            containers:
              - image: swr.cn-north-7.myhuaweicloud.com/cci-test/nginx:1.0.0.x86_64_test
                name: container-0
                resources:
                  requests:
                    cpu: 250m
                    memory: 512Mi
                  limits:
                    cpu: 250m
                    memory: 512Mi
                volumeMounts: []
            imagePullSecrets:
              - name: default-secret
    4. 部署工作负载。
      kubectl apply -f  dep.yaml

使用第三方镜像拉取用户业务镜像

  1. 使用CCI提供的工具创建第三方镜像仓库认证secret。
    imagepull-secret-generator --ak=$ak --sk=$sk --private-user=$user --private-password=$password --output-file-path=my-imagepull-secret.json --project-name=cn-north-4 --secret-name=my-imagepull-secret --swr-address=swr.cn-north-4.myhuaweicloud.com
    登录CCE集群节点,为集群创建secret。
    kubectl apply -f my-imagepull-secret.json
  2. 创建负载,使用secret。

    在spec.imagePullSecrets中指定第三方仓库认证secret。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: test-imagepull
        virtual-kubelet.io/burst-to-cci: enforce
      name: test-imagepull
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: test-imagepull
      template:
        metadata:
          labels:
            app: test-imagepull
        spec:
          containers:
          - image: xxx/my-image:latest
            imagePullPolicy: Always
            name: nginx
            resources:
              limits:
                cpu: 1
                memory: 2Gi
              requests:
                cpu: 1
                memory: 2Gi
          imagePullSecrets:
          - name: my-imagepull-secret

混合使用华为云SWR + 第三方镜像仓拉取用户业务镜像

操作场景:

在某些情况下,用户在CCE的工作负载拉取第三方镜像仓库的镜像,拉取的镜像可以同步到SWR服务,当发生业务波峰时,弹性到CCI的工作负载使用SWR镜像,可以极大提高镜像拉取效率,帮助用户更丝滑应对业务波峰。

操作指导

为配置工作负载yaml配置annotation,示例值如下所示:

"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"}
]'
  • 配置策略执行无先后顺序。
  • 替换策略可以配置多条,各个策略repositoryPrefix值不允许重复。

替换规则key

字段含义

字段说明

repositoryPrefix

用户希望被匹配并被替换的镜像前缀字段。

  • 该字段为空,匹配所有image字段不含"/"的容器。
  • 该字段不为空,匹配所有image字段最后一个"/"字符前缀相同的容器。
  • 该字段字符校验规则与容器镜像名的规则一致,且不能以"/"字符结尾。

replaceWith

用户希望替换成的镜像前缀字段。

  • 该字段字符不能与repositoryPrefix相同
  • 该字段字符校验规则与容器镜像名的规则一致,且不能以"/"字符结尾
表1 annotation说明

annotation

替换前

替换后

说明

"coordinator.cci.io/image-replacement": '[

{"repositoryPrefix":"harbor.domain","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org1"}

]'

containers:

- name: container-0

image: 'harbor.domain/ubuntu:latest'

containers:

- name: container-0

image: 'swr.cn-north-4.myhuaweicloud.com/org1/ubuntu:latest'

repositoryPrefix匹配第三方仓库域名场景。

"coordinator.cci.io/image-replacement": '[

{"repositoryPrefix":"","replaceWith":"swr.cn-north-4.myhuaweicloud.com/org1"}

]'

containers:

- name: container-1

image: 'nginx:latest'

containers:

- name: container-1

image: 'swr.cn-north-4.myhuaweicloud.com/org1/nginx:latest'

repositoryPrefix为空场景。

"coordinator.cci.io/image-replacement": '[

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

]'

containers:

- name: container-2

image: 'harbor.domain/a/b/c/d/redis:latest'

containers:

- name: container-2

image: 'swr.cn-north-4.myhuaweicloud.com/org2/redis:latest'

repositoryPrefix匹配第三方仓库域名+组织目录场景。