• 本地docker注册证书docker login连接到harbor仓库、利用shell脚本将大量镜像pull、tag、push到私有harbor仓库


    1. 本地docker注册证书docker login连接到harbor仓库:

    我们使用docker login/push/pull去与Harbor打交道,上传下载镜像等。 但是可能会出现x509: certificate signed by unknown authority之类的错误。

    [root@test01 harbor.dev]# docker login harbor.dev
    Authenticating with existing credentials…
    Login did not succeed, error: Error response from daemon: Get https://harbor.dev/v2/: x509: certificate signed by unknown authority
    Username (admin): admin
    Password:
    Error response from daemon: Get https://harbor.dev/v2/: x509: certificate signed by unknown authority

    此时根据docker官方https://docs.docker.com/engine/security/certificates/,客户端要使用tls与Harbor通信,使用的还是自签证书,那么必须建立一个目录:/etc/docker/certs.d,在这个目录下建立签名的域名的目录, 那么整个目录为: /etc/docker/certs.d/xxxx(harbor域名), 然后把harbor的证书ca.crt等拷贝到这个目录即可。
    在这里插入图片描述

    2.使用shell脚本将大量镜像pull、tag、push到私有harbor仓库

    #!/bin/bash
    
    while IFS= read -r line; do
      image_name=$(echo "$line" | awk '{print $1}')
    
      new_name=$(echo "$line" | awk '{print $2}' | tr -d '\r')
    
      docker pull "$image_name"
      docker tag "$image_name" "$new_name"
      docker push "$new_name"
    
      echo "Pulled, tagged, and pushed $image_name as $new_name"
    done < images.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    如上所示,我们首先需要准备一个images.txt里面包括了所有下载镜像名及上传名,如下所示,每行包括image_name与new_name,其中用空格隔开

    istio/proxyv2:1.14.1 harbor.dev/cube_studio/istio_proxyv2:1.14.1
    istio/pilot:1.14.1 harbor.dev/cube_studio/pilot:1.14.1
    mysql:5.7 harbor.dev/cube_studio/

    当然,以上脚本的关键在于image_name与new_name的获取,若new_name可根据image_name修改得到,也可以更改脚本得到适合自己的new_name,可根据自己需要进行修改。

      #images.txt每行只有image_name,tr -d '\r'去掉换行符
      image_name=$(echo "$line" | awk '{print $1}' | tr -d '\r')
      
      #根据情况给出new_name的名称,可根据自己需要进行修改
      if [[ "$image_name" == *"ccr.ccs.tencentyun.com"* ]]; then
      #image_name中的ccr.ccs.tencentyun.com/hub替换为harbor.dev/hub
        new_name=$(echo "$image_name" | sed 's/ccr.ccs.tencentyun.com/harbor.dev\/hub//g')
      else
       #在image_name前加上harbor.dev/hub/
        new_name="harbor.dev/hub/$image_name"
      fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    此时images.txt每行只需要包括需要上传的镜像名即可,new_name可根据脚本得到,如下所示:

    istio/proxyv2:1.14.1
    istio/pilot:1.14.1
    mysql:5.7

    将Excel表格中的数据粘贴到记事本中,且每列间距都为一个空格

    可在EXECL中将你需要的所有数据先合并成一列再导出。

    • 假定你原数据有AB两列,在C1单元格输入公式:=A1&" "&B1,将公式用填充柄向下复制,之后将C列粘贴到txt文档即每列间距都为一个空格

    3. 判断仓库中是否已经存在镜像,若不存在则上传

    采用上述脚本后有时会出现有些镜像已经上传过重复pull的情况,会验证影响效率,因此进行改进,结果如下所示:

    #!/bin/bash
    
    while read -r line; do
      image_name=$(echo "$line" | awk '{print $1}' | tr -d '\r')
      #根据情况给出new_name的名称,可根据自己需要进行修改
      if [[ "$image_name" == *"ccr.ccs.tencentyun.com"* ]]; then
        new_name=$(echo "$image_name" | sed 's/ccr.ccs.tencentyun.com/harbor.dev\/hub//g')
      else
        new_name="harbor.dev/hub/$image_name"
      fi
      
      #清除output.txt
      echo -n "" > output.txt
      
      result=$(timeout 8s docker pull "$new_name" 2>&1 | tee output.txt)
      #若输出Error response from daemon说明harbor仓库中不存在镜像,则直接上传
      if grep -q "Error response from daemon" output.txt; then
        echo "-------------------暂无该镜像-------------$new_name"
        #echo $image_name >> push_images.txt
        #echo $new_name >> push_newimages.txt
        
        docker pull "$image_name"
        docker tag "$image_name" "$new_name"
        docker push "$new_name"
        echo "Pulled, tagged, and pushed $image_name as $new_name"
        echo -n "" > output.txt
        result=$(timeout 15s docker pull "$new_name" 2>&1 | tee output.txt)
        if grep -q "Pulling from" output.txt; then
          echo "$new_name"
          echo "$image_name $new_name" >> exist_images.txt
        fi
      #若输出Pulling from说明harbor仓库中已存在该镜像,则写入exist_images.txt
      elif grep -q "Pulling from" output.txt; then
        echo "$new_name"
        echo "$image_name $new_name" >> exist_images.txt
      else
        echo "***************docker pull 时间过短***************$new_name"
        echo $new_name >> timeout_images.txt
      fi
    done < push_images.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    4. 镜像上传后,利用find+sed脚本修改项目源代码中的镜像地址

    #!/bin/bash  
      
    while IFS= read -r line; do  
      image_name=$(echo "$line" | awk '{print $1}')  
      new_name=$(echo "$line" | awk '{print $2}' | tr -d '\r')  
      
      # 定义多个项目文件夹路径,使用空格分隔  
      project_folders="D:\Gitlab\cubestudio-apps\docs D:\Gitlab\cubestudio-apps\install D:\Gitlab\cubestudio-apps\myapp"  
      
      # 遍历项目文件夹路径  
      for project_folder in $project_folders; do  
        find "$project_folder" -type f \( -name "*.yml" -o -name "*.yaml" -o -name "*.json" -o -name "*.sh" -o -name "*.md" -o -name "*.py" -o -name "*ockerfile*" \) -print0 | xargs -0 grep -l "$image_name" | while read -r file; do
    
        # 替换文件内容
        sed -i "s@$image_name@$new_name@g" "$file"
        echo "在文件 $file 中将 $image_name 替换为 $new_name "
        unix2dos "$file"
       done ;
      done;
    done < images1.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    文件应该包含两列数据,第一列是旧的镜像名称,第二列是新的镜像名称,并且每行数据之间使用空格分隔。

  • 相关阅读:
    边缘人工智能——nanodet模型实践指引,从标注数据集到实现部署文件
    Codeforces Round 958 (Div. 2)[部分题解ABC]
    (附源码)springboot手工diy网站 毕业设计 310226
    [面试] k8s面试题 2
    aliyunoss上传图片
    Stream流数组和对象List排序
    linux 网络图标消失的解决办法
    基于Qt的yolov5工程
    HTML进阶(3)- 表单元素
    visual studio 2017创建Cmake项目,并修改默认工作目录
  • 原文地址:https://blog.csdn.net/qq_45808700/article/details/132852079