自学内容网 自学内容网

Harbor镜像仓库安装配置及使用

一、环境准备

Harbor 是一个开源的企业级 Docker 镜像 仓库,提供了许多功能,包括用户管理、访问控制、镜像复制和安全扫描。

官方文档部署手册:Harbor docs

下载地址:Releases 

官方给出的最低配置和推荐配置如下:

硬件配置

软件配置

网络配置

本文使用虚拟机配置

主机名IP系统软件版本配置信息
k8s-harbor10.10.181.210
CentOS Linux release 7.9.2009 (Core)

Docker version 26.1.4

Docker Compose version v2.29.7

2核4G,磁盘48G

Harbor:v2.11.0

可以从官方发布页面下载 Harbor 安装程序。下载在线安装程序或离线安装程序。
 
在线安装程序:在线安装程序从 Docker 中心下载 Harbor 镜像。因此,安装程序的尺寸非常小。

离线安装程序:如果要部署 Harbor 的主机没有连接到 Internet,请使用离线安装程序。离线安装程序包含预先构建的映像,因此它比在线安装程序大。

在线和离线安装程序的安装过程几乎相同。推荐使用离线安装。

二、部署安装

1、安装docker

查看有没有安装过旧版

yum list installed | grep docker

没有出现任何东西就表示没有安装过docker,可以直接去安装了

如果出现以下代表安装过docker,需要进行卸载

卸载Docker旧版本

yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

或者
yum remove -y docker*

设置阿里云镜像

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

如果设置时报如下错:

-bash: yum-config-manager: command not found,

是因为没有安装这个命令,这个命令在yum-utils 包里,安装命令如下:

yum install -y yum-utils device-mapper-persistent-data lvm2

安装成功后再次设置就可以了。

查看所有docker

yum list docker-ce --showduplicates | sort -r

安装最新版本Docker

yum install -y docker-ce docker-ce-cli containerd.io

安装成功!

安装指定版本

比如我要安装23.0.6这个版本

#安装docker
yum install docker-ce-23.0.6-1.el7 containerd.io

#设置启动和开机启动
systemctl start docker & systemctl enable docker

#查看docker状态
systemctl status docker

2、安装docker-compose

先到GitHub下载 docker-compose

如果最新版没有这个安装文件,就往下一版找,直到找到这个文件。

上传到 /usr/local/bin 目录,不需要解压

重命名为docker-compose

mv docker-compose-linux-x86_64 docker-compose

查看版本号

docker-compose version

如果出现如下错误说明没有可执行权限

添加可执行权限,命令如下:

chmod +x /usr/local/bin/docker-compose

安装完成!

3、安装harbor

将下载的harbor软件包上传至虚拟机

解压

[root@k8s-harbor ~]# tar -zxvf harbor-offline-installer-v2.11.0.tgz
harbor/harbor.v2.11.0.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@k8s-harbor ~]# ll
总用量 643728
-rw-------. 1 root root      1260 8月  26 17:28 anaconda-ks.cfg
drwxr-xr-x. 2 root root       123 9月  30 16:06 harbor
-rw-r--r--. 1 root root 659171069 9月  30 16:04 harbor-offline-installer-v2.11.0.tgz
[root@k8s-harbor ~]# ll harbor
总用量 648524
-rw-r--r--. 1 root root      3646 6月   4 18:41 common.sh
-rw-r--r--. 1 root root 664046766 6月   4 18:41 harbor.v2.11.0.tar.gz
-rw-r--r--. 1 root root     14270 6月   4 18:41 harbor.yml.tmpl
-rwxr-xr-x. 1 root root      1975 6月   4 18:41 install.sh
-rw-r--r--. 1 root root     11347 6月   4 18:41 LICENSE
-rwxr-xr-x. 1 root root      1882 6月   4 18:41 prepare

将 harbor.yml.tmpl 拷贝一份并命名为 harbor.yml

