目录
1. 准备事项
安装准备,详见官网:Harbor Installation Prerequisites
-
检测80端口是否被占用:
netstat -tunlp|grep 80
,若使用其它端口,需在2.2节配置的hostname中加上端口号 -
环境:docker、docker-compose、https
1.1. 安装docker
- 移除电脑上原有的dockers
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- 安装docker
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce
-
配置阿里云镜像加速器
加速器地址:阿里云控制台–容器镜像服务–镜像工具–镜像加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
EOF
- 验证
systemctl daemon-reload &&
systemctl restart docker &&
systemctl enable docker &&
systemctl status docker
1.2. 安装docker-compose
- 下载docker-compose
- 授权、移动(并改名)至可执行目录下
- 授权
chmod +x
- 移动并改名(去掉版本号)
mv [] /usr/local/bin/docker-compose
- 验证
docker-compose version
1.3. 配置HTTPS访问证书
- 在工作目录下创建证书存放目录
mkdir ssl
cd ssl
- 创建证书
openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt
# 一路回车5次直至出现Common Name 输入(因为是CA,可不输入IP或域名):Harbor Cert Root CA
注:Harbor为自定义名称
- 生成证书签名请求
openssl req -newkey rsa:4096 -nodes -sha256 -keyout harbor.key -out harbor.csr
# 一路回车5次出现Common Name 输入IP或域名:lc.harbor.cn
- 新建extfile.cnf
--- vim extfile.cnf ---
subjectAltName = @alt_names
extendedKeyUsage = serverAuth
[alt_names]
# 域名,如有多个用DNS.2,DNS.3…来增加
DNS.1 = lc.harbor.cn
DNS.2 = *.harbor.cn
# IP地址, 服务器的ip
IP.1 = 172.16.16.104
IP.2 = 127.0.0.1
- 生成证书
openssl x509 -req -days 3650 -in harbor.csr -CA ca.crt -CAkey ca.key -CAcreateserial -extfile extfile.cnf -out harbor.crt
1.4. 为docker login配置证书
- 方式一:配置docker的CA证书,不需要重启docker(墙裂建议)
- 创建目录
mkdir -p /etc/docker/certs.d/lc.harbor.cn/
- 分发证书
cp ca.crt /etc/docker/certs.d/lc.harbor.cn/
- 方式二:配置insecure-registries,跳过https认证,重启docker生效
vim /etc/docker/daemon.json
"insecure-registries": ["xxx","lc.harbor.cn"],
-----------
- 重启docker
systemctl restart docker
- 方式三:配置系统级CA认证,需重启docker
- 分发证书
scp ca.crt root@172.16.16.103:/etc/pki/ca-trust/source/anchors/
- 更新配置并重启docker
update-ca-trust extract &&
systemctl restart docker
2. docker-compose安装Harbor
2.1. 下载harbor安装包
- 下载harbor
- 解压
tar zxvf harbor-offline-installer-v2.2.2.tgz
2.2. 配置harbor
- hostname:本机IP
- 证书地址:见1.3节
- 仓库地址:自定义
- 编辑harbor.yml.tmpl(按需修改)
vim harbor.yml.tmpl
------------------------
hostname: 172.16.16.104
http:
port: 80
https:
port: 443
certificate: /root/harbor/ssl/harbor.crt
private_key: /root/harbo/ssl/harbor.key
harbor_admin_password: Harbor12345
------------------------
- 生成harbor.yml文件
cp harbor.yml.tmpl harbor.yml
- 解压镜像
docker load --input harbor.v2.2.2.tar.gz
- 执行
./prepare
./install.sh
3. 登录harbor仓库
3.1. 网页登录
- 配置hosts
win10 hosts路径:C:\Windows\System32\drivers\etc\hosts
------------------
121.89.211.133 lc.harbor.cn
3.2. docker登录
- 配置hosts
- vim /etc/hosts(公网/私网按需配置)
------------------------
172.16.16.104 lc.harbor.cn
- docker login
docker login lc.harbor.cn -uadmin
3.3. 推送镜像
- 拉取官方镜像(默认tag为latest)
docker pull nginx
- 查看镜像
docker images |grep nginx
- 打tag
docker tag nginx:latest lc.harbor.cn/library/nginx:latest
- 推送(默认推送镜像需要登录)
docker push lc.harbor.cn/library/nginx:latest
- 网页上查看镜像
3.4. 拉取镜像
- 先移除本地打了tag的镜像
docker rmi lc.harbor.cn/library/nginx:latest
- 拉取镜像
docker pull lc.harbor.cn/library/nginx:latest
4. Harbor后台API2.0
4.1. 准备事项
- API手册链接位于网页左下角
- 标记多个tag并推送(不能使用相同的镜像)
docker tag nginx:1.17.0 lc.harbor.cn/library/nginx:100
docker tag nginx:1.18.0 lc.harbor.cn/library/nginx:18
docker tag nginx:1.19.0 lc.harbor.cn/library/nginx:19
docker tag nginx:1.20.0 lc.harbor.cn/library/nginx:20
docker tag nginx:latest lc.harbor.cn/library/nginx:latest
推送顺序:latest,100,18,19,20
4.2. HarborAPI2.0删除镜像示例
- 获取项目/仓库下的镜像信息
image_info=$(curl -s -k -u admin:Harbor12345 -X GET "https://lc.harbor.cn/api/v2.0/projects/library/repositories/nginx/artifacts?page=1&page_size=10&with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false" -H "accept: application/json")
echo $image_info
- 提取出镜像tag
tags="$(echo "$image_info" | tr , '\n' | grep name | cut -d '"' -f4)"
echo $tags
- 镜像tag排序,取出最近3个镜像以外的tag
for tag in `echo ${tags} | awk 'BEGIN{i=1}{gsub(/ /,"\n");i++;print}' | awk -F. '{print $NF}' | sed "1,3d"`;
do
curl -s -k -u admin:Harbor12345 -X DELETE https://lc.harbor.cn/api/v2.0/projects/library/repositories/nginx/artifacts/${tag}
done