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

如上所示,我们首先需要准备一个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

此时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

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

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

GitHub 加速计划 / ha / harbor
23.24 K
4.67 K
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:1 个月前 )
c5d26723 chore(deps): bump github.com/go-openapi/runtime in /src Bumps [github.com/go-openapi/runtime](https://github.com/go-openapi/runtime) from 0.26.2 to 0.28.0. - [Release notes](https://github.com/go-openapi/runtime/releases) - [Commits](https://github.com/go-openapi/runtime/compare/v0.26.2...v0.28.0) --- updated-dependencies: - dependency-name: github.com/go-openapi/runtime dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: miner <yminer@vmware.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 11 天前
76624373 chore(deps): bump github.com/coreos/go-oidc/v3 in /src Bumps [github.com/coreos/go-oidc/v3](https://github.com/coreos/go-oidc) from 3.10.0 to 3.11.0. - [Release notes](https://github.com/coreos/go-oidc/releases) - [Commits](https://github.com/coreos/go-oidc/compare/v3.10.0...v3.11.0) --- updated-dependencies: - dependency-name: github.com/coreos/go-oidc/v3 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 11 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