Debian11之Harbor2.7.1安装及应用

Harbor致力于成为 Kubernetes 值得信赖的云原生存储库,帮助我们搭建属于自己的私有仓库,与Docker hub相比(1、上传下载镜像快 2、保护企业内部的镜像不被泄露),提供了管理UI、基于角色的访问控制、审计日志等企业用户需求的功能

安装条件

1、硬件条件

CPU 2-4核,推荐4核
内存 4-8G,推荐8G
硬盘 40-160G,推荐160G

2、软件条件

Docker engine [Version 17.06.0-ce+ or higher]
Docker Compose [docker-compose (v1.18.0+) or docker compose v2 (docker-compose-plugin)]
Openssl [Latest is preferred,用于生成证书和密钥对]

Docker在线安装教程

3、网路条件

80 [访问Harbor门户和核心API的HTTP请求]
443	[访问Harbor门户和核心API的HTTPS请求]
4443 [与Harbor的Docker内容信任服务的连接。仅在启用公证人的情况下才需要]

安装

官方安装教程
在这里插入图片描述

1、下载

1、在线安装程序:联机安装程序从 Docker 中心下载 Harbor 映像。因此安装程序的尺寸非常小
2、脱机安装程序:如果要将 Harbor 部署到的主机没有连接到 Internet,请使用脱机安装程序。脱机安装程序包含预构建的映像,因此它比联机安装程序大

Github下载地址
在这里插入图片描述
在线安装包:harbor-online-installer-v2.7.1.tgz
签名文件:harbor-online-installer-v2.7.1.tgz.asc
包含以上2个文件的MD5值,用于校验下载文件的准确性:md5sum

  • 验证在线安装包的真实性(可选)
gpg --keyserver hkps://keyserver.ubuntu.com --receive-keys 644FF454C0B4115C # 获取文件的公钥
# gpg -v --keyserver hkps://keyserver.ubuntu.com --verify harbor-online-installer-version.tgz.asc
gpg -v --keyserver hkps://keyserver.ubuntu.com --verify harbor-online-installer-v2.7.1.tgz.asc # 在线安装程序校验

2、解压安装包

# 上传相关包到当前目录(在线安装包、安装包校验文件)
cd /usr/local && mkdir -p harbor && cd harbor && mv /home/lixing/harbor/harbor-online-installer-v2.7.1.tgz /usr/local/harbor
# 解压在线安装包
tar xzvf harbor-online-installer-v2.7.1.tgz && mv harbor harbor2.7.1

在这里插入图片描述

3、Configure HTTPS Access

默认情况下 Harbor 不附带证书,只能通过HTTP即可访问,若要配置 HTTPS,必须创建 SSL 证书
可以使用由受信任的第三方CA 签名的证书,也可以使用自签名证书
以下将介绍如何使用 OpenSSL 创建 CA,以及如何使用 CA 对服务器证书和客户端证书进行签名

生成CA证书

cd /usr/local/harbor && mkdir -p certificate # 创建证书目录
openssl genrsa -out /usr/local/harbor/certificate/ca.key 4096 # 生成【CA】证书私钥
# 生成【CA】证书,注意:CN=<主机域名>
openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=192.168.111.20" \
 -key /usr/local/harbor/certificate/ca.key \
 -out /usr/local/harbor/certificate/ca.crt

通过CA证书生成服务器证书

# ‎生成【服务器】证书‎私钥
# openssl genrsa -out /usr/local/harbor/certificate/<主机域名>.key 4096
openssl genrsa -out /usr/local/harbor/certificate/192.168.111.20.key 4096
# 成服【服务器】证书,注意:CN=<主机的域名>
openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=192.168.111.20" \
    -key /usr/local/harbor/certificate/192.168.111.20.key \
    -out /usr/local/harbor/certificate/192.168.111.20.csr

生成 x509 v3 扩展文件

cd /usr/local/harbor/certificate # 执行以下命令,注意需要动态修改alt_names的相关字段:
######### 这里仅做模板展示
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=yourdomain.com
DNS.2=yourdomain
DNS.3=hostname
EOF
######### 这里仅做模板展示
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
IP.1=192.168.111.20
EOF

使用‎ v3.ext、ca.crt、ca.key、192.168.111.20.csr 为hrobor主机生成 192.168.111.20.crt 证书

openssl x509 -req -sha512 -days 3650 \
    -extfile /usr/local/harbor/certificate/v3.ext \
    -CA /usr/local/harbor/certificate/ca.crt -CAkey /usr/local/harbor/certificate/ca.key -CAcreateserial \
    -in /usr/local/harbor/certificate/192.168.111.20.csr \
    -out /usr/local/harbor/certificate/192.168.111.20.crt

为 Harbor 和 Docker 提供证书