[root@k8s-harbor ~]# cd harbor
[root@k8s-harbor harbor]# ll
总用量 648524
-rw-r--r--. 1 root root      3646 6月   4 18:41 common.sh
-rw-r--r--. 1 root root 664046766 6月   4 18:41 harbor.v2.11.0.tar.gz
-rw-r--r--. 1 root root     14270 6月   4 18:41 harbor.yml.tmpl
-rwxr-xr-x. 1 root root      1975 6月   4 18:41 install.sh
-rw-r--r--. 1 root root     11347 6月   4 18:41 LICENSE
-rwxr-xr-x. 1 root root      1882 6月   4 18:41 prepare
[root@k8s-harbor harbor]# cp harbor.yml.tmpl harbor.yml
[root@k8s-harbor harbor]# ll
总用量 648540
-rw-r--r--. 1 root root      3646 6月   4 18:41 common.sh
-rw-r--r--. 1 root root 664046766 6月   4 18:41 harbor.v2.11.0.tar.gz
-rw-r--r--. 1 root root     14270 9月  30 16:11 harbor.yml
-rw-r--r--. 1 root root     14270 6月   4 18:41 harbor.yml.tmpl
-rwxr-xr-x. 1 root root      1975 6月   4 18:41 install.sh
-rw-r--r--. 1 root root     11347 6月   4 18:41 LICENSE
-rwxr-xr-x. 1 root root      1882 6月   4 18:41 prepare

现在我根据需要对该文件进行设定修改,hostname参数直接使用IP;这里因为https用不到,为避免学习麻烦先注释,有需要再加,修改见图:

然后继续用命令执行安装

[root@k8s-harbor harbor]# ./install.sh

看到下图中箭头所指的successfully就部署成功了。

浏览器访问IP即可,例如我这里就访问 http://10.10.181.210/

如果配置文件中自己没有修改,默认用户名:admin   默认密码:Harbor12345

4、设置开机启动

Docker compose 不会伴随docker的启动而启动,需要进行一下设置

执行下述代码即可,如果你的docker-compose.yml目录位置和我不一样,你需要对下面这段代码路径进行自主修改。

cat >/usr/lib/systemd/system/harbor.service <<EOF
[Unit]
Description=Harbor service with docker-compose
Requires=docker.service
After=docker.service
[Service]
Restart=always
RemainAfterExit=yes
StandardError=null
StandardOutput=null
WorkingDirectory=/root/harbor
ExecStartPre=/usr/bin/docker compose -f /root/harbor/docker-compose.yml down
ExecStart=/usr/bin/docker compose -f /root/harbor/docker-compose.yml up -d
ExecStop=/usr/bin/docker compose -f /root/harbor/docker-compose.yml down
[Install]
WantedBy=multi-user.target
EOF

重新加载 systemd 的配置并设置自启动。

systemctl daemon-reload && systemctl enable harbor.service

重启docker测试harbor会不会正常运行

[root@k8s-harbor ~]# systemctl restart docker

需等待一会验证,下面这样就是ok了。

[root@k8s-harbor ~]# docker ps
CONTAINER ID   IMAGE                                 COMMAND                   CREATED          STATUS                    PORTS                                                                            NAMES
77d7b2ac98e8   goharbor/harbor-jobservice:v2.11.0    "/harbor/entrypoint.…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    harbor-jobservice
d2e289f84f90   goharbor/nginx-photon:v2.11.0         "nginx -g 'daemon of…"   10 minutes ago   Up 10 minutes (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp   nginx
3aa0e0712ffb   goharbor/harbor-core:v2.11.0          "/harbor/entrypoint.…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    harbor-core
6d3cbfbdfc43   goharbor/redis-photon:v2.11.0         "redis-server /etc/r…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    redis
71e132f32ef5   goharbor/harbor-portal:v2.11.0        "nginx -g 'daemon of…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    harbor-portal
89e29f23be7c   goharbor/registry-photon:v2.11.0      "/home/harbor/entryp…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    registry
8d0fab7d0559   goharbor/harbor-registryctl:v2.11.0   "/home/harbor/start.…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    registryctl
cca133ebe602   goharbor/harbor-db:v2.11.0            "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes (healthy)                                                                                    harbor-db
9235952a796e   goharbor/harbor-log:v2.11.0           "/bin/sh -c /usr/loc…"   10 minutes ago   Up 10 minutes (healthy)   127.0.0.1:1514->10514/tcp                                                        harbor-log

三、基础使用

1、创建一个用户

2、docker登录用户

直接在docker登陆是必要用443端口的https,那么如果我们想用80端口的http,需要额外配置insecure-registries,如果其他服务想登录harbor服务器,需要也需要配置insecure-registries,如下

[root@k8s-harbor ~]# vi /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://do.nark.eu.org",
    "https://dc.j8.work",
    "https://pilvpemn.mirror.aliyuncs.com",
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "insecure-registries": ["10.10.181.210"]
}

