Harbor----使用 Harbor 安装包安装部署 Harbor

一、Harbor安装准备条件

这里以 harbor 2.5.3 版本为例

1.1 硬件要求

Harbor 安装对硬件资源CPU、内存和硬盘的要求如下表:

资源最小要求推荐配置
CPU2 CPU4 CPU
Mem4 GB8 GB
Disk40 GB160 GB

使用如下命令分别查看服务器的物理CPU和逻辑CPU个数

# 查看物理CPU个数
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l

# 查看逻辑CPU个数
cat /proc/cpuinfo |grep "processor"|wc -l

# 查看内存
free -h

# 查看地盘
df -h

1.2 软件要求

harbor 2.5.3 版本对docker、docker-compose等软件的要求

软件版本号
docker17.06.0 及以上
docker-compose1.18.0 及以上
openssl

1.3 网络端口要求

harbor 2.5.3 版本对网络端口的要求如下表

端口协议描述
443httpshttps端口,可以从配置文件中修改
4443https当notary打开时使用,否则不需要使用,可以通过配置文件修改
80httphttp端口,可以从配置文件中修改

二、下载 Harbor 的安装包文件

2.1 Harbor 安装包分类

Harbor 的安装文件分为在线安装包和离线安装包

  • 在线安装包:
    在线安装包从 docker Hub 下载镜像,因此安装包文件比较小,适用于安装服务器能联网的场景下

  • 离线安装包:
    离线安装包包含了预先编译好的镜像文件,因此安装文件较大,适用于安装服务器未联网的场景下

2.2 下载并解压安装包

(1) 打开 Harbor发布网站 挑选合适的版本,比如这里选择 v2.5.3 版本,点击 【v2.5.3】,进入发布附件页面,如下:

(2) 选择版本,如果服务器能联网,则右键复制链接,如果服务器不能联网,则只能点击下载,然后手工将安装包上传至服务器

(3) 如服务器能联网,则到服务器直接使用wget 下载,命令如下:

# 下载在线安装版
wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-online-installer-v2.5.3.tgz

# 下载离线安装版
wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz

(4) 解压

# 解压在线安装版
tar -zxvf harbor-online-installer-v2.5.3.tgz

# 解压离线安装版
tar -zxvf harbor-offline-installer-v2.5.3.tgz

三、配置安装 Harbor

3.1 安装基于 http 协议访问的默认的 Harbor

默认安装命令没有启用 Notary, Trivy 和 Chart Repository Service。

  • Notary:镜像签名认证
  • Trivy: 容器漏洞扫描
  • Chart Repository Service: Helm chart 仓库服务

一般在内网的情况下,可以直接使用 http 的方式访问,这种方式安装部署方式相对比较简单,在 harbor 压缩包解压后有一个 harbor 文件夹,进入

[root@redrose2100 online]# ls
harbor  harbor-online-installer-v2.5.3.tgz
[root@redrose2100 online]# cd harbor
[root@redrose2100 harbor]# ls
common.sh  harbor.yml.tmpl  install.sh  LICENSE  prepare
[root@redrose2100 harbor]#

可以发现 harbor 文件夹中有一个 harbor.yml。tmpl文件,将其文件名修改为 harbor.yml

[root@redrose2100 harbor]# mv harbor.yml.tmpl harbor.yml
[root@redrose2100 harbor]# ls
common.sh  harbor.yml  install.sh  LICENSE  prepare
[root@redrose2100 harbor]#

然后编辑通过 vi harbor.yml 编辑 harbor.yml 文件

3.1.1 配置域名

配置 harbor 的域名,如果内部的不用域名,可以直接设置主机名或者IP

# 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.
hostname: harbor.redrose2100.com

3.1.2 配置 http 的端口

默认是 80,这里修改为 10001

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 10001

3.1.3 注释 https 配置

暂时可以先将 https 配置注释

# https related config
#https:
  # https port for harbor, default is 443
  #port: 443
  # The path of cert and key files for nginx
  #certificate: /your/certificate/path
  #private_key: /your/private/key/path

