本教程提供使用[设置Harbor的分步指南 Let's Encrypt](https://letsencrypt.org/)证书使用Certbot

要求

  • Linux机器_(用Ubuntu 20.04测试)_

  • 指向你的 Linux 机器的公共域

  • 端口 80/443 可从外部访问

  • 码头工人

  • 码头工人撰写

Certbot / 让我们加密

首先,我们需要安装 Certbot 以在我们的机器上创建 Let's Encrypt 证书。在 Ubuntu 上,这可以通过使用 snap 轻松完成:

$ snap install certbot --classic

进入全屏模式 退出全屏模式

之后,我们可以使用 Certbot 的独立模式为我们的 Harbor 域创建一个新证书,并将所需的域添加为参数。首次运行 Certbot 时,它会在创建证书之前要求提供有效的电子邮件地址并接受服务条款。

$ certbot certonly --standalone -d registry.example.com

进入全屏模式 退出全屏模式

命令完成后,它应该为我们提供以下输出:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
  /etc/letsencrypt/live/registry.example.com/fullchain.pem
  Your key file has been saved at:
  /etc/letsencrypt/live/registry.example.com/privkey.pem
  Your certificate will expire on 2021-04-21. To obtain a new or
  tweaked version of this certificate in the future, simply run
  certbot again. To non-interactively renew *all* of your
  certificates, run "certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
Donating to EFF:                    https://eff.org/donate-le

进入全屏模式 退出全屏模式

最后,我们可以在**/etc/letsencrypt/live**的相关域的文件夹中找到证书文件。

$ ls /etc/letsencrypt/live/registry.example.com
README  cert.pem  chain.pem  fullchain.pem  privkey.pem

进入全屏模式 退出全屏模式

港口

现在我们可以继续在机器上安装 Harbor。我们去Harbor GitHub Releases 页面,下载最新的安装程序并解压存档:

$ wget https://github.com/goharbor/harbor/releases/download/v2.1.3/harbor-online-installer-v2.1.3.tgz
$ tar xvf harbor-online-installer-v2.1.3.tgz

进入全屏模式 退出全屏模式

要配置 Harbor 实例,我们可以创建提取的 Harbor 配置模板的副本并使用编辑器打开它:

$ cd harbor
$ cp harbor.yml.tmpl harbor.yml
$ vim harbor.yml

进入全屏模式 退出全屏模式

配置文件提供了很多属性来配置和自定义 Harbor 实例。对于简约安装,更改以下值就足够了:

# Change the hostname to your domain configured earlier
hostname: reg.mydomain.com
# Keep the http and https port configuration, but change the path of
# the certificate files to the Let's Encrypt certificate
http:
  port: 80
https:
  port: 443
  certificate: /your/certificate/path
  private_key: /your/private/key/path
# Change the admin password to something more secure
harbor_admin_password: Harbor12345
# Change the database password to something more secure
database:
  password: root123

进入全屏模式 退出全屏模式

调整配置后,Harbor 就可以使用安装脚本进行部署了:

$ ./install.sh
...
✔ ----Harbor has been installed and started successfully.----

进入全屏模式 退出全屏模式

最后,让我们在浏览器中打开 Harbor 实例的域并检查结果。我们应该看到由有效证书保护的 Harbor 登录页面。

证书更新

Certbot 创建一个 Cronjob/Timer 以在请求的证书过期之前自动更新它们。

$ systemctl list-timers
Fri 2021-01-22 07:10:00 CET 5h 57min left Thu 2021-01-21 22:20:02 CET 2h 52min ago snap.certbot.renew.timer     snap.certbot.renew.service

进入全屏模式 退出全屏模式

目前这个更新会失败,因为Harbor的Nginx容器已经在使用机器上的80端口了。 Certbot 需要此端口来更新现有证书。

幸运的是,Certbot 提供了 Pre-/Post-Hooks,可以创建它们以在更新证书之前/之后停止/开始运行服务。

这些钩子可以在 /etc/letsencrypt/renewal-hooks/{pre|post} 创建。在运行更新任务之前,让我们创建一个脚本来停止 Harbor 的 Nginx 容器:

$ vim /etc/letsencrypt/renewal-hooks/pre/harbor.sh

Insert the following content:

#!/bin/bash
/usr/bin/docker stop nginx

Make the script executable:

$ chmod 755 /etc/letsencrypt/renewal-hooks/pre/harbor.sh

进入全屏模式 退出全屏模式

现在我们还必须创建 post-hook 脚本,将新更新的证书复制到 Harbor 的数据目录并再次启动 Nginx 容器。创建后挂钩脚本并添加以下内容:

$ vim /etc/letsencrypt/renewal-hooks/post/harbor.sh

Insert the following content:

#!/bin/bash
cp -f /etc/letsencrypt/live/registry.example.com/fullchain.pem /data/secret/cert/server.crt
cp -f /etc/letsencrypt/live/registry.example.com/privkey.pem /data/secret/cert/server.key
/usr/bin/docker start nginx

Make the script executable:

$ chmod 755 /etc/letsencrypt/renewal-hooks/post/harbor.sh

进入全屏模式 退出全屏模式

请记住根据您的设置更改路径。可以在harbor.yml 文件中找到为Harbor 配置的数据目录。

要检查脚本是否配置正确,我们可以在试运行模式下使用 Certbot 的 renew 命令测试证书的更新。我们应该看到以下输出:

$ certbot renew --dry-run
...
Running pre-hook command: /etc/letsencrypt/renewal-hooks/pre/harbor.sh
Output from pre-hook command harbor.sh:
nginx
...
Running post-hook command: /etc/letsencrypt/renewal-hooks/post/harbor.sh
Output from post-hook command harbor.sh:
nginx

进入全屏模式 退出全屏模式

现在设置完成。 Harbor 实例应该已启动并正在运行,并且 Let's Encrypt 证书应该会自动更新。

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