docker-compose安装harbor

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

    • 准备事项
    • 安装docker
    • 安装docker-compose
    • docker-compose安装Harbor
    • 登录harbor仓库
    • Harbor后台API2.0
    • HarborAPI2.0删除镜像示例


准备事项

安装准备,详见官网:Harbor
参考:准备工作
上传工具

检测80端口是否被占用:netstat -tunlp|grep 80,若使用其它端口,需在2.2节配置的hostname中加上端口号
harbor可能与ceph存在冲突,尽量选择其它节点部署
环境:docker、docker-compose、https
docker-compose安装harbor_第1张图片

安装docker

[root@localhost ~]# yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install -y docker-ce
配置阿里云镜像加速器
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
 {
 "registry-mirrors": ["https://ohdpuoqu.mirror.aliyuncs.com"]
 }
EOF
验证
[root@localhost ~]# sudo systemctl daemon-reload &&
 sudo systemctl restart docker &&
 sudo systemctl enable docker &&
 sudo systemctl status docker

安装docker-compose

下载docker-compose

[root@localhost ~]# cd /home/samba/
[root@localhost samba]# chmod +x docker-compose-linux-x86_64 #提权
[root@localhost samba]# mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose #转移到指定目录
[root@localhost samba]# docker-compose version #测试,查看版本信息
Docker Compose version v2.10.2

配置HTTPS访问证书

[root@localhost /]# mkdir ssl
[root@localhost /]# cd ssl
创建证书
[root@localhost 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为自定义名称
生成证书签名请求
[root@localhost ssl]# openssl req  -newkey rsa:4096 -nodes -sha256 -keyout harbor.key -out harbor.csr
# 一路回车5次出现Common Name 输入IP或域名:test.harbor.cn
新建extfile.cnf
[root@localhost ssl]# vim extfile.cnf
subjectAltName = @alt_names
extendedKeyUsage = serverAuth
[alt_names]
# 域名,如有多个用DNS.2,DNS.3…来增加
DNS.1 = test.harbor.cn
DNS.2 = *.harbor.cn
# IP地址, 服务器的ip
IP.1 = 192.168.1.10
生成证书
[root@localhost ssl]# openssl x509 -req -days 3650 -in harbor.csr -CA ca.crt -CAkey ca.key -CAcreateserial -extfile extfile.cnf -out harbor.crt

为docker login配置证书
配置docker的CA证书,不需要重启docker
[root@localhost ssl]# mkdir -p /etc/docker/certs.d/test.harbor.cn/ #创建目录
[root@localhost ssl]# cp ca.crt /etc/docker/certs.d/test.harbor.cn/ #分发证书

docker-compose安装Harbor

下载harbor

[root@localhost harbor]# tar zxvf harbor-offline-installer-v2.6.0.tgz

[root@localhost harbor]# vim harbor.yml.tmpl   (按需修改)
hostname:本机IP
证书地址:见前面ssl地址
仓库地址:自定义
hostname: 192.168.1.10

  certificate: /ssl/harbor.crt
  private_key: /ssl/harbor.key

harbor_admin_password: Harbor12345

[root@localhost harbor]# cp harbor.yml.tmpl harbor.yml         #生成harbor.yml文件
[root@localhost harbor]# docker load --input harbor.v2.6.0.tar.gz    #解压镜像
[root@localhost harbor]# ./prepare                     #执行
[root@localhost harbor]# ./install.sh                   #执行
[root@localhost harbor]# vim /usr/lib/systemd/system/harbor.service
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor

[Service]
Type=simple
Restart=on-failure
RestartSec=5
#注意docker-compose和harbor的安装位置
ExecStart=/usr/local/bin/docker-compose -f  /home/harbor/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /home/harbor/harbor/docker-compose.yml down

[Install]
WantedBy=multi-user.target
systemctl enable harbor  #启动harbor服务
systemctl restart harbor  #重启harbor服务
systemctl status harbor  #查看harbor服务状态

登录harbor仓库

配置hosts

win10  
C:\Windows\System32\drivers\etc\hosts
192.168.1.10 test.harbor.cn 

访问 test.harbor.cn #没修改harbor.yml.tmpl里面的默认密码 就是admin Harbor12345
docker-compose安装harbor_第2张图片

docker登录

[root@localhost harbor]# vim /etc/hosts
192.168.1.10 test.harbor.cn

docker login

[root@localhost harbor]# docker login test.harbor.cn -uadmin
Password: 
Login Succeeded    #登陆成功
docker pull nginx   #拉取官方镜像(默认tag为latest)
docker images |grep nginx    #查看镜像
docker tag nginx:latest test.harbor.cn/library/nginx:latest   #打tag
docker push test.harbor.cn/library/nginx:latest    推送(默认推送镜像需要登录)

网页上查看镜像
docker-compose安装harbor_第3张图片

拉取镜像

[root@localhost harbor]# docker rmi test.harbor.cn/library/nginx:latest #先移除本地打了tag的镜像
[root@localhost harbor]# docker pull test.harbor.cn/library/nginx:latest

Harbor后台API2.0

准备事项
API手册链接位于网页左下角
标记多个tag并推送(不能使用相同的镜像,提前拉取各版本官方镜像)

[root@localhost harbor]# docker tag nginx:1.17.0 test.harbor.cn/library/nginx:100
[root@localhost harbor]# docker tag nginx:1.18.0 test.harbor.cn/library/nginx:18
[root@localhost harbor]# docker tag nginx:1.19.0 test.harbor.cn/library/nginx:19
[root@localhost harbor]# docker tag nginx:1.20.0 test.harbor.cn/library/nginx:20
[root@localhost harbor]# docker tag nginx:latest test.harbor.cn/library/nginx:latest
推送顺序:latest,100,18,19,20

HarborAPI2.0删除镜像示例

获取项目/仓库下的镜像信息

[root@localhost harbor]# image_info=$(curl -s -k -u admin:Harbor12345 -X GET "https://test.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")
[root@localhost harbor]# echo $image_info 

提取出镜像tag

[root@localhost harbor]# tags="$(echo "$image_info" | tr , '\n' | grep name | cut -d '"' -f4)"
[root@localhost harbor]# echo $tags
20 19 18 100 latest

镜像tag排序,取出最近3个镜像以外的tag

[root@localhost harbor]# 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://test.harbor.cn/api/v2.0/projects/library/repositories/nginx/artifacts/${tag}
done

在这里插入图片描述

你可能感兴趣的:(docker,docker,linux,运维)