k8s

harbor proxy cache 强制使用本地缓存

目标

使用proxy代理远端仓库时,即使远端仓库镜像删除了,本地也能正常拉取到镜像。防止许久不更新的镜像被远端仓库清理策略清理掉,导致本地服务重启时拉取不到镜像。

(PS:可以直接使用我的镜像:wgh9626/harbor:v2.8.2)

环境

docker:19.03.15
docker-compose:v2.12.2
harbor:v2.8.2

步骤

访问harbor github:https://github.com/goharbor/harbor/tree/v2.8.2

修改源代码

新建Codespaces

使用gitpod或者github codespaces,不然在make的时候会无法下载镜像。(本地机器无法访问github等地址时)

file

切换分支

git checkout v2.8.2 -b v2.8.2

file

修改源码

远程镜像仓库找不到镜像时,使⽤本地缓存。

vim src/controller/proxy/controller.go
# 注释掉删除本地清单与远程仓库镜像tag保持同步的逻辑
# 添加直接正常返回,继续拉取本地缓存镜像的处理⽅法
return true, nil, nil

查看差异

git diff

file

编译代码

编译harbor-core

make compile_core versions_prepare -e DEVFLAG=false
# 需要加上versions_prepare,不然下面build的时候会报错versions文件不存在。

file

file

file

打包镜像

把wgh9626换成你自己的dockerhub地址。

# 使用官方goharbor/harbor-core-base:v2.8.2 作为打包基础镜像
docker build -f make/photon/core/Dockerfile --build-arg harbor_base_namespace=goharbor --build-arg harbor_base_image_version=v2.8.2 . -t wgh9626/harbor:v2.8.2

file

file

推送镜像

docker login docker.io/wgh9626
docker push docker.io/wgh9626/harbor:v2.8.2

这里推送会报错:unauthorized:access token has insufficient scopes

file

需要先docker logout,再docker login,不用加wgh9626。

重启harbor

docker pull docker.io/wgh9626/harbor:v2.8.2
vim docker-compose.yml
services:
 core:
 # image: goharbor/harbor-core:v2.8.2
 image: docker.io/wgh9626/harbor:v2.8.2

docker-compose up -d

测试

推送镜像到远端仓库

docker tag b5af743e5984 xxx/local-path-provisioner:v0.0.23
docker push xxx/local-path-provisioner:v0.0.23

创建远端仓库

file

添加镜像代理

cache层harbor拉取镜像

docker pull harbor.harbor.com/proxy/local-path-provisioner:v0.0.23

删除远端和cache层镜像

docker rmi harbor.harbor.com/proxy/local-path-provisioner:v0.0.23

cache层再次拉取

拉取成功。验证成功。

docker pull harbor.harbor.com/proxy/local-path-provisioner:v0.0.23

file

harbor自带的cache功能

文档地址:https://github.com/goharbor/perf/wiki/Cache-layer

在harbor.yml中可以配置是否启用cache。默认是禁用的,推荐启用。

cache:
  # not enabled by default
  enabled: true
  # keep cache for one day by default
  expire_hours: 24

file

当用户向 Harbor 核心发出请求时

  • Harbor 将检查 Redis 缓存中请求的资源。如果在缓存中找到请求的 Harbor 资源,则 Harbor 将返回该请求将缓存的资源。

  • 如果请求的 Harbor 资源不在缓存中,则 Harbor 将启动其正常的检索过程,方法是从 Harbor 数据库中检索 Harbor 资源(如果请求了项目、项目、project_metadata或存储库)或 Harbor 分发(如果请求了manifest)。

  • 然后将 Harbor 资源写回 Redis 缓存以供将来请求,并将请求的资源返回给用户。

分类: k8s
0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部
0
希望看到您的想法,请您发表评论x