harbor下镜像tag查询和镜像批量推送

mac2024-05-15  28

镜像查询

harbor接口说明

接口请求方式接口作用修改方/api/projectsPOST创建镜像仓库Java后台/api/projects/quotaListGET查看Project配额Harbor后台/api/projects/{projectId}/quotaPOST更新project配额Harbor后台/api/repositories/manifestsGET获取manifestsJava后台/api/repositories/getClairStatistcsGET展示漏洞扫描结果Harbor后台/api/projects/{projectId}/logs/filterPOST获取日志信息Java后台api/repositories/copyImagePOST复制镜像(同harbor)Harbor后台

查询repository_name

通过search接口查询 /api/search? 查询统配查询所有repositry_name 自动化查询工具编写(该脚本会遍历镜像仓库所有的项目) USER="" PASS="" HURL="https://x.x.x.x:x" curl -k -u "$USER:$PASS" -X GET -H "Content-Type: application/json" "$HURL/api/search?"|grep repository_name|awk -F 2>&1 > images-name.log #user:镜像仓库登录名 #pass:镜像仓库登录密码 #hurl:镜像仓库 #curl通过查询接口将查询repository_name数据重定向到当前目录下images-name

镜像仓库镜像名字和tag获取

查询repositry_name对应的repositories_tag

自动化查询工具编写 USER="" PASS="" HURL="https://x.x.x.x:x" URL="x.x.x.x:x" cat $bashpath/images-name |while read line;do curl -k -u "$USER:$PASS" -X GET -H "Content-Type: application/json" "$HURL/api/repositories/tags?repo_name=$line" 2>&1 |grep tag|awk -F "\"" '{print $4}'|while read tag;do echo $URL/$line:$tag;done;done > images-result #user:镜像仓库登录名 #pass:镜像仓库登录密码 #hurl:镜像仓库 #url: 镜像仓库拼接地址 #curl通过查询接口定向的images-name文件进行轮询获取repositry_name对应的repositories_tag并进行拼接输出到images-result #images-result下为镜像仓库内所有的镜像和tag

镜像批量恢复推送

自动化查询工具编写 USER="" PASS="" URL="x.x.x.x:x" docker login $URL -u $USER -p $PASS sed -n "1,50p" $bashpath/images-result |while read line;do docker push $line;done #user:镜像仓库登录名 #pass:镜像仓库登录密码 #url: 镜像仓库拼接地址 #通过循环将镜像仓库内导出的images-result在所有工作节点上进行推送,若有重复推送的镜像,镜像仓库只查询后不再进行推送,故不会对相同节点重复进行进行二次推送。 #可变量值【sed -n "1,50p" 】

因导出images-result有大量的镜像,为了防止批量大量推送镜像造成镜像 仓库负载高原因,可修改此值,每次批量推送images-result中50个镜像, 下一次变量值【sed -n “51,100p” 】

脚本执行

1.准备所有工作节点上ip地址编写host文件 2.将上述镜像批量推送脚本和images-result文件分发到各个工作节点中

ansible -i hosts all -m copy -a "src=/opt/software/push_images dest=/opt/software" -u gyt -s -k -K

3.修改每次变量值【sed -n “起始,结束p” 】后进行每个节点上的多次分批量进行推送

ansible -i hosts all -m shell -a "bash /opt/software/push_images/push_images.sh" -u gyt -s -k -K

验证推送结果

在一台没有镜像的机器上进行images-result内的镜像批量拉取来验证所有镜像是否推送到镜像仓库中

cat images-result |while read line;do docker pull $line;done

备注:下拉镜像后清理没用的镜像

docker system prue -a
最新回复(0)