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

https://blog.csdn.net/shudaqi2010/article/details/126378864

 

一、Harbor安装准备条件

这里以 harbor 2.5.3 版本为例

1.1 硬件要求

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

资源最小要求推荐配置
CPU 2 CPU 4 CPU
Mem 4 GB 8 GB
Disk 40 GB 160 GB

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

  1.  
    # 查看物理CPU个数
  2.  
    cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
  3.  
     
  4.  
    # 查看逻辑CPU个数
  5.  
    cat /proc/cpuinfo |grep "processor"|wc -l
  6.  
     
  7.  
    # 查看内存
  8.  
    free -h
  9.  
     
  10.  
    # 查看地盘
  11.  
    df -h

1.2 软件要求

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

软件版本号
docker 17.06.0 及以上
docker-compose 1.18.0 及以上
openssl

1.3 网络端口要求

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

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

二、下载 Harbor 的安装包文件

2.1 Harbor 安装包分类

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

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

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

2.2 下载并解压安装包

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

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

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

  1.  
    # 下载在线安装版
  2.  
    wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-online-installer-v2.5.3.tgz
  3.  
     
  4.  
    # 下载离线安装版
  5.  
    wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz

(4) 解压

  1.  
    # 解压在线安装版
  2.  
    tar -zxvf harbor-online-installer-v2.5.3.tgz
  3.  
     
  4.  
    # 解压离线安装版
  5.  
    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 文件夹,进入

  1.  
    [root@redrose2100 online]# ls
  2.  
    harbor harbor-online-installer-v2.5.3.tgz
  3.  
    [root@redrose2100 online]# cd harbor
  4.  
    [root@redrose2100 harbor]# ls
  5.  
    common.sh harbor.yml.tmpl install.sh LICENSE prepare
  6.  
    [root@redrose2100 harbor]#

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

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

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

3.1.1 配置域名

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

  1.  
    # The IP address or hostname to access admin UI and registry service.
  2.  
    # DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
  3.  
    hostname: harbor.redrose2100.com

3.1.2 配置 http 的端口

默认是 80,这里修改为 10001

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

3.1.3 注释 https 配置

暂时可以先将 https 配置注释

  1.  
    # https related config
  2.  
    #https:
  3.  
    # https port for harbor, default is 443
  4.  
    #port: 443
  5.  
    # The path of cert and key files for nginx
  6.  
    #certificate: /your/certificate/path
  7.  
    #private_key: /your/private/key/path

3.1.4 配置 admin 用户的密码

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

  1.  
    # The initial password of Harbor admin
  2.  
    # It only works in first time to install harbor
  3.  
    # Remember Change the admin password from UI after launching Harbor.
  4.  
    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"],

完整的内容如下所示:

  1.  
    [root@redrose2100 harbor]# cat /etc/docker/daemon.json
  2.  
    {
  3.  
    "exec-opts":["native.cgroupdriver=systemd"],
  4.  
    "registry-mirrors":[
  5.  
    "https://ooe7wn09.mirror.aliyuncs.com",
  6.  
    "https://registry.docker-cn.com",
  7.  
    "http://hub-mirror.c.163.com",
  8.  
    "https://docker.mirrors.ustc.edu.cn"
  9.  
    ],
  10.  
    "insecure-registries":["harbor.redrose2100.com"],
  11.  
    "dns": ["114.114.114.114", "8.8.8.8"]
  12.  
    }

然后重启 docker

systemctl restart docker

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

  1.  
    # 停止 harbor
  2.  
    docker-compose down -v
  3.  
     
  4.  
    # 启动 harbor
  5.  
    docker-compose up -d

3.1.7 验证 harbor

  1.  
    # 修改 hello-world 镜像的 tag 值
  2.  
    docker tag hello-world:latest harbor.redrose2100.com/redrose2100/hello-world:latest
  3.  
     
  4.  
    # 登录 harbor
  5.  
    docker login harbor.redrose2100.com -u admin -p xxxx(harbor的密码)
  6.  
     
  7.  
    # 推送镜像
  8.  
    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) 首先创建一个目录,用于存放生成的证书等文件

  1.  
    mkdir ssl
  2.  
    cd ssl

(2) 生成 CA 证书 私钥

openssl genrsa -out ca.key 4096

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

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

3.2.2 生成服务端证书

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

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

(2) 生成证书签名

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

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

  1.  
    cat > v3.ext <<-EOF
  2.  
    authorityKeyIdentifier=keyid,issuer
  3.  
    basicConstraints=CA:FALSE
  4.  
    keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  5.  
    extendedKeyUsage = serverAuth
  6.  
    subjectAltName = @alt_names
  7.  
     
  8.  
    [alt_names]
  9.  
    DNS.1=harbor.redrose2100.com
  10.  
    EOF

(4) 为 harbor 主机生成证书

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

