前言:
可能有测试环境,而测试环境下有N多的镜像,需要批量导入到自己搭建的Harbor私有仓库内,一般涉及到批量的操作,自然还是使用脚本比较方便。
本文将介绍如何把某个服务器的本地镜像 推送到带有安全证书的私有Harbor仓库。
一,
###本例中,私有Harbor仓库的地址是https://192.168.123.14,部署文档是:harbor仓库的构建及简单使用(修订版)_harbor 数据库_晚风_END的博客-CSDN博客
获取本地镜像完整名称
我们都知道镜像推送到仓库,需要提供镜像名称:版本号 这样的形式,如果是推送到私有仓库,该名字之前还需要加入私有仓库的名称。
例如:
192.168.123.14/library/registry.cn-shanghai.aliyuncs.com/c7n/nfs-client-provisioner:v3.1.0-k8s1.11
这样的镜像是分为三个部分
192.168.123.14/library/ 是第一部分,表示私有Harbor仓库的默认项目library
registry.cn-shanghai.aliyuncs.com/c7n/nfs-client-provisioner是第二部分,这个表示镜像名称
v3.1.0-k8s1.11是第三部分,这个表示镜像的版本号
例如,查询某个服务器下的所有镜像命令:
[root@centos4 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
csiplugin/snapshot-controller v4.0.0 f1d8a00ae690 2 years ago 46.6MB
asciinema/asciicast2gif latest e7ea78133adb 4 years ago 408MB
vmware/redis-photon v1.5.0 7c03076402d9 5 years ago 207MB
vmware/clair-photon v2.0.1-v1.5.0 7ae4e0670a3f 5 years ago 301MB
vmware/notary-server-photon v0.5.1-v1.5.0 0b2b23300552 5 years ago 211MB
vmware/notary-signer-photon v0.5.1-v1.5.0 67c41b4a1283 5 years ago 209MB
vmware/registry-photon v2.6.2-v1.5.0 3059f44f4b9a 5 years ago 198MB
vmware/nginx-photon v1.5.0 e100456182fc 5 years ago 135MB
vmware/harbor-log v1.5.0 62bb6b8350d9 5 years ago 200MB
vmware/harbor-jobservice v1.5.0 aca9fd2e867f 5 years ago 194MB
vmware/harbor-ui v1.5.0 1055166068d0 5 years ago 212MB
vmware/harbor-adminserver v1.5.0 019bc4544829 5 years ago 183MB
vmware/harbor-db v1.5.0 82354dcf564f 5 years ago 526MB
vmware/mariadb-photon v1.5.0 403e7e656499 5 years ago 526MB
vmware/postgresql-photon v1.5.0 35c891dea9cf 5 years ago 221MB
vmware/harbor-migrator v1.5.0 466c57ab0dc3 5 years ago 1.16GB
vmware/photon 1.0 4b481ecbef2a 5 years ago 130MB
首先,需要将第一行和第二行提取出来并按 镜像名称:镜像版本号的形式保存到文本文件内,那么,脚本应该如下:
#!/bin/bash
docker images|while read i t _;do
[[ "${t}" == "TAG" ]] && continue
echo $i:$t
done
通过重定向命令导入到指定文件内:
bash 脚本名 > images-list-new.txt
二,
推送脚本
根据上面步骤生成的镜像信息,修改所有镜像标签,并推送到私有Harbor仓库。
#!/bin/bash
for i in `cat images-list-v3.0.0.txt`;
do
docker tag $i 192.168.123.14/library/$i
docker push 192.168.123.14/library/$i
done
三,
推送服务器的证书处理
https比较麻烦,需要把Harbor所在的服务器上的证书拷贝一份,推送服务器的IP是192.168.123.11
根据最前面的Harbor部署文档,拷贝相关证书从14到11服务器上,在14服务器上执行:
scp -r /etc/docker/certs.d 192.168.123.11:/etc/docker/
脚本执行后,输出如下;
root@centos1 ~]# bash push.sh
v0.22.0: Pulling from library/flannel/flannel
Digest: sha256:79b736171912bca65ab67befdeb9de53da652130f8d9da748343c73bedcddfbd
Status: Image is up to date for 192.168.123.14/library/flannel/flannel:v0.22.0
192.168.123.14/library/flannel/flannel:v0.22.0
v3.3.2: Pulling from library/kubesphere/ks-installer
Digest: sha256:c4d309f7c71068a7b6cda3437e4db868765d9f843ea72d9411a8c6c791a600f6
Status: Image is up to date for 192.168.123.14/library/kubesphere/ks-installer:v3.3.2
在Harbor的web界面可以看到相关日志,表示推送成功:
四,
私有仓库的批量创建项目脚本:
####注:原脚本是适用于Harbor2.0版本的,而我现在的Harbor版本是1.5.0,因此,做了适当的改造
#### ${url}/api/v2.0/projects 这里删除了v2.0
#### \"public\": true修改为了\"metadata\": {\"public\": \"true\"}, \"storage_limit\": -1
[root@centos1 ~]# cat create_project.sh
#!/usr/bin/env bash
# Copyright 2018 The KubeSphere Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
url="https://192.168.123.14"##私有仓库的地址
user="admin"
passwd="mima"###密码就不告诉你,其实就是登录Harbor的密码。请牢记
harbor_projects=(library
kubesphere
calico
coredns
openebs
csiplugin
minio
mirrorgooglecontainers
osixia
prom
thanosio
jimmidyson
grafana
elastic
istio
jaegertracing
jenkins
weaveworks
openpitrix
joosthofman
nginxdemos
fluent
kubeedge
)
for project in "${harbor_projects[@]}"; do
echo "creating $project"
curl -k -u "${user}:${passwd}" -X POST -H "Content-Type: application/json" "${url}/api/projects" -d "{ \"project_name\":\"${project}\", \"metadata\": {\"public\": \"true\"}, \"storage_limit\": -1}"
done
在Harbor的web界面,可以看到确实创建了指定的项目:文章来源:https://www.toymoban.com/news/detail-690681.html
文章来源地址https://www.toymoban.com/news/detail-690681.html
到了这里,关于Docker|kubernetes|本地镜像批量推送到Harbor私有仓库的脚本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!