3.1.4 配置 admin 用户的密码

默认密码是Harbor12345,这里可以修改自己的密码

# 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

3.1.5 执行安装 harbor 的脚本

其他配置暂时保持默认配置,执行瑞安脚本即可

./install.sh

安装完成后,在浏览器打开 http://harbor.redrose210.com 即可打开 harbor 的页面了

3.1.6 修改 docker 配置文件

安装完成 harbor 后,因为此时使用的是 http 协议,此时需要在 docker 的配置文件 /etc/docker/daemon.json 中增加如下内容:

 "insecure-registries":["harbor.redrose2100.com"],

完整的内容如下所示:

[root@redrose2100 harbor]# cat /etc/docker/daemon.json
{
    "exec-opts":["native.cgroupdriver=systemd"],
    "registry-mirrors":[
        "https://ooe7wn09.mirror.aliyuncs.com",
        "https://registry.docker-cn.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn"
    ],
    "insecure-registries":["harbor.redrose2100.com"],
    "dns": ["114.114.114.114", "8.8.8.8"]
}

然后重启 docker

systemctl restart docker

再重启 harbor , 在 harbor 文件夹中执行如下两条命令

# 停止 harbor
docker-compose down -v

# 启动 harbor
docker-compose up -d

3.1.7 验证 harbor

# 修改 hello-world 镜像的 tag 值
docker tag hello-world:latest harbor.redrose2100.com/redrose2100/hello-world:latest

# 登录 harbor
docker login harbor.redrose2100.com -u admin -p xxxx(harbor的密码)

# 推送镜像
docker push harbor.redrose2100.com/redrose2100/hello-world:latest

然后到页面上,可以发现,此时镜像已经推送到 harbor 了

至此,最简单的基于http协议访问的默认的harbor已经安装完成了

3.2 配置 https 访问 Harbor

在生产环境,需要从CA获取证书,在测试环境,可以通过openssl生成CA证书以及通过CA生成服务端证书和客户端证书

如下假设harbor的域名为: harbor.redrose2100.com

3.2.1 配置生成CA证书

(1) 首先创建一个目录,用于存放生成的证书等文件

mkdir ssl
cd ssl

(2) 生成 CA 证书 私钥

openssl genrsa -out ca.key 4096

(3) 生成 CA 证书,注意修改域名

openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.redrose2100.com" \
 -key ca.key \
 -out ca.crt

3.2.2 生成服务端证书

(1) 生成服务端证书私钥,注意替换自己的域名

openssl genrsa -out harbor.redrose2100.com.key 4096

(2) 生成证书签名

openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.redrose2100.com" \
    -key harbor.redrose2100.com.key \
    -out harbor.redrose2100.com.csr

(3) 生成 v3 扩展文件,注意替换为自己的域名

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.redrose2100.com
EOF

(4) 为 harbor 主机生成证书

openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in harbor.redrose2100.com.csr \
    -out harbor.redrose2100.com.crt

3.2.3 为 harbor 和 docker 颁发证书

(1) 将服务端证书拷贝至 /data/cert/目录下

mkdir -p /data/cert/
cp harbor.redrose2100.com.crt /data/cert/
cp harbor.redrose2100.com.key /data/cert/

(2) 将 crt文件转换为cert文件,为docker使用

openssl x509 -inform PEM -in harbor.redrose2100.com.crt -out harbor.redrose2100.com.cert

(3) 将 harbor.redrose2100.com.cert harbor.redrose2100.com.key ca.crt 文件拷贝到docker的证书目录下,注意替换为自己的域名

mkdir -p /etc/docker/certs.d/harbor.redrose2100.com/
cp harbor.redrose2100.com.cert /etc/docker/certs.d/harbor.redrose2100.com/
cp harbor.redrose2100.com.key /etc/docker/certs.d/harbor.redrose2100.com/
cp ca.crt /etc/docker/certs.d/harbor.redrose2100.com/