3.2.3 为 harbor 和 docker 颁发证书

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

  1.  
    mkdir -p /data/cert/
  2.  
    cp harbor.redrose2100.com.crt /data/cert/
  3.  
    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的证书目录下,注意替换为自己的域名

  1.  
    mkdir -p /etc/docker/certs.d/harbor.redrose2100.com/
  2.  
    cp harbor.redrose2100.com.cert /etc/docker/certs.d/harbor.redrose2100.com/
  3.  
    cp harbor.redrose2100.com.key /etc/docker/certs.d/harbor.redrose2100.com/
  4.  
    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 的配置放开注释,而且修改证书的文件路径,如下所示

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

然后执行如下命令更新 harbor

./install.sh

3.2.5 Nginx 配置 https

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

  1.  
    server {
  2.  
    listen 443 ssl;
  3.  
    server_name harbor.redrose2100.com;
  4.  
     
  5.  
    ssl_certificate /data/cert/harbor.redrose2100.com.crt;
  6.  
    ssl_certificate_key /data/cert/harbor.redrose2100.com.key;
  7.  
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  8.  
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  9.  
     
  10.  
    ssl_session_cache shared:SSL:1m;
  11.  
    ssl_session_timeout 5m;
  12.  
     
  13.  
    location / {
  14.  
    client_max_body_size 1024M;
  15.  
    client_body_buffer_size 1024M;
  16.  
    proxy_redirect off;
  17.  
    proxy_pass https://172.22.27.162:8443;
  18.  
     
  19.  
    proxy_set_header Host $host;
  20.  
     
  21.  
    proxy_set_header X-Real-IP $remote_addr;
  22.  
     
  23.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24.  
    }
  25.  
     
  26.  
    error_page 500 502 503 504 /50x.html;
  27.  
    location = /50x.html {
  28.  
    root html;
  29.  
    }
  30.  
    }

然后将 http 的跳转到 https

  1.  
    server {
  2.  
    listen 80;
  3.  
    server_name harbor.redrose2100.com;
  4.  
    return 301 https://harbor.redrose2100.com$request_uri;
  5.  
    }

然后重新加载配置文件

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命令所在的目录

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

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

  1.  
    [root@redrose2100 nginx-1.21.4]# nginx -V
  2.  
    nginx version: nginx/1.21.4
  3.  
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  4.  
    built with OpenSSL 1.1.1o 3 May 2022
  5.  
    TLS SNI support enabled
  6.  
    configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.1.1o
  7.  
    [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

 

一、Harbor安装准备条件

这里以 harbor 2.5.3 版本为例

1.1 硬件要求

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

资源最小要求推荐配置
CPU 2 CPU 4 CPU
Mem 4 GB 8 GB
Disk 40 GB 160 GB

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

  1.  
    # 查看物理CPU个数
  2.  
    cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
  3.  
     
  4.  
    # 查看逻辑CPU个数
  5.  
    cat /proc/cpuinfo |grep "processor"|wc -l
  6.  
     
  7.  
    # 查看内存
  8.  
    free -h
  9.  
     
  10.  
    # 查看地盘
  11.  
    df -h

1.2 软件要求

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

软件版本号
docker 17.06.0 及以上
docker-compose 1.18.0 及以上
openssl

1.3 网络端口要求

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

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

二、下载 Harbor 的安装包文件

2.1 Harbor 安装包分类

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

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

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

2.2 下载并解压安装包

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

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

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

  1.  
    # 下载在线安装版
  2.  
    wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-online-installer-v2.5.3.tgz
  3.  
     
  4.  
    # 下载离线安装版
  5.  
    wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz

(4) 解压

  1.  
    # 解压在线安装版
  2.  
    tar -zxvf harbor-online-installer-v2.5.3.tgz
  3.  
     
  4.  
    # 解压离线安装版
  5.  
    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 文件夹,进入

  1.  
    [root@redrose2100 online]# ls
  2.  
    harbor harbor-online-installer-v2.5.3.tgz
  3.  
    [root@redrose2100 online]# cd harbor
  4.  
    [root@redrose2100 harbor]# ls
  5.  
    common.sh harbor.yml.tmpl install.sh LICENSE prepare
  6.  
    [root@redrose2100 harbor]#

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

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

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

3.1.1 配置域名

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

  1.  
    # The IP address or hostname to access admin UI and registry service.
  2.  
    # DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
  3.  
    hostname: harbor.redrose2100.com

3.1.2 配置 http 的端口

默认是 80,这里修改为 10001

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

3.1.3 注释 https 配置

暂时可以先将 https 配置注释

  1.  
    # https related config
  2.  
    #https:
  3.  
    # https port for harbor, default is 443
  4.  
    #port: 443
  5.  
    # The path of cert and key files for nginx
  6.  
    #certificate: /your/certificate/path
  7.  
    #private_key: /your/private/key/path

3.1.4 配置 admin 用户的密码

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

  1.  
    # The initial password of Harbor admin
  2.  
    # It only works in first time to install harbor
  3.  
    # Remember Change the admin password from UI after launching Harbor.
  4.  
    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"],

完整的内容如下所示:

  1.  
    [root@redrose2100 harbor]# cat /etc/docker/daemon.json
  2.  
    {
  3.  
    "exec-opts":["native.cgroupdriver=systemd"],
  4.  
    "registry-mirrors":[
  5.  
    "https://ooe7wn09.mirror.aliyuncs.com",
  6.  
    "https://registry.docker-cn.com",
  7.  
    "http://hub-mirror.c.163.com",
  8.  
    "https://docker.mirrors.ustc.edu.cn"
  9.  
    ],
  10.  
    "insecure-registries":["harbor.redrose2100.com"],
  11.  
    "dns": ["114.114.114.114", "8.8.8.8"]
  12.  
    }

然后重启 docker

systemctl restart docker

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

  1.  
    # 停止 harbor
  2.  
    docker-compose down -v
  3.  
     
  4.  
    # 启动 harbor
  5.  
    docker-compose up -d

3.1.7 验证 harbor

  1.  
    # 修改 hello-world 镜像的 tag 值
  2.  
    docker tag hello-world:latest harbor.redrose2100.com/redrose2100/hello-world:latest
  3.  
     
  4.  
    # 登录 harbor
  5.  
    docker login harbor.redrose2100.com -u admin -p xxxx(harbor的密码)
  6.  
     
  7.  
    # 推送镜像
  8.  
    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) 首先创建一个目录,用于存放生成的证书等文件

  1.  
    mkdir ssl
  2.  
    cd ssl

