私有镜像仓库 Harbor 安装
本文汇总 Harbor 私有镜像仓库的三种安装方式:
- docker + http 离线安装:有 docker / docker-compose 环境,最常用,仓库走 http。
- nerdctl 离线安装:containerd 环境(无 docker),用 nerdctl 替代 docker/docker-compose。
- https 安装:自签证书,仓库走 https,并配置开机自启。
安装文件位于 install/kubernetes/harbor/。Harbor 安装后 admin 默认密码为 Harbor12345(在 harbor.yml 中可改 harbor_admin_password)。
一、docker + http 离线安装
1. 离线下载安装包
# github地址
# amd64版本
wget https://githubfast.com/goharbor/harbor/releases/download/v2.11.1/harbor-offline-installer-v2.11.1.tgz
# wget https://cube-studio.oss-cn-hangzhou.aliyuncs.com/install/harbor-offline-installer-v2.11.1.tgz
# arm64版本
wget https://githubfast.com/wise2c-devops/build-harbor-aarch64/releases/download/v2.13.0/harbor-offline-installer-aarch64-v2.13.0.tgz
# 解压
rm -rf /usr/local/harbor
tar xf harbor-offline-installer-v2.11.1.tgz -C /usr/local/
cd /usr/local/harbor
cp harbor.yml.tmpl harbor.yml
2. 修改 harbor.yml(hostname、登录密码、关闭 https)
vim harbor.yml,主要涉及:hostname 改为 ip,http 端口可以换掉,https 块去掉:
hostname: xx.xx.xx.xx
harbor_admin_password: admin
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 88
#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
data_volume: /data #这个路径是宿主机的路径,根据实际情况修改成空间大的地方
# external_url: http://xx.xx.xx.xx:88 # 如果有公网地址,这里填写
3. 执行安装
安装 Harbor 前需要先安装 docker 和 docker-compose,并启动它们。Docker 安装参考 02-部署安装/节点准备/安装Docker。
cd /usr/local/harbor
# 每次修改了配置文件都要删除之前的配置,重新安装
rm -rf ./common/config
./install.sh
用 docker-compose 查看 Harbor 容器运行状态:
docker-compose ps
4. 客户端使用 http 仓库服务
http(非 https)仓库需要在 docker 客户端把仓库地址加入 insecure-registries。vi /etc/docker/daemon.json:
{
"insecure-registries":["xx.xx.xx.xx:88"]
}
重启 docker 生效:
systemctl stop docker
systemctl daemon-reload
systemctl start docker
二、nerdctl 离线安装(containerd 环境,无 docker)
无 docker 环境下,用 nerdctl 替换安装脚本中的 docker/docker-compose 命令。
# 下载离线安装包
wget https://cube-studio.oss-cn-hangzhou.aliyuncs.com/harbor/harbor-offline-installer-v2.3.4.tgz
# 解压并拷贝至 /usr/local/ 目录下
tar xf harbor-offline-installer-v2.3.4.tgz -C /usr/local/
# 进入 /usr/local/harbor 目录
cd /usr/local/harbor
# 复制 harbor 配置文件
cp harbor.yml.tmpl harbor.yml
# 注释配置文件中的 https,修改 http 端口,并设置 hostname 为主机 ip
vi harbor.yml
# 默认密码 Harbor12345
# 将 install.sh、common.sh、prepare 文件中的 docker-compose 改为 nerdctl compose,docker 改为 nerdctl
sed -i 's/docker-compose/nerdctl compose/g' install.sh
sed -i 's/docker/nerdctl/g' install.sh
sed -i 's/docker-compose/nerdctl compose/g' common.sh
sed -i 's/docker/nerdctl/g' common.sh
sed -i 's/docker-compose/nerdctl compose/g' prepare
sed -i 's/docker/nerdctl/g' prepare
# 执行安装
# 1、vi install.sh 中去除 check_nerdctl、check_nerdctlcompose 等命令
# 2、执行安装
bash ./install.sh
卸载
cd /usr/local/harbor
nerdctl compose down
rm -rf `find / -name harbor`
# 将运行的容器全部停止
nerdctl stop `nerdctl ps -q`
# 将容器全部删除
nerdctl rm `nerdctl ps -qa`
# 将镜像全部删除
nerdctl rmi `nerdctl images -q`
三、https 安装(自签证书)
参考 https://blog.csdn.net/networken/article/details/107502461
1. 生成 https 证书
以 registry.harbor.com 域名为例:
mkdir /root/ssl
cd /root/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=registry.harbor.com" \
-key ca.key \
-out ca.crt
# 生成服务器证书私钥
openssl genrsa -out registry.harbor.com.key 4096
# 生成证书签名请求(CSR)
openssl req -sha512 -new \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=registry.harbor.com" \
-key registry.harbor.com.key \
-out registry.harbor.com.csr
# 生成一个 x509 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=registry.harbor.com
DNS.2=registry.harbor
DNS.3=harbor
EOF
# 使用该 v3.ext 文件为 Harbor 主机生成证书
openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in registry.harbor.com.csr \
-out registry.harbor.com.crt
# 转换 registry.harbor.com.crt 为 registry.harbor.com.cert,供 Docker 使用
openssl x509 -inform PEM -in registry.harbor.com.crt -out registry.harbor.com.cert
# 将服务器证书、密钥和 CA 文件复制到 Harbor 主机上的 Docker 证书文件夹中
mkdir -p /etc/docker/certs.d/registry.harbor.com/
cp registry.harbor.com.cert /etc/docker/certs.d/registry.harbor.com/
cp registry.harbor.com.key /etc/docker/certs.d/registry.harbor.com/
cp ca.crt /etc/docker/certs.d/registry.harbor.com/
systemctl restart docker
2. 离线下载包
# github地址
wget https://githubfast.com/goharbor/harbor/releases/download/v2.11.1/harbor-offline-installer-v2.11.1.tgz
# wget https://cube-studio.oss-cn-hangzhou.aliyuncs.com/install/harbor-offline-installer-v2.11.1.tgz
# 解压
tar xf harbor-offline-installer-v2.11.1.tgz -C /usr/local/
cd /usr/local/harbor
cp harbor.yml.tmpl harbor.yml
3. 修改 harbor.yml(hostname、登录密码、https 证书位置)
vim harbor.yml:
hostname: registry.harbor.com
# 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: /root/ssl/registry.harbor.com.crt
private_key: /root/ssl/registry.harbor.com.key
# enable strong ssl ciphers (default: false)
# strong_ssl_ciphers: false
data_volume: /data #这个路径是宿主机的路径,根据实际情况修改成空间大的地方
# external_url: http://xx.xx.xx.xx:88 # 如果有公网地址,这里填写
4. 执行部署
./install.sh
# 每次修改了配置文件都要删除之前的配置,重新安装
cd /usr/local/harbor
rm -rf ./common/config
./install.sh
5. 配置 Harbor 开机自启
cat > /usr/lib/systemd/system/harbor.service << 'EOF'
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor
[Service]
Type=simple
Restart=on-failure
RestartSec=5
Environment=harbor_install_path=/data/packages
ExecStart=/usr/local/bin/docker-compose -f ${harbor_install_path}/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f ${harbor_install_path}/harbor/docker-compose.yml down
[Install]
WantedBy=multi-user.target
EOF
注意:上面 systemd 单元里的
harbor_install_path=/data/packages(即docker-compose.yml路径为/data/packages/harbor/docker-compose.yml)与前文解压目录/usr/local/harbor不一致,请根据实际安装路径调整,见install/kubernetes/harbor/https-harbor.md:136-138。