# ‎将服务器证书、密钥复制到 Harbor 主机上的证书文件夹中‎
mkdir -p /etc/cert && cd /etc/cert \
&& cp -rf /usr/local/harbor/certificate/192.168.111.20.crt /etc/cert/ \
&& cp -rf /usr/local/harbor/certificate/192.168.111.20.key /etc/cert/
# 转换‎ <主机域名>.crt 为 <主机域名>.cert,‎供 Docker 使用‎
# openssl x509 -inform PEM -in <主机域名>.crt -out <主机域名>.cert
cd /etc/cert/ && openssl x509 -inform PEM -in 192.168.111.20.crt -out 192.168.111.20.cert

‎将服务器证书、密钥和 CA 文件复制到 Harbor 主机上的 Docker 证书文件夹‎

# cp /data/cert/192.168.213.142.cert /etc/docker/certs.d/<主机域名>/
# cp /data/cert/192.168.213.142.key /etc/docker/certs.d/<主机域名>/
# cp /usr/local/harbor/certificate/ca.crt /etc/docker/certs.d/<主机域名>/
mkdir -p /etc/docker/certs.d/192.168.111.20/ \
&& cp /etc/cert/192.168.111.20.cert /etc/docker/certs.d/192.168.111.20/ \
&& cp /etc/cert/192.168.111.20.key /etc/docker/certs.d/192.168.111.20/ \
&& cp /usr/local/harbor/certificate/ca.crt /etc/docker/certs.d/192.168.111.20/
systemctl daemon-reload && systemctl restart docker && systemctl status docker # 重启docker

4、自定义 harbor.yml

cd /usr/local/harbor/harbor2.7.1 && cp harbor.yml.tmpl harbor.yml # 备份配置文件
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
# Harbor主机域名或IP,对外暴露访问使用
hostname: 192.168.111.20
# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80
# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /etc/cert/192.168.111.20.crt
  private_key: /etc/cert/192.168.111.20.key
# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345
# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123
# The default data volume【******该文件夹需要提前创建******】
data_volume: /usr/local/harbor/data
# Log configurations
log:
  # options are debug, info, warning, error, fatal
  level: info
  # configs for logs in local storage
  local:
    # Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
    rotate_count: 50
    # Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes.
    # If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G
    # are all valid.
    rotate_size: 200M
    # The directory on your host that store log【******该文件夹需要提前创建******】
    location: /usr/local/harbor/log

5、安装 Harbor

systemctl daemon-reload && systemctl restart docker
cd /usr/local/harbor/harbor2.7.1 && ./prepare # 重新部署
cd /usr/local/harbor/harbor2.7.1 && ./install.sh # 安装 harbor
cd /usr/local/harbor/harbor2.7.1 && docker-compose stop # 停止
cd /usr/local/harbor/harbor2.7.1 && docker-compose start # 启动

设置开机自启

vi /usr/local/harbor/harbor.sh # 编辑脚本文件,添加如下内容
cd /usr/local/harbor/harbor2.7.1 && docker-compose start
chmod +x /usr/local/harbor/harbor.sh
vim /etc/rc.local # 编辑文件,添加如下内容
#!/bin/bash
/bin/bash /usr/local/harbor/harbor.sh
chmod +x /etc/rc.local

卸载

删除所有和harbor有关的目录

rm -rf 'find / -name harbor'

删除docker中的harbor容器

docker ps -a # 显示所有容器

在这里插入图片描述

docker stop <容器id> # 停止正在运行的容器
docker rm <容器id> # 删除正在运行的容器

删除docker中的harbor镜像

docker images # 显示所有镜像

在这里插入图片描述

docker rmi <镜像id>

Docker 登陆 Harbor

docker login https://192.168.111.20
默认管理员账号:admin
默认管理员密码:Harbor12345

在这里插入图片描述

浏览器登陆 Harbor

https://192.168.111.20

Harbor 相关操作

创建用户

在这里插入图片描述

创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

推送镜像(hello-world)到Harbor中的项目(private_dev_demo)

  • 编辑主机下docker的daemon.json文件
vi /etc/docker/daemon.json # 在原有配置的基础上追加 "insecure-registries":["192.168.111.20:80"] 配置
{
  "registry-mirrors": ["https://lixing.mirror.aliyuncs.com"]
  ,"insecure-registries":["192.168.111.20:80"]
}
systemctl daemon-reload && systemctl restart docker
  • 运行 hello-world 镜像
docker run hello-world # 默认从DockerHub 拉取镜像并运行
  • 查看 docker 镜像
    在这里插入图片描述
  • 为镜像(hello-world)打标签
# docker tag <镜像ID> harbor服务访问地址/<harbor中创建的项目名称>/<自定义镜像名称>:<自定义镜像版本>
docker tag feb5d9fea6a5 192.168.111.20:80/private_dev_demo/hello-world:20230410 && docker images

在这里插入图片描述

  • 推送镜像到harbor
docker login -u admin -p Harbor12345 https://192.168.111.20:80 # 登陆 Harbor 服务
docker push 192.168.111.20:80/private_dev_demo/hello-world:20230410

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

从Harbor中项目(private_dev_demo)拉取镜像(hello-world)

docker pull 192.168.111.20:80/private_dev_demo/hello-world:20230410
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大能嘚吧嘚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值