这里需要注意的是,如果nginx中对https没有使用默认的443端口,修改为其他端口了,则此时的需要创建的目录为:/etc/docker/certs.d/yourdomain.com:port, 或者 /etc/docker/certs.d/harbor_IP:port

(4) 重启docker

systemctl restart docker

3.2.4 修改 harbor.yml 配置文件

修改配置,开启 https 配置,如下前面部署 http 模式的时候将https的部分注释了,这里要将 https 的配置放开注释,而且修改证书的文件路径,如下所示

# https related config
https:
  # https port for harbor, default is 443
  port: 8443
  # The path of cert and key files for nginx
  certificate: /data/cert/harbor.redrose2100.com.crt
  private_key: /data/cert/harbor.redrose2100.com.key

然后执行如下命令更新 harbor

./install.sh

3.2.5 Nginx 配置 https

修改 nginx.conf 配置,增加监听 443 端口的,跳转到harbor的8443端口,此外在这里需要指定证书文件的路径

server {
        listen       443 ssl;
        server_name  harbor.redrose2100.com;

        ssl_certificate      /data/cert/harbor.redrose2100.com.crt;
        ssl_certificate_key  /data/cert/harbor.redrose2100.com.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        location / {
          client_max_body_size  1024M;
          client_body_buffer_size  1024M;
          proxy_redirect off;
          proxy_pass https://172.22.27.162:8443;

          proxy_set_header Host $host;

          proxy_set_header X-Real-IP $remote_addr;

          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

然后将 http 的跳转到 https

server {
        listen       80;
        server_name  harbor.redrose2100.com;
        return 301 https://harbor.redrose2100.com$request_uri;
    }

然后重新加载配置文件

nginx -s reload

此时在浏览器就可以 通过 http://harbor.redrose2100.com 和 https://harbor.redrose2100.com 访问,当使用 http://harbor.redrose2100.com 时会发现自动跳转到 https://harbor.redrose2100.com

3.2.6 Nginx 配置可能遇到额问题

注意,如果出现类似如下错误:

nginx: [emerg] unknown directive "ssl_certificate" in /usr/local/nginx/conf/nginx.conf

或者类似如下错误;

error: SSL modules require the OpenSSL library.

说明 nginx 没有安装ssl,此时进入nginx的源码目录,执行如下命令

 ./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.1.1o

其中:

  • /usr/local/src/openssl-1.1.1o 目录 为openssl 源码包的解压目录

然后使用 make 编译,注意,不要使用 make install,否则会覆盖

make

然后备份原有的nginx 命令,并将新编译的nginx拷贝的nginx命令所在的目录

# 将原有的命令重命名备份
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bark
# 将新编译的nginx命令拷贝到nginx命令的目录下
cp objs/nginx /usr/local/nginx/sbin/

然后执行 nginx -V ,如下,出现 --with-http_ssl_module 表示安装成功

[root@redrose2100 nginx-1.21.4]# nginx -V
nginx version: nginx/1.21.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1o  3 May 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.1.1o
[root@redrose2100 nginx-1.21.4]#

然后再次执行如下命令重新加载nginx配置

nginx -s reload

3.3 安装镜像签名

安装签名时,只需要在执行 ./install.sh 的命令的时候带上 --with-notary 参数即可

./install.sh --with-notary

3.4 安装扫描器

安装扫描器时,只需要在执行 ./install.sh 的命令的时候带上 --with-trivy 参数即可

./install.sh --with-trivy

3.5 安装 Chart Repository 服务

安装 Chart Repository 服务时,只需要在执行 ./install.sh 的命令的时候带上 --with-chartmuseum 参数即可

./install.sh --with-chartmuseum

3.6 同时安装签名、扫描器、Chart Repository 服务

同时安装签名、扫描器、Chart Repository 服务时只需要将三个参数都带上即可

./install.sh --with-notary --with-trivy --with-chartmuseum

感谢充电鼓励与支持

在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

您的鼓励是我最大的创作动力

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

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

打赏作者

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

抵扣说明:

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

余额充值