搭建一个公网的Harbor镜像仓库

提前准备:

     1. 已准备好一台公网云服务器,例如Ip是:1.2.3.4
     2. 服务器上已成功安装docker、docker-compose
     -- apt update
     -- apt install docker.io docker-compose 

步骤一:

      服务器端下载harbor软件包,并解压
wget https://github.com/goharbor/harbor/releases/download/v2.7.2/harbor-offline-installer-v2.7.2.tgz

步骤二:

       修改harbor安装配置文件
cp harbor.yml.tmpl harbor.yml
vi harbor.yml
// 主要需要修改hostname,要是对外暴露的是域名访问,这里修改为域名,要是内网主机名访问,可以修改为主机名
hostname: 1.2.3.4
// 修改https证书配置,暴露在公网推荐https方式,可以openssl生成证书,这里不做详细介绍。
// 使用http方式,需要注释掉https认证,不然会安装报错
// 修改/data 目录

步骤三:

安装harbor,等待安装完成success

./install.sh --with-chartmuseum

步骤四:

安装完成即可web端登录(默认端口80,可进行修改),默认账号密码是admin/Harbor12345,可以在安装配置文件进行修改

http://1.2.3.4/harbor/projects/2/repositories

步骤五:

在web中根据创建项目

步骤六:

        客户端服务器推送镜像(例如我找的是一个有外网+权限的海外机器)
// 1. 客户端服务器已安装docker,并拉取需要的镜像到本地
docker pull nginx
docker tag nginx 1.2.3.4/{项目名称}/nginx:latest
// 2. 客户端服务器docker配置信任服务端镜像仓库
cat /etc/docker/daemon.json
root@hcss-ecs-8403:~# cat /etc/docker/daemon.json
{
    "insecure-registries": ["1.2.3.4"]
}
// 3. 登录到镜像仓库服务端
docker login 1.2.3.4
// 3.1 登录成功后会保存登录信息
[root@harbor01:~]# more /root/.docker/config.json
{
    "auths": {
        "1.2.3.4": {
            "auth": "YWRtaW46MQ=="
        }
    }
}
// 3.2 推送成功后,最好退出登录 docker logout 1.2.3.4
Removing login credentials for 1.2.3.4

[root@harbor01:~]# more /root/.docker/config.json
{
    "auths": {}
}
// 4. 推送镜像到harbor镜像仓库
docker push docker login 113.142.162.47

步骤七:

        其他客户端服务器拉取镜像
// 1. 需要先信任harbor主机服务器,配置信任后重启docker(拉取镜像不需要登录)
root@hcss-ecs-8403:~# cat /etc/docker/daemon.json
{
    "insecure-registries": ["1.2.3.4"]
}
// 2. 拉取镜像(镜像地址可以在harbor web端直接拷贝)
docker pull 1.2.3.4/mingfu-container-cloud/nginx

踩坑记录:

  1. k8s 1.30版本后默认是containerd,则在拉取镜像时只修改了/etc/docker/daemon.josn并没有用,需要修改containerd的配置(/etc/containerd/config.toml)

步骤八

上面的部分已经完整完成http的配置,但是在生产为了安全推荐使用tls,使用域名也更方便,下面是域名和tls的步骤
8.1 生成ca和服务端证书

# 创建证书相关数据的目录
[root@Ubuntu2204 ~]#mkdir -p /data/harbor/certs
[root@Ubuntu2204 ~]#cd /data/harbor/certs/

# 生成 ca 的私钥
[root@Ubuntu2204 certs]#openssl genrsa -out ca.key 4096

# 生成 ca 的自签名证书
[root@Ubuntu2204 certs]#openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=NanJing/L=NanJing/O=example/OU=Personal/CN=containercloud-mirror.xaidc.com" -key ca.key -out ca.crt

# 生成 harbor 主机的私钥
[root@Ubuntu2204 certs]#openssl genrsa -out containercloud-mirror.xaidc.com.key 4096

# 生成 harbor 主机的证书申请
[root@Ubuntu2204 certs]#openssl req -sha512 -new -subj "/C=CN/ST=NanJing/L=NanJing/O=example/OU=Personal/CN=containercloud-mirror.xaidc.com" -key containercloud-mirror.xaidc.com.key -out containercloud-mirror.xaidc.com.csr

# 创建 x509 v3 扩展文件(新版新增加的要求)
[root@Ubuntu2204 certs]#cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=containercloud-mirror.xaidc.com   # 此处必须和 harbor 的网站名称一致
EOF

# 给 harbor 主机颁发证书
[root@Ubuntu2204 certs]#openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in containercloud-mirror.xaidc.com.csr -out containercloud-mirror.xaidc.com.crt


# 生成的文件
[root@Ubuntu2204 certs]#ls
ca.crt  ca.key  containercloud-mirror.xaidc.com.crt  containercloud-mirror.xaidc.com.csr  containercloud-mirror.xaidc.com.key  v3.ext

8.2 配置证书和域名
root@k8s-master-01:/home/ubuntu/harbor# cat harbor.yml

# Configuration file of Harbor

# 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: 113.142.162.47
hostname: containercloud-mirror.xaidc.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: /data/harbor/certs/containercloud-mirror.xaidc.com.crt
  private_key: /data/harbor/certs/containercloud-mirror.xaidc.com.key

8.3 重新加载配置,并重启

[root@Ubuntu2204 harbor]#./prepare
[root@Ubuntu2204 harbor]#docker-compose down -v
[root@Ubuntu2204 harbor]#docker-compose up -d

8.4 客户端配置证书信任
8.4.1 docker配置
docker只需创建目录/etc/docker/certs.d/containercloud-mirror.xaidc.com,并在下面复制ca.crt即可(或者skip校验)
8.4.2 containerd配置cat /etc/containerd/config.toml(配置后k8s可以正常拉镜像,ctr拉镜像还是继续证书报错。。。)

      [plugins."io.containerd.grpc.v1.cri".registry.auths]
      [plugins."io.containerd.grpc.v1.cri".registry.configs]
        [plugins."io.containerd.grpc.v1.cri".registry.configs."containercloud-mirror.xaidc.com".tls]
          insecure_skip_verify = true
      [plugins."io.containerd.grpc.v1.cri".registry.headers]
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容