重启docker

# 前面配置systemd 的管理harbor,配置了对docker启动而启动,因此重启docker,harbor也会重启。
[root@harbor ~]# systemctl restart docker

登录

[root@k8s-harbor ~]# docker login 10.10.181.210

3、创建项目

给这个项目添加使用用户

往仓库推送镜像

#给镜像打上标记
[root@k8s-node1 ~]# docker push 10.10.181.210/public/my-nginx-test:v1.0.1

#往镜像仓库推送镜像
[root@k8s-node1 ~]# docker push 10.10.181.210/public/my-nginx-test:v1.0.1
The push refers to repository [10.10.181.210/public/my-nginx-test]
7bf3eb1a80e4: Pushed
43adef21ed65: Pushed
f7df5efb2c99: Pushed
5b316f9079a1: Pushed
5e19cd5b03d0: Pushed
678ea5c52c14: Pushed
8d853c8add5d: Pushed
v1.0.1: digest: sha256:f41b7d70c5779beba4a570ca861f788d480156321de2876ce479e072fb0246f1 size: 1778

镜像推送成功!

从镜像仓库拉取镜像

[root@k8s-node1 ~]# docker pull 10.10.181.210/public/my-nginx-test:v1.0.1

四、 自制证书配置HTTPS

删除上面在docker配置的   "insecure-registries": ["http://10.10.181.210"]

[root@k8s-harbor harbor]# vi /etc/docker/daemon.json
 
#删除这行  "insecure-registries": ["http://10.10.181.210"]

删除后见下图样式

创建脚本文件

[root@k8s-harbor ~]# cd harbor
[root@k8s-harbor harbor]# vi generate_cert.sh
#!/bin/bash
 
# 提示用户输入信息
read -p "请输入 IP 地址 (如果没有请留空): " IP_ADDRESS
read -p "请输入域名 (如果没有请留空): " DOMAIN
read -p "请输入证书存储目录 (例如 /root/harbor/certs): " CERT_DIR
 
# 设置证书文件路径
CERT_KEY="${CERT_DIR}/harbor.key"
CERT_CRT="${CERT_DIR}/harbor.crt"
CSR_FILE="${CERT_DIR}/harbor.csr"
REQ_FILE="${CERT_DIR}/harbor.req"
 
# 创建证书存放目录(如果不存在)
mkdir -p $CERT_DIR
 
# 生成私钥
openssl genrsa -out $CERT_KEY 4096
 
# 生成证书请求配置文件
cat > $REQ_FILE <<EOF
[req]
default_bits       = 4096
default_keyfile    = $CERT_KEY
default_md         = sha256
default_country    = CN
default_state      = SHANXI
default_city       = XIAN
default_org        = DEVOPS
default_email      = email@example.com
default_commonname = $DOMAIN
req_extensions     = req_ext
distinguished_name = req_distinguished_name
[req_distinguished_name]
countryName                = Country Name (2 letter code)
countryName_default        = CN
stateOrProvinceName        = State or Province Name (full name)
stateOrProvinceName_default= SHANXI
localityName               = Locality Name (eg, city)
localityName_default       = XIAN
organizationName           = Organization Name (eg, company)
organizationName_default   = DEVOPS
emailAddress               = Email Address
emailAddress_default       = email@example.com
emailAddress_max           = 64
[req_ext]
subjectAltName = @alt_names
[alt_names]
EOF
 
