侧边栏壁纸
博主头像
xuesheng博主等级

分享web知识,学习就是取悦自己!

  • 累计撰写 118 篇文章
  • 累计创建 14 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

Harbor安装部署

xuesheng
2022-11-26 / 0 评论 / 1 点赞 / 241 阅读 / 2,441 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-11-26,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

下载 Harbor 的安装包文件

Harbor 安装包分类

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

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

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

下载并解压安装包

打开 Harbor发布网站挑选合适的版本,比如这里选择 v2.6.2 版本,点击 【v2.6.2】,进入发布附件页面
image-1669455794881

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

image-1669455848459

如服务器能联网,则到服务器直接使用wget 下载

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

解压

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

配置安装 Harbor

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

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

cd harbor

# ls 
# common.sh  harbor.yml.tmpl  install.sh  LICENSE  prepare

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

mv harbor.yml.tmpl harbor.yml

# ls
# common.sh  harbor.yml  install.sh  LICENSE  prepare

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

sudo vim harbor.yml

配置域名

配置 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 --->>> 主机名或者IP

配置 http 的端口

默认是 80,这里修改为 2080 , 根据自己需要改

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

注释 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
 

配置 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

执行安装 harbor 的脚本

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

sudo ./install.sh

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

image-1669457280195

修改 docker 配置文件

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

 "insecure-registries":["harbor.redrose2100.com:2080"],
# 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:2080"],
    "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

验证 harbor

# 修改 hello-world 镜像的 tag 值
docker tag caddy harbor.redrose2100.com:2080/test/caddy
# 登录 harbor
docker login harbor.redrose2100.com:2080 -u admin -p xxxx(harbor的密码)
# 推送镜像
docker push harbor.redrose2100.com:2080/test/caddy

image-1669457596291

基于http协议访问的默认的harbor已经安装完成了

配置 https 访问 Harbor

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

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

配置生成CA证书

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


mkdir ssl
cd ssl

生成 CA 证书 私钥

openssl genrsa -out ca.key 4096

生成 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

生成服务端证书

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

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

生成证书签名

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

生成 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

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

为 harbor 和 docker 颁发证书

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

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

crt文件转换为cert文件,为docker使用

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

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

重启docker

systemctl restart docker

修改 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

sudo ./install.sh

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

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 表示安装成功

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

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

nginx -s reload

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

默认安装命令没有启用 Notary, TrivyChart Repository Service

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

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

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

安装镜像签名

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

./install.sh --with-notary

安装扫描器

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

./install.sh --with-trivy

安装 Chart Repository 服务

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

./install.sh --with-chartmuseum
1
博主关闭了所有页面的评论