(2) 生成 CA 证书 私钥

openssl genrsa -out ca.key 4096

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

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

3.2.2 生成服务端证书

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

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

(2) 生成证书签名

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

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

  1.  
    cat > v3.ext <<-EOF
  2.  
    authorityKeyIdentifier=keyid,issuer
  3.  
    basicConstraints=CA:FALSE
  4.  
    keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  5.  
    extendedKeyUsage = serverAuth
  6.  
    subjectAltName = @alt_names
  7.  
     
  8.  
    [alt_names]
  9.  
    DNS.1=harbor.redrose2100.com
  10.  
    EOF

(4) 为 harbor 主机生成证书

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

3.2.3 为 harbor 和 docker 颁发证书

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

  1.  
    mkdir -p /data/cert/
  2.  
    cp harbor.redrose2100.com.crt /data/cert/
  3.  
    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的证书目录下,注意替换为自己的域名

  1.  
    mkdir -p /etc/docker/certs.d/harbor.redrose2100.com/
  2.  
    cp harbor.redrose2100.com.cert /etc/docker/certs.d/harbor.redrose2100.com/
  3.  
    cp harbor.redrose2100.com.key /etc/docker/certs.d/harbor.redrose2100.com/
  4.  
    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 的配置放开注释,而且修改证书的文件路径,如下所示

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

然后执行如下命令更新 harbor

./install.sh

3.2.5 Nginx 配置 https

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

  1.  
    server {
  2.  
    listen 443 ssl;
  3.  
    server_name harbor.redrose2100.com;
  4.  
     
  5.  
    ssl_certificate /data/cert/harbor.redrose2100.com.crt;
  6.  
    ssl_certificate_key /data/cert/harbor.redrose2100.com.key;
  7.  
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  8.  
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  9.  
     
  10.  
    ssl_session_cache shared:SSL:1m;
  11.  
    ssl_session_timeout 5m;
  12.  
     
  13.  
    location / {
  14.  
    client_max_body_size 1024M;
  15.  
    client_body_buffer_size 1024M;
  16.  
    proxy_redirect off;
  17.  
    proxy_pass https://172.22.27.162:8443;
  18.  
     
  19.  
    proxy_set_header Host $host;
  20.  
     
  21.  
    proxy_set_header X-Real-IP $remote_addr;
  22.  
     
  23.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24.  
    }
  25.  
     
  26.  
    error_page 500 502 503 504 /50x.html;
  27.  
    location = /50x.html {
  28.  
    root html;
  29.  
    }
  30.  
    }

然后将 http 的跳转到 https

  1.  
    server {
  2.  
    listen 80;
  3.  
    server_name harbor.redrose2100.com;
  4.  
    return 301 https://harbor.redrose2100.com$request_uri;
  5.  
    }

然后重新加载配置文件

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命令所在的目录

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

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

  1.  
    [root@redrose2100 nginx-1.21.4]# nginx -V
  2.  
    nginx version: nginx/1.21.4
  3.  
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  4.  
    built with OpenSSL 1.1.1o 3 May 2022
  5.  
    TLS SNI support enabled
  6.  
    configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.1.1o
  7.  
    [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