if [ -n "$IP_ADDRESS" ]; then
    echo "IP.1 = $IP_ADDRESS" >> $REQ_FILE
fi
 
if [ -n "$DOMAIN" ]; then
    echo "DNS.1 = $DOMAIN" >> $REQ_FILE
fi
 
# 生成证书请求(CSR)
openssl req -new -key $CERT_KEY -out $CSR_FILE -config $REQ_FILE
 
# 生成自签名证书
openssl x509 -req -in $CSR_FILE -signkey $CERT_KEY -out $CERT_CRT -days 365 -extfile $REQ_FILE -extensions req_ext
 
# 如果需要保留临时文件,请注释掉下一行
# 清理临时文件
rm $REQ_FILE $CSR_FILE
 
echo "证书生成完成:"
echo "私钥: $CERT_KEY"
echo "证书: $CERT_CRT"

这里我直接用IP生成了,没使用域名

#给脚本赋予权限
[root@harbor harbor]# chmod +x generate_cert.sh
#执行脚本
[root@harbor harbor]# ./generate_cert.sh

此命令将在 ./certs 目录中生成以下文件:

  • harbor.key(私钥)
  • harbor.crt(证书)

编辑Harbor配置https

[root@k8s-harbor harbor]# vi harbor.yml
https:
  port: 443
  certificate: /root/harbor/certs/harbor.crt
  private_key: /root/harbor/certs/harbor.key

【踩坑】生成证书报错

139938439645072:error:0D07A097:asn1 encoding routines:ASN1_mbstring_ncopy:string too long:a_mbstr.c:158:maxsize=2

解决办法

【So I figured out that I've written some string which should have been 2 characters long (maxsize=2), but happened way longer. I returned back to my config file and quickly found that I've wrote the long name of the country, instead of the 2-character code. This solved my problem.】

重启docker,因为前面配置了harbor启动方式,重启docker,harbor也会重启。

[root@k8s-harbor harbor]# systemctl daemon-reload
[root@k8s-harbor harbor]# systemctl restart docker

重启harbor,因为前面配置了systemd的方式,可以通过systemctl命令重启。

[root@k8s-harbor harbor]# systemctl restart harbor
#或
[root@k8s-harbor harbor]# ./install.sh 

完成!

将 Harbor 服务器的自签名证书(CA 证书)放置在正确的位置,以便 Docker 客户端可以找到并信任它。

[root@k8s-harbor harbor]# mkdir -p /etc/docker/certs.d/10.10.181.210/
[root@k8s-harbor harbor]# cp /root/harbor/certs/harbor.crt /etc/docker/certs.d/10.10.181.210/ca.crt
[root@k8s-harbor harbor]# systemctl restart docker
[root@k8s-harbor harbor]# systemctl restart harbor

使用 openssl 工具检查证书是否被正确安装和配置

[root@harbor harbor]# openssl s_client -connect 10.10.181.210:443 -CAfile /etc/docker/certs.d/10.10.181.210/ca.crt

登录

[root@k8s-harbor harbor]# docker login 10.10.181.210
Authenticating with existing credentials...
Stored credentials invalid or expired
Username (admin): admin      #输入harbor用户名
Password:                       #输入用户名对应的密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credential-stores
 
Login Succeeded

切换登录用户的方法:

# 删除 Docker 配置文件中的凭据
[root@k8s-harbor harbor]# rm ~/.docker/config.json
rm: remove regular file '/root/.docker/config.json'? yes
[root@k8s-harbor harbor]# docker login 10.10.181.210
Username: admin      #登录的此用户需要在harbor中有
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credential-stores
 
Login Succeeded


原文地址:https://blog.csdn.net/zxz547388910/article/details/142639513

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!