Harbor 仓库部署(docker-compose 部署方式)

发布于:2025-02-10 ⋅ 阅读:(44) ⋅ 点赞:(0)

一、 安装的前提条件

硬件

资源

最低

推荐

cpu 2C 4C
内存 4G 8G
硬盘 40G 160G

软件

软件

版本

描述

docker v17.0.6-ce+

安装参考官方文档

Install Docker Engine | Docker Documentation

docker-compose v1.18.0+

安装参考官方文档

Overview | Docker Documentation

Openssl latest 用于为harbor生成证书和密钥

二、安装配置

生成harbor自签证书和密钥

docker 在docker login 时默认使用 https 的方式连接到镜像仓库,修改docker连接方式需要重启docker,生产环境不允许,所以要提前为harbor 准备证书和密钥。

1.生成 CA 证书私钥

$ openssl genrsa -out ca.key 1024

2.生成 CA 证书

$ openssl req -x509 -new -nodes -sha512 -days 3650  -subj "/C=US/ST=TestState/L=TestCity/O=TestOrg/OU=TestOU/CN=Test"  -key ca.key  -out ca.crt

3.生成私钥

$ openssl genrsa -out test.harbor.com.key 1024

4.生成证书签名请求

$ openssl req -sha512 -new \
    -subj "/C=US/ST=TestState/L=TestCity/O=TestOrg/OU=TestOU/CN=Test" \
    -key test.harbor.com.key \
    -out test.harbor.com.csr

5.生成 x509 v3扩展文件

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

[alt_names]
DNS.1=test.harbor.com
DNS.2=test.harbor
DNS.3=harbor

6.使用该v3.ext文件为的Harbor主机生成证书

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

7.提供证书给Harbor和Docker

将服务器的证书和密钥复制到harbor主机证书文件夹中(/etc/docker/certs.d/)

将crt文件 解释为CA证书(cert)

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

将服务器证书、密钥和CA文件复制到Docker证书文件夹中

$ cp test.harbor.com.cert /etc/docker/certs.d/test.harbor.com/
$ cp test.harbor.com.key /etc/docker/certs.d/test.harbor.com/
$ cp ca.crt /etc/docker/certs.d/test.harbor.com/
安装配置Harbor

安装目录为 /opt/harbor 目录

1. 将离线安装包mv至安装目录并解压

$ mv /tmp/harbor-offline-installer-v2.3.5.tgz /opt/harbor
$ cd /opt/harbor && tar -vxf harbor-offline-installer-v2.3.5.tgz
harbor/harbor.v2.3.5.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl

2.copy原来的配置文件harbor.yml.tmpl 为harbor.yml,并修改harbor的配置文件

$ cd harbor && cp harbor.yml.tmpl harbor.yml

修改的地方有:

$ vim harbor.yml
hostname: test.harbor.com
http:
  port: 80
https:
  port: 443
  certificate: /path/test.harbor.com.cert
  private_key: /path/test.harbor.com.key
harbor_admin_password: xxxxx
database:
  password: xxxxx
data_volume: /opt/harbor/data

3.修改完后启动harbor,配置文件修改后都得先执行 ./prepare 安装执行 ./install

$ ./prepare
$ ./install

docker-compose 常用命令

# docker-compose 命令需要在模板文件(docker-compose.yml)所在目录执行才有效
docker-compose up -d      # 启动服务(后台运行)
docker-compose start      # 启动服务
docker-compose stop       # 停止正在运行的服务
docker-compose down       # 停止和删除容器、网络、卷、镜像等(参数 -v 会将数据卷一起删除)
docker-compose restart    # 重启服务
docker-compose ps         # 查看服务
docker-compose top        # 查看服务中运行的进程

三、Harbor 加入开机自启

$ vim /usr/lib/systemd/system/harbor.service
[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
# docker-compose.yml文件为harbor启动文件,应使用绝对路径
ExecStart=/bin/docker-compose -f /opt/harbor/harbor/docker-compose.yml up -d
ExecStop=/bin/docker-compose -f /opt/harbor/harbor/docker-compose.yml down
[Install]
WantedBy=multi-user.target

$ chmod +x  /usr/lib/systemd/system/harbor.service
$  systemctl daemon-reload
$  systemctl start harbor
$  systemctl enable harbor
$  systemctl status harbor

四、测试连接Harbor私有仓库

不建议添加为Docker私有仓库地址,因为需要重启docker才可生效,要登录harbor仓库中时可进行以下操作:

$ vim /etc/hosts
10.40.x.x test.harbor.com
$ docker login test.harbor.com
$ docker pull images


网站公告

今日签到

点亮在社区的每一天
去签到