
# 镜像
用户可以通过华为云镜像仓库服务SWR来管理业务镜像。本章节将介绍CCE+bursting插件场景中，涉及到镜像相关的使用场景及用法。通过阅读本章用户可以在CCE+bursting插件场景中：
- [使用SWR拉取用户业务镜像]
- [混合使用华为云SWR + 第三方镜像仓拉取用户业务镜像]
 #### 使用SWR拉取用户业务镜像
- **方式一：通过console选取SWR镜像**
  1. 用户镜像上传SWR。使用方式请参考：[SWR官方文档](https://support.huaweicloud.com/swr/index.html)。
  
  
  
  2. 在华为云CCE控制台创建负载选择镜像。 ![](https://support.huaweicloud.com/usermanual-cci2/zh-cn_image_0000002205288940.png "点击放大")
     
  
  3. 对接到SWR中的镜像。请确保您的SWR仓库中已正确上传了镜像，SWR服务使用详情请参见：[SWR官方文档](https://support.huaweicloud.com/swr/index.html)。
   
- **方式二：通过在CCE集群配置node选取SWR镜像**
  1. 查看SWR镜像仓库中的镜像地址，示例：swr.cn-north-4.myhuaweicloud.com/cci-test/nginx:1.0.0.x86_64_test 其中region为cn-north-4，cci-test为swr的组织。
     
  
  2. 登录CCE集群节点。
  
  3. 配置工作负载yaml。
     ```
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: test
       namespace: default
       labels:
         bursting.cci.io/burst-to-cci: enforce    # 弹性到CCI 2.0
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: test
       template:
         metadata:
           labels:
             app: test
         spec:
           containers:
             - image: swr.***.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  test-deployment.yaml
     ```
     
   
 
 #### 混合使用华为云SWR + 第三方镜像仓拉取用户业务镜像
**操作场景：**
在某些情况下，用户在CCE的工作负载拉取第三方镜像仓库的镜像，拉取的镜像可以同步到SWR服务，当发生业务波峰时，弹性到CCI的工作负载使用SWR镜像，可以极大提高镜像拉取效率，帮助用户更丝滑应对业务波峰。
**操作指导**：
为工作负载yaml配置annotation，示例值如下所示：
```
bursting.cci.io/image-replacement: '[
    {"repositoryPrefix":"harbor.domain","replaceWith":"swr.***.com/org1"},
    {"repositoryPrefix":"","replaceWith":"swr.***.com/org1"},
    {"repositoryPrefix":"harbor.domain/a/b/c/d","replaceWith":"swr.***.com/org2"}
]'
```
![](https://support.huaweicloud.com/usermanual-cci2/public_sys-resources/note_3.0-zh-cn.png)
- 配置策略执行无先后顺序。
- 替换策略可以配置多条，各个策略repositoryPrefix值不允许重复。
 
表1imageReplacement参数说明 
| 替换规则key          | 字段含义                | 字段说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|
| repositoryPrefix | 用户希望被匹配并被替换的镜像前缀字段。 | - 该字段为空字符串("")，匹配所有image字段不含"/"的容器。  - 该字段不为空，匹配所有image字段最后一个"/"字符前缀相同的容器。  - 该字段字符校验规则与容器镜像名的规则一致，且不能以"/"字符结尾。   |
| replaceWith      | 用户希望替换成的镜像前缀字段。     | - 该字段字符不能与repositoryPrefix相同。  - 该字段字符校验规则与容器镜像名的规则一致，且不能以"/"字符结尾。                                                                                                                                                                           |
   
表2annotation说明 
| annotation                                                                                                                                                                                                                                         | 替换前                                                                                                                                                                                                 | 替换后                                                                                                                                                                                               | 说明                                |
|:---|:---|:---|:---|
| bursting.cci.io/image-replacement: '\[ {"repositoryPrefix":"harbor.domain","replaceWith":"swr.\*\*\*.com/org1"} \]'         | containers: - name: container-0 image: 'harbor.domain/ubuntu:latest'           | containers: - name: container-0 image: 'swr.\*\*\*.com/org1/ubuntu:latest' | repositoryPrefix匹配第三方仓库域名场景。      |
| bursting.cci.io/image-replacement: '\[ {"repositoryPrefix":"","replaceWith":"swr.\*\*\*.com/org1"} \]'                      | containers: - name: container-1 image: 'nginx:latest'                      | containers: - name: container-1 image: 'swr.\*\*\*.com/org1/nginx:latest' | repositoryPrefix为空场景。             |
| bursting.cci.io/image-replacement: '\[ {"repositoryPrefix":"harbor.domain/a/b/c/d","replaceWith":"swr.\*\*\*.com/org2"} \]' | containers: - name: container-2 image: 'harbor.domain/a/b/c/d/redis:latest' | containers: - name: container-2 image: 'swr.\*\*\*.com/org2/redis:latest'    | repositoryPrefix匹配第三方仓库域名+组织目录场景。 |
   
