harbar镜像同步

摘要:
1、 完成图像文件1。编辑脚本pull sh#!/bin/bashif[$#-ne1];theecho“用法:$0filename”exit1fifile=$1if[!-f$file];Thenecho“the$fileisnotafile”exit2ficount=0helreadline#使用read命令循环读取文件内容,并将读取的文件内容分配给变量linedoline1=${l

 一、做好镜像文件

1、编辑脚本 pull.sh

#!/bin/bash
if [ $# -ne 1 ];then
  echo "Usage:$0 filename"
  exit 1
fi
file=$1
if [ ! -f $file ];then
  echo "the $file is not a file"
  exit 2
fi
count=0
while read line      #使用read命令循环读取文件内容,并将读取的文件内容赋值给变量line
do
   line1=${line:16}
   line2=${line1///_}
   line2=${line2/:/_}
   let count++
   echo "now pull $count:   $line "  >> replicat.log
   docker pull $line >/dev/null
   if [ $? == 0 ];then
     echo "now tag $count: $line " >> replicat.log
     zyd_tag=${line/harbor/harbor-zyd1}
     docker tag $line $zyd_tag
     docker save -o $line2.tar $zyd_tag >/dev/null
     echo "now gzip $count:     $line2" >> replicat.log
     echo "now tar "
     tar -czvf $line2.tar.gz $line2.tar >/dev/null
     rm -f $line2.tar
     echo "$count: $line  successed"  >> success.log
     docker rmi $line  > /dev/null
     docker rmi $zyd_tag > /dev/null
   else
     echo "pull failed $count: $line"     >> failed.log
   fi
done <$file           #“done <$file”将整个while循环的标准输入指向文件$file
echo -e "
totle $count lines read"  >> replicat.log

exit 0

 拉取的镜像列表

[root@ops0002 zyd1230]# cat list3
harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005

执行操作  nohup sh pull.sh list3 &

执行过程如下

读取一个文件列表,按行执行操作
过程如下
docker pull harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005   拉取镜像
docker tag harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005  harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005   镜像打标记
docker save -o enos_dataexplorer_tag_dataexplore_20191227_005.tar harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005     将镜像保存为 gz文件
tar -czvf enos_dataexplorer_tag_dataexplore_20191227_005.tar.gz   enos_dataexplorer_tag_dataexplore_20191227_005.tar  将镜像文件压缩为 tar.gz文件
rm -f enos_dataexplorer_tag_dataexplore_20191227_005.tarr 删除文件
docker rmi harbor.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005   删除拉取的镜像
docker rmi harbor-zyd1.eniot.io/enos/dataexplorer:tag_dataexplore_20191227_005    删除打标机的镜像
--------------------------------
最后留下的文件为 enos_dataexplorer_tag_dataexplore_20191227_005.tar.gz

 2、下载文件

nginx配置下载

server {
    listen       8080;
    server_name  localhost;
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
    charset utf-8,gbk;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /data/images;
        #index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

harbar镜像同步第1张

3)以上功能使用 python脚本实现。

[root@harbor0001 images]# cat pull_image.py 
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os,sys

class MyError(BaseException):
    def __init__(self,msg):
        super().__init__()
        self.msg=msg
    def __str__(self):
        return '<%s>' %self.msg


class Images:
    def cmd(self,comand):
        ret = os.system(comand)
        if ret != 0:
            with open('failure.txt', mode='a') as f:
                f.write(comand)
                f.write('
')
            raise MyError('command error')

    def pull(self,image):
        '''拉取镜像'''
        comand = "docker pull %s" % image
        self.cmd(comand)

    def tag(self,source_image,desc_image):
        '''标记镜像'''
        comand = "docker tag %s %s" %(source_image,desc_image)
        self.cmd(comand)


    def save(self,image_tar,desc_image,image_tar_gz):
        '''保存镜像'''
        comand = "docker save -o %s %s" %(image_tar,desc_image)
        self.cmd(comand)
        comand2 = "tar -czvf %s %s" %(image_tar_gz,image_tar)
        self.cmd(comand2)
        comand3 = "rm -f %s" % image_tar
        self.cmd(comand3)

    def delete_image(self,source_image,desc_image):
        comand1 = "docker rmi %s" %source_image
        comand2 = "docker rmi %s" %desc_image
        self.cmd(comand1)
        self.cmd(comand2)


if len(sys.argv) == 2:
    file = sys.argv[1]
    image = Images()
    with open(file,mode='r') as f:
        for line in f:
            image_error = line.split('/')
            image_error[-1] = image_error[-1].split('
')[0]
            source_image = '/'.join(image_error)
            image_error[0] = 'harbor-test2.eniot.io'
            desc_image = '/'.join(image_error)
            image_tar = '_'.join(image_error)
            image_tar = image_tar.split(':')
            image_tar = '_'.join(image_tar) + ".tar"
            image_tar_gz = image_tar + ".gz"
            # print(image_tar)
            # print(image_tar_gz)
            # print(source_image)
            # print(desc_image)
            image.pull(source_image)
            image.tag(source_image, desc_image)
            image.save(image_tar, desc_image, image_tar_gz)
            image.delete_image(source_image, desc_image)
            with open('success.txt', mode='a') as f:
                f.write(line)


else:
    print('input image file')

nohup python pull_image.py list  &

二、推送镜像1

[root@harbor0001 ~]# cat a.sh
#!/bin/bash
docker pull harbor-cn2.eniot.io/${1}
docker tag harbor-cn2.eniot.io/${1}  harbor-zyd1.eniot.io/${1}
docker push  harbor-zyd1.eniot.io/${1}

三、推送镜像2

[root@harbor0001 images]# cat pull.sh 
#!/bin/bash
if [ $# -ne 1 ];then
  echo "Usage:$0 filename"
  exit 1
fi
file=$1
if [ ! -f $file ];then
  echo "the $file is not a file"
  exit 2
fi
count=0
while read line      #使用read命令循环读取文件内容,并将读取的文件内容赋值给变量line
do
   let count++
   echo "now pull $count:   $line "  >> replicat.log
   docker pull $line >/dev/null
   if [ $? == 0 ];then
 
     echo "now tag $count: $line " >> replicat.log
     india_tag=${line/cn2/india1}
     docker tag $line $india_tag
     echo "    now push $india_tag" >> replicat.log
     docker push $india_tag
     if [ $? == 0 ]; then
         echo "------$count: $line  successed"  >> success.log
         echo " push $india_tag successed"  >> replicat.log
         docker rmi $line  > /dev/null
         docker rmi $india_tag > /dev/null
     else
         echo " push $india_tag  failed" >> replicat.log
         echo " $count :    push $india_tag  failed" >> failed.log
     fi
   else
     echo "pull failed $count: $line"     >> failed.log
   fi
done <$file           #“done <$file”将整个while循环的标准输入指向文件$file
echo -e "
totle $count lines read"  >> replicat.log

exit 0

2)该脚本内容用python已经改写

[root@harbor0001 files]# cat push_image.py 
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os,sys

class MyError(BaseException):
    def __init__(self,msg):
        super().__init__()
        self.msg=msg
    def __str__(self):
        return '<%s>' %self.msg


class Images:
    def cmd(self,comand):
        ret = os.system(comand)
        if ret != 0:
            with open('failure.txt', mode='a') as f:
                f.write(comand)
                f.write('
')
            raise MyError('command error')


    def load(self,image_tar_gz,image):
        '''加载镜像'''
        image_tar = image_tar_gz.split(".gz")[0]
        comand1 = "tar -xf %s" %(image_tar_gz)
        self.cmd(comand1)
        comand2 = "rm -f %s" % image_tar_gz
        self.cmd(comand2)
        comand3 = "docker load -i %s" % image_tar
        self.cmd(comand3)
        comand4 = "docker push %s" % image
        self.cmd(comand4)
        comand5 = "docker rmi %s" % image
        self.cmd(comand5)
        comand6 = "rm -f %s" % image_tar
        self.cmd(comand6)


    def main(self):
        list_files = os.listdir('.')
        for image_tar_gz in list_files:
            if image_tar_gz.endswith('tar.gz'):
                new_image = image_tar_gz.split(".tar.gz")[0].split("_")
                if len(new_image) == 4:
                    image = new_image[0] + '/' + new_image[1] + "/" + new_image[2] + ':' + new_image[3]
                    self.load(image_tar_gz,image)
                    with open('success.txt', mode='a') as f:
                        f.write(image)
                        f.write("
")


if __name__ == '__main__':
    image = Images()
    image.main()

 注意上面镜像 bug 。标记的镜像 不能包含 “_”。以及下载的时候,就已经做了标记

三、镜像标记为: tag_nginx_20200227_005  ,有且只有 一个 tag 时。推送时,再给镜像地址

下载镜像

harbar镜像同步第2张harbar镜像同步第3张
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os,sys

class MyError(BaseException):
    def __init__(self,msg):
        super().__init__()
        self.msg=msg
    def __str__(self):
        return '<%s>' %self.msg


class Images:
    def cmd(self,comand):
        ret = os.system(comand)
        if ret != 0:
            with open('failure.txt', mode='a') as f:
                f.write(comand)
                f.write('
')
            raise MyError('command error')

    def pull(self,image):
        '''拉取镜像'''
        comand = "docker pull %s" % image
        self.cmd(comand)

    def save(self,image_tar,desc_image,image_tar_gz):
        '''保存镜像'''
        comand = "docker save -o %s %s" %(image_tar,desc_image)
        self.cmd(comand)
        comand2 = "tar -czvf %s %s" %(image_tar_gz,image_tar)
        self.cmd(comand2)
        comand3 = "rm -f %s" % image_tar
        self.cmd(comand3)

    def delete_image(self,source_image):
        comand = "docker rmi %s" %source_image
        self.cmd(comand)


if len(sys.argv) == 2:
    file = sys.argv[1]
    image = Images()
    with open(file,mode='r') as f:
        for line in f:
            image_error = line.split('/')
            if image_error[0] != 'harbor-test1.eniot.io':
                continue
            image_error[-1] = image_error[-1].split('
')[0]
            source_image = '/'.join(image_error)
            desc_image = '/'.join(image_error)
            image_tar = '_'.join(image_error)
            image_tar = image_tar.split(':')
            image_tar = '_'.join(image_tar) + ".tar"
            image_tar_gz = image_tar + ".gz"
            image.pull(source_image)
            image.save(image_tar,source_image,image_tar_gz)
            image.delete_image(source_image)
            with open('success.txt', mode='a') as f:
                f.write(line)

else:
    print('input image file')
pull_images.py

推送镜像

harbar镜像同步第2张harbar镜像同步第3张
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os,sys,time

class MyError(BaseException):
    def __init__(self,msg):
        super().__init__()
        self.msg=msg
    def __str__(self):
        return '<%s>' %self.msg


class Images:
    def cmd(self,comand):
        ret = os.system(comand)
        if ret != 0:
            with open('failure.txt', mode='a') as f:
                f.write(comand)
                f.write('
')
            raise MyError('command error')


    def load(self,image_tar_gz,source_image,desc_image):
        '''加载镜像'''
        image_tar = image_tar_gz.split(".gz")[0]
        comand1 = "tar -xf %s" %(image_tar_gz)
        self.cmd(comand1)
        comand2 = "rm -f %s" % image_tar_gz
        self.cmd(comand2)
        comand3 = "docker load -i %s" % image_tar
        self.cmd(comand3)
        comand4 = "docker tag %s %s" %(source_image,desc_image)
        self.cmd(comand4)
        comand5 = "docker push %s" % desc_image
        self.cmd(comand5)
        comand6 = "docker rmi %s" % source_image
        comand7 = "docker rmi %s" % desc_image
        self.cmd(comand6)
        self.cmd(comand7)
        comand8 = "rm -f %s" % image_tar
        self.cmd(comand8)


    def main(self):
        list_files = os.listdir('.')
        for image_tar_gz in list_files:
            if image_tar_gz.endswith('tar.gz'):
                new_image = image_tar_gz.split(".tar.gz")[0]
                if new_image.count("tag") == 1:
                    new_image = new_image.split("_tag_")
                    harbor_adress = "harbor-test2.eniot.io"
                    app = new_image[0].split("_")
                    source_image = app[0] + "/" + app[1] + "/" + app[2] + ":tag_" + new_image[1]
                    desc_image = harbor_adress + "/" + app[1] + "/" + app[2] + ":tag_" + new_image[1]
                    self.load(image_tar_gz,source_image,desc_image)
                    with open('success.txt', mode='a') as f:
                        f.write(desc_image)
                        f.write("
")

if __name__ == '__main__':
    image = Images()
    image.main()
push_images.py

免责声明:文章转载自《harbar镜像同步》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇k8s污点Taints与容忍详解TolerationSQLAlchemy技术文档(中文版)-下下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Docker学习のWindows下安装Docker

一、docker最初只支持linux的,因此在windows下运行需要虚拟机。 利用VirtualBox建立linux虚拟机,在linux虚拟机中安装docker服务端和客户端 利用Windows的Hyper-v虚拟化技术,直接在Windows上安装docker服务端和客户端。(在windows10和windows server2016) WIndow...

docker 的安装与卸载

Docker 理解为应用市场管家 核心简化运维安装软件的发杂环境 Docker daemon( Docker守护进程) Docker daemon是一个运行在宿主机( DOCKER-HOST)的后台进程。可通过 Docker客户端与之通信。 Client( Docker客户端) Docker客户端是 Docker的用户界面,它可以接受用户命令和配置标识,并...

Docker 容器(container)及资源限制

Container: 既然container是由image运行起来的,那么是否可以理解为container和image有某种关系?先来看张图: 其实可以理解为container只是基于image之后的layer而已,也就是可以通过docker run image创建出一个container出来。 底层技术支持: Container是一种轻量级的虚拟化技术...

win10安装docker步骤

一、启用Hyper-V  打开控制面板 - 程序和功能 - 启用或关闭Windows功能,勾选Hyper-V,然后点击确定,之后重启电脑; 注意不能同时安装VMware; 二、去docker注册账号,地址:https://hub.docker.com; 注册完账号后登陆可以在线下载docker 安装包 下载完成后运行安装包,安装完成后界面如图: 单击C...

docker运行爬虫代码

目录 docker 启动与停止 镜像制作 Dockerfile build 镜像 镜像操作 容器操作 docker运行爬虫代码 系统:Ubuntu 在项目根目录的同级目录下新建 Dockerfile 文件,这个Dockerfile就作为打包项目的配置文件 但是在这之前我还要做一件事情:就是将 python 依赖的一些基础环境打包成一个基础镜像,...

中标麒麟(龙芯CPU)--docker基础镜像制作

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。Docker 的出现为开发人员和运维人员带来了极大的便利。Docker在X86下常见的发行版Linux如Ubuntu、Centos上应用非常成熟,教程也非常多。本文基于龙芯平台(Mips64)+中标麒麟操作系统总结一下Docker的实践过程。 环境: 主机:龙芯3B...