Cloudpods docker部署harbor镜像仓库
docker部署harbor镜像仓库
docker容器应用的开发和运行离不开可靠的镜像管理,虽然docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。之前介绍了docker私有仓库Registry,这里介绍另一款企业级docker镜像仓库Harbor的部署和使用,在Kubernetes集群中,推荐使用hdrbor仓库环境...
编辑 韩帅平 · 2020-05-02 11:08:29 发布
Docker私有仓库Harbor介绍和部署记录
Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry
也是非常必要的。之前介绍了Docker私有仓库Registry,这里介绍另一款企业级Docker镜像仓库Harbor的部署和使用,在Kubernetes集群中,推荐使用Harbor仓库环境。
一、Harbor仓库介绍
我们在日常Docker容器使用和管理过程中,渐渐发现部署企业私有仓库往往是很有必要的, 它可以帮助你管理企业的一些敏感镜像, 同时由于Docker Hub的下载速度和GFW的原因, 往往需要将一些无法直接下载的镜像导入本地私有仓库. 而Harbor就是部署企业私有仓库的一个不二之选。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,Harbor主要提供Dcoker Registry管理UI,提供的功能包括:基于角色访问的控制权限管理(RBAC)、AD/LDAP集成、日志审核、管理界面、自我注册、镜像复制和中文支持等。Harbor的目标是帮助用户迅速搭建一个企业级的Docker registry服务。它以Docker公司开源的registry为基础,额外提供了如下功能:
-> 基于角色的访问控制(Role Based Access Control)
-> 基于策略的镜像复制(Policy based image replication)
-> 镜像的漏洞扫描(Vulnerability Scanning)
-> AD/LDAP集成(LDAP/AD support)
-> 镜像的删除和空间清理(Image deletion & garbage collection)
-> 友好的管理UI(Graphical user portal)
-> 审计日志(Audit logging)
-> RESTful API
-> 部署简单(Easy deployment)
Harbor的所有组件都在Dcoker中部署,所以Harbor可使用Docker Compose快速部署。需要特别注意:由于Harbor是基于Docker Registry V2版本,所以docker必须大于等于1.10.0版本,docker-compose必须要大于1.6.0版本!
二、Harbor仓库结构
Harbor的每个组件都是以Docker容器的形式构建的,可以使用Docker Compose来进行部署。如果环境中使用了kubernetes,Harbor也提供了kubernetes的配置文件。Harbor大概需要以下几个容器组成:ui(Harbor的核心服务)、log(运行着rsyslog的容器,进行日志收集)、mysql(由官方mysql镜像构成的数据库容器)、Nginx(使用Nginx做反向代理)、registry(官方的Docker registry)、adminserver(Harbor的配置数据管理器)、jobservice(Harbor的任务管理服务)、redis(用于存储session)。
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,整体架构还是很清晰的。下面借用了网上的架构图:
Harbor依赖的外部组件
-> Nginx(即Proxy代理层): Nginx前端代理,主要用于分发前端页面ui访问和镜像上传和下载流量; Harbor的registry,UI,token等服务,通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务。
-> Registry v2: 镜像仓库,负责存储镜像文件; Docker官方镜像仓库, 负责储存Docker镜像,并处理docker push/pull命令。由于我们要对用户进行访问控制,即不同用户对Docker image有不同的读写权限,Registry会指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token, Registry会通过公钥对token进行解密验证。
-> Database(MySQL或Postgresql):为core services提供数据库服务,负责储存用户权限、审计日志、Docker image分组信息等数据。
Harbor自有组件
-> Core services(Admin Server): 这是Harbor的核心功能,主要提供以下服务:
-> UI:提供图形化界面,帮助用户管理registry上的镜像(image), 并对用户进行授权。
-> webhook:为了及时获取registry 上image状态变化的情况, 在Registry上配置webhook,把状态变化传递给UI模块。
-> Auth服务:负责根据用户权限给每个docker push/pull命令签发token. Docker 客户端向Regiøstry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。
-> API: 提供Harbor RESTful API
-> Replication Job Service:提供多个 Harbor 实例之间的镜像同步功能。
-> Log collector:为了帮助监控Harbor运行,负责收集其他组件的log,供日后进行分析。
再来仔细看下Harbor主要组件和数据流走向:
-> proxy,它是一个nginx前端代理,主要是分发前端页面ui访问和镜像上传和下载流量,上图中通过深蓝色先标识;
-> ui提供了一个web管理页面,当然还包括了一个前端页面和后端API,底层使用mysql数据库;
-> registry是镜像仓库,负责存储镜像文件,当镜像上传完毕后通过hook通知ui创建repository,上图通过红色线标识,当然registry的token认证也是通过ui组件完成;
-> adminserver是系统的配置管理中心附带检查存储用量,ui和jobserver启动时候回需要加载adminserver的配置,通过灰色线标识;
-> jobsevice是负责镜像复制工作的,他和registry通信,从一个registry pull镜像然后push到另一个registry,并记录job_log,上图通过紫色线标识;
-> log是日志汇总组件,通过docker的log-driver把日志汇总到一起,通过浅蓝色线条标识。
Harbor的误区
误区一: Harbor是负责存储容器镜像的 (Harbor是镜像仓库,那么它就应当是存储镜像的)
其实关于镜像的存储,Harbor使用的是官方的docker registry服务去完成,至于registry是用本地存储或者s3都是可以的,Harbor的功能是在此之上提供用户权限管理、镜像复制等功能,提高使用的registry的效率。
误区二:Harbor镜像复制是存储直接复制 (镜像的复制,很多人以为应该是镜像分层文件的直接拷贝)
其实Harbor镜像复制采用了一个更加通用、高屋建瓴的做法,通过docker registry 的API去拷贝,这不是省事,这种做法屏蔽了繁琐的底层文件操作、不仅可以利用现有docker registry功能不必重复造轮子,而且可以解决冲突和一致性的问题。
Harbor的部署
这里不建议使用kubernetes来部署, 原因是镜像仓库非常重要, 尽量保证部署和维护的简洁性, 因此这里直接使用compose的方式进行部署。官方提供3种部署Harbor的方式:
1)在线安装: 从Docker Hub下载Harbor的镜像来安装, 由于Docker Hub比较慢, 建议Docker配置好加速器。
2)离线安装: 这种方式应对与部署主机没联网的情况使用。需要提前下载离线安装包: harbor-offline-installer-.tgz 到本地
3)OVA安装: 这个主要用vCentor环境是使用
后面部署时会为Docker配置镜像加速器, 因此会采用在线部署的方式, 部署步骤如下:
-> 下载Harbor最新的在线安装包
-> 配置Harbor (harbor.cfg)
-> 运行install.sh来安装和启动Harbor
-> Harbor的日志路径:/var/log/harbor
Harbor仓库部署的官方要求的最小系统配置
-> 2个cpu
-> 4g内存
-> 40g硬盘,因为是存储镜像的所以推荐硬盘大点。
参考
Harbor GitHub: https://github.com/goharbor/harbor
Harbor 安装手册: https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md
Harbor 用户手册: https://github.com/goharbor/harbor/blob/master/docs/user_guide.md
三、Harbor仓库环境部署记录
3.1) 环境要求和准备工作
Harbor以容器的形式进行部署, 因此可以被部署到任何支持Docker的Linux发行版, 要使用Harbor,需要安装docker和docker-compose编排工具,并且具备如下环境: Python2.7+ Docker Engine 1.10+ Docker Compose 1.6.0+ 这里测试环境部署到Centos7.5机器上,如下: [root@lcalhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@lcalhost ~]# setenforce 0 setenforce: SELinux is disabled [root@lcalhost ~]# cat /etc/sysconfig/selinux ... SELINUX=disabled [root@lcalhost ~]# systemctl stop firewalld [root@lcalhost ~]# systemctl disable firewalld [root@lcalhost ~]# firewall-cmd --state not running centos7自带的python版本就是2.7.5 [root@lcalhost ~]# python --version Python 2.7.5
3.2) 安装Docker
更新yum包 [root@localhost ~]# yum update 卸载旧版本 Docker [root@localhost ~]# yum remove docker docker-common docker-selinux docker-engine 安装软件包 [root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 添加 Docker yum源 [root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo Loaded plugins: fastestmirror adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo 安装 Docker (直接yum按照docker-ce即可) 随着Docker的不断流行与发展,docker公司(或称为组织)也开启了商业化之路,Docker 从 17.03版本之后分为 CE(Community Edition) 和 EE(Enterprise Edition): 1) Docker EE由公司支持,可在经过认证的操作系统和云提供商中使用,并可运行来自Docker Store的、经过认证的容器和插件。 2) Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员和运维团队构建容器APP。 事实上,Docker CE 17.03,可理解为Docker 1.13.1的Bug修复版本。因此,从Docker 1.13升级到Docker CE 17.03风险相对是较小的。 [root@localhost ~]# yum -y install docker-ce 启动 Docker [root@localhost ~]# systemctl start docker [root@localhost ~]# systemctl enable docker [root@localhost ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2019-05-26 22:15:34 CST; 27min ago Docs: https://docs.docker.com Main PID: 15260 (dockerd) CGroup: /system.slice/docker.service └─15260 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock 查看 Docker 版本号 [root@localhost ~]# docker --version Docker version 18.09.6, build 481bc77156 [root@localhost ~]# docker version Client: Version: 18.09.6 API version: 1.39 Go version: go1.10.8 Git commit: 481bc77156 Built: Sat May 4 02:34:58 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.6 API version: 1.39 (minimum version 1.12) Go version: go1.10.8 Git commit: 481bc77 Built: Sat May 4 02:02:43 2019 OS/Arch: linux/amd64 Experimental: false
3.3) 安装Docker Compose
Docker Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器。 安装 epel-release [root@lcalhost ~]# yum install epel-release 安装 python-pip [root@lcalhost ~]# yum install -y python-pip 已安装: python2-pip.noarch 0:8.1.2-12.el7 完毕! 安装 docker-compose [root@lcalhost ~]# pip install docker-compose ......... SSuccessfully installed attrs-19.3.0 bcrypt-3.1.7 cached-property-1.5.1 certifi-2020.4.5.1 cffi-1.14.0 chardet-3.0.4 configparser-4.0.2 contextlib2-0.6.0.post1 cryptography-2.9.2 docker-4.2.0 docker-compose-1.25.5 dockerpty-0.4.1 docopt-0.6.2 functools32-3.2.3.post2 idna-2.9 importlib-metadata-1.6.0 jsonschema-3.2.0 paramiko-2.7.1 pathlib2-2.3.5 pynacl-1.3.0 pyrsistent-0.16.0 requests-2.23.0 scandir-1.10.0 subprocess32-3.5.4 urllib3-1.25.9 websocket-client-0.57.0 zipp-1.2.0 查看 docker-compose 版本号 [root@lcalhost ~]# docker-compose -version docker-compose version 1.25.5, build unknown [root@lcalhost ~]# pip freeze | grep compose docker-compose==1.25.5 安装 git [root@lcalhost ~]# yum install git
3.4) 为Docker配置加速器, 方便通过国内镜像服务器快速拉取Docker Hub提供的镜像
[root@lcalhost ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://l5s01364.mirror.aliyuncs.com"] }
3.5) 下载Harbor安装包,配置Harbor
到Harbor的GitHub仓库的Release页面(https://github.com/goharbor/harbor/releases), 下载最新的在线安装包(如果下载不了, 请从这里下载百度网盘Harbor(https://pan.baidu.com/s/1VyotH-m2uw9A97LXzwmSWA) 提取码:xu3j)。后续harbor若要升级,可参考这里(https://lyyao09.coding.me/2019/08/08/k8s/Harbor-upgrade-and-migration-guide/)。
这里分为在线和离线的版本,我下载的是1.10.2在线的版本
[root@lcalhost ~]# ll harbor-online-installer-v1.10.2.tgz -rw-r--r-- 1 root root 8486 4月 10 01:20 harbor-online-installer-v1.10.2.tgz [root@lcalhost ~]# tar -zxvf harbor-online-installer-v1.10.2.tgz harbor/prepare harbor/LICENSE harbor/install.sh harbor/common.sh harbor/harbor.yml [root@lcalhost ~]# cd harbor [root@lcalhost harbor]# ls common.sh harbor.yml install.sh LICENSE prepare 下载下来之后解压缩,目录下会有harbor.yaml (新版本是.yaml文件,之前版本是.conf 或者 .cfg文件),就是Harbor的配置文件了。 [root@lcalhost harbor]# cp harbor.yml harbor.yml.bak [root@lcalhost harbor]# vim harbor.yml [root@lcalhost harbor]# cat harbor.yml |grep -v "#"|grep -v "^$" hostname: 192.168.31.10 http: port: 213 harbor_admin_password: kevin@B0123 database: password: root123 max_idle_conns: 50 max_open_conns: 100 data_volume: /data clair: updaters_interval: 12 jobservice: max_job_workers: 10 notification: webhook_job_max_retry: 10 chart: absolute_url: disabled log: level: info local: rotate_count: 50 rotate_size: 200M location: /var/log/harbor _version: 1.10.0 proxy: http_proxy: https_proxy: no_proxy: components: - core - jobservice - clair 配置解释 hostname: 修改成Harbao部署机自身的ip地址 db_password: 这是postgresql数据库root密码 harbor_admin_password: harbor初始管理员密码为Harbor12345, 这里最好修改成自己的密码,默认密码至少8位,最好是大小写、数字和特殊字符。 配置完Harbor之后,接着进行安装启动Harbor,Harbor目录下有一个install.sh, 执行它来进行安装 [root@lcalhost harbor]# sh ./install.sh ........... ........... ✔ ----Harbor has been installed and started successfully.---- 安装完成后,会发现解压目录harbor下面多了一个docker-compose.yml文件,里面包含了harbor依赖的镜像和对应容器创建的信息 查看harbor对应容器信息(还可以执行"docker images"和"docker ps"查看harbor的镜像和容器情况) [root@lcalhost harbor]# docker-compose ps #"注意docker-compose"命令只能在当前harbor目录下使用(因为该目录下有harbor配置文件) Name Command State Ports --------------------------------------------------------------------------------------------- harbor-core /harbor/harbor_core Up (healthy) harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp harbor-jobservice /harbor/harbor_jobservice ... Up (healthy) harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp nginx nginx -g daemon off; Up (healthy) 0.0.0.0:213->8080/tcp redis redis-server /etc/redis.conf Up (healthy) 6379/tcp registry /home/harbor/entrypoint.sh Up (healthy) 5000/tcp registryctl /home/harbor/start.sh Up (healthy) 然后就可以访问harbor了,访问地址为:http://192.168.31.10:213 用户名为admin,密码为配置文件中定义的"kevin@BO123"
==== 这里需要注意一个Harbor 部署的坑点(Docker 18.09.1 及以上的版本,系统内核版本需要升级到4.4.x) ====
1)CentOS 7.x 系统自带的3.10.x内核存在一些Bugs,导致运行的Docker、Kubernetes不稳定。
2)高版本的 docker(1.13 以后) 启用了3.10 kernel实验支持的kernel memory account功能(无法关闭),当docker节点压力大 (如频繁启动和停止容器) 时会导致 cgroup memory leak;
3)Docker 18.09.1 及以上的版本,需要手动升级内核到 4.4.X 以上;
因为得出结论:
部署harbor的时候,要首先查看下本机的docker版本,如果docker版本在18.90.1以上,则需要手动升级内核版本到 4.4.x以上。
否则会出现:
harbor部署Harbor正常启动,端口正常监听,防火墙也已关闭,但是通过http://ip:80 访问不了harbor,并且/var/log/harbor目录下没有任何日志产生!! 使用" telnet ip 80"查看发现不通或者闪退!!!
手动修改系统内核版本可以参考:https://www.cnblogs.com/kevingrace/p/10961264.html
具体升级内核操作如下:
[root@localhost ~]# uname -r 3.10.0-862.el7.x86_64 [root@localhost ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装一次! [root@localhost ~]# yum --enablerepo=elrepo-kernel install -y kernel-lt 设置开机从新内核启动 [root@localhost ~]# grub2-set-default 0 重启机器 [root@localhost ~]# init 6 安装内核源文件(在升级完内核并重启机器后执行,也可以不用执行这一步。可选): [root@localhost ~]# yum --enablerepo=elrepo-kernel install kernel-lt-devel-$(uname -r) kernel-lt-headers-$(uname -r) [root@harbor-node ~]# uname -r 4.4.180-2.el7.elrepo.x86_64 ======================================================================================================================== 或者也可以采用下面升级内核的方法: # git clone --branch v1.14.1 --single-branch --depth 1 https://github.com/kubernetes/kubernetes # cd kubernetes # KUBE_GIT_VERSION=v1.14.1 ./build/run.sh make kubelet GOFLAGS="-tags=nokmem" # init 6
Harbor 服务的关闭和启动
1) Harbor的日志路径:var/log/harbor [root@lcalhost harbor]# cat harbor.yml | grep log log: # configs for logs in local storage # Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated. # Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes. # The directory on your host that store log location: /var/log/harbor [root@lcalhost harbor]# ls /var/log/harbor/ core.log jobservice.log portal.log postgresql.log proxy.log redis.log registryctl.log registry.log 2) 停止和关闭harbor命令: "docker-compose down -v" [root@lcalhost harbor]# docker-compose down -v Stopping harbor-jobservice ... done Stopping nginx ... done Stopping harbor-core ... done Stopping registry ... done Stopping harbor-db ... done Stopping harbor-portal ... done Stopping redis ... done Stopping registryctl ... done Stopping harbor-log ... done Removing harbor-jobservice ... done Removing nginx ... done Removing harbor-core ... done Removing registry ... done Removing harbor-db ... done Removing harbor-portal ... done Removing redis ... done Removing registryctl ... done Removing harbor-log ... done Removing network harbor_harbor [root@lcalhost harbor]# docker-compose ps Name Command State Ports ------------------------------ [root@lcalhost harbor]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 可以修改harbor配置文件,比如这里修改harbor的web登录端口,由80端口修改为8080端口 [root@lcalhost harbor]# vim harbor.yml http: port: 8080 然后将harbor修改的配置更新到 docker-compose.yml 文件 [root@lcalhost harbor]# ./prepare prepare base dir is set to /root/harbor WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https Clearing the configuration file: /config/log/logrotate.conf Clearing the configuration file: /config/log/rsyslog_docker.conf Clearing the configuration file: /config/nginx/nginx.conf Clearing the configuration file: /config/core/env Clearing the configuration file: /config/core/app.conf Clearing the configuration file: /config/registry/config.yml Clearing the configuration file: /config/registry/root.crt Clearing the configuration file: /config/registryctl/env Clearing the configuration file: /config/registryctl/config.yml Clearing the configuration file: /config/db/env Clearing the configuration file: /config/jobservice/env Clearing the configuration file: /config/jobservice/config.yml Generated configuration file: /config/log/logrotate.conf Generated configuration file: /config/log/rsyslog_docker.conf Generated configuration file: /config/nginx/nginx.conf Generated configuration file: /config/core/env Generated configuration file: /config/core/app.conf Generated configuration file: /config/registry/config.yml Generated configuration file: /config/registryctl/env Generated configuration file: /config/db/env Generated configuration file: /config/jobservice/env Generated configuration file: /config/jobservice/config.yml loaded secret from file: /secret/keys/secretkey Generated configuration file: /compose_location/docker-compose.yml Clean up the input dir 防止容器进程没有权限读取生成的配置 [root@lcalhost harbor]# ll common/ 总用量 0 drwxr-xr-x 9 root root 105 4月 30 15:08 config [root@lcalhost harbor]# chmod -R 777 common [root@lcalhost harbor]# ll common/ 总用量 0 drwxrwxrwx 9 root root 105 4月 30 15:08 config ======================================================================================================================================= 最后再次启动 harbor [root@lcalhost harbor]# docker-compose up -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registry ... done Creating harbor-portal ... done Creating redis ... done Creating harbor-db ... done Creating registryctl ... done Creating harbor-core ... done Creating harbor-jobservice ... done Creating nginx ... done 查看服务 [root@lcalhost harbor]# docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------- harbor-core /harbor/harbor_core Up (healthy) harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp harbor-jobservice /harbor/harbor_jobservice ... Up (healthy) harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp nginx nginx -g daemon off; Up (healthy) 0.0.0.0:8080->8080/tcp redis redis-server /etc/redis.conf Up (healthy) 6379/tcp registry /home/harbor/entrypoint.sh Up (healthy) 5000/tcp registryctl /home/harbor/start.sh Up (healthy) 然后访问http://192.168.31.10:8080,即可访问harbor的web界面 ############################################################################################### 要是想修改harbor的登陆用户密码,则最好在harbor web界面里直接修改,这样是最保险的! 如果是想通过修改harbar.yaml文件来重置harbor用户密码,则不能单纯的修改后就执行"./prepare"和重启docker-compose,这样是不能修改harbor用户密码的! 这时因为harbor在这里用的是postgresql数据库,以pdkdf2算法保存的秘文密码!需要先进入"harbor-db"容器内部,执行相关postgresql命令行。 而且postgresql的用户密码采用的是pbkdf2算法,需要提前计算好新密码的密钥值,pdkdf2算法需要"盐值","迭代次数"和密钥长度int型等,特别麻烦!! 所以如果忘记harbor的web密码或者是admin密码需要重置,并且对于postgresql数据库 或者 pbkdf2算法操作不熟悉的话,建议删除data源数据的database,重新部署! 做法如下: # docker-compose down -v # rm -rf /data/database # vim harbor.yaml #在这里重置或修改密码 # docker-compose up -d 这样就可以使用重置后的新密码登陆harbor web界面了,但是之前创建的用户和项目就都删除了。 这种情况最好适用于刚创建初期。 ############################################################################################### docker-compose up -d # 后台启动,如果容器不存在根据镜像自动创建 docker-compose down -v # 停止容器并删除容器 docker-compose start # 启动容器,容器不存在就无法启动,不会自动创建镜像 docker-compose stop # 停止容器 需要注意: 其实上面是停止docker-compose.yml中定义的所有容器,默认情况下docker-compose就是操作同目录下的docker-compose.yml文件。 如果使用其他yml文件,可以使用-f自己指定。
-> 登录Harbor web界面,在"系统管理"->“配置管理”->“认证模式”->"允许自注册"这一项的对勾去掉,则登录的时候就不会有"用户注册"这一功能了。
-> 可以在"配置管理"这一项进行认证模式,邮箱,标签等设置。
3.6) 使用Harbor私有仓库
3.6.1)harbor的login登录
1)在harbor远程别的客户机上登录 [root@lcalhost harbor]# docker login 192.168.31.10 Username: admin Password: Error response from daemon: Get https://192.168.31.10/v2/: dial tcp 192.168.31.10:443: connect: connection refused 在进行harbor登录或上传代码时,会报出上面错误! 这是因为docker1.3.2版本开始默认docker registry使用的是https,而Harbor默认设置的是http方式而不是https,所以当执行用docker login、pull、push等 命令操作非https的docker regsitry的时就会报错。 解决办法: 如下,在/etc/docker/daemon.json文件里添加"insecure-registries"配置。(如果还不行,可以尝试将下面添加的地址由"192.168.31.10"改为"http://192.168.31.10") [root@lcalhost harbor]# vim /etc/docker/daemon.json { "insecure-registries": ["192.168.31.10"] } 然后重启docker服务 [root@lcalhost harbor]# systemctl restart docker 接着再次验证harbor登录,发现就能登录上了 [root@lcalhost harbor ~]# docker login 192.168.31.10 #或者直接执行"docker login -u admin -p kevin@BO123 192.168.31.10"命令登陆 Username: admin Password: Login Succeeded [root@lcalhost harbor ~]# 2)如果是在harbor本机登录,出现上面的报错: [root@lcalhost harbor]# docker login 192.168.31.10 Username: admin Password: Error response from daemon: Get https://192.168.31.10/v1/users/: dial tcp 192.168.31.10:443: connect: connection refused 解决办法: 在/etc/docker/daemon.json 文件里添加"insecure-registries"配置 (第一行是之前添加的docker加速配置),注意两行之间有一个","逗号隔开 [root@lcalhost harbor]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://l5s01364.mirror.aliyuncs.com"], "insecure-registries": ["192.168.31.10"] } 修改过后重启docker, 然后重启Harbor服务 [root@lcalhost harbor]# systemctl restart docker [root@lcalhost harbor]# docker-compose stop [root@lcalhost harbor]# docker-compose start 然后再测试再harbor本机登录 [root@lcalhost harbor]# docker login 192.168.31.10 Username: admin 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/#credentials-store Login Succeeded [root@lcalhost harbor]# 登录的账号信息都保存到/root/.docker/config.json文件里了 [root@lcalhost harbor]# cat /root/.docker/config.json { "auths": { "192.168.31.10": { "auth": "YWRtaW46a2V2aW5AQk8xOTg3" } }, "HttpHeaders": { "User-Agent": "Docker-Client/18.09.6 (linux)" } 只要/root/.docker/config.json里的信息不删除,后续再次登录的时候,就不用输入用户名和密码了 [root@lcalhost harbor ~]# docker login 192.168.31.10 Authenticating with existing credentials... 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/#credentials-store Login Succeeded
注意事项总结:
harbor支持http和https,但如果使用http的话,在拉取镜像的时候,会抛出仓库不受信任的异常。
需要在所有的docker客户端的docker配置文件/etc/docker/daemon.json中添加如下配置:
{ "insecure-registries": ["https://*.*.*.*"] }
如果使用自签名的https证书,仍然会提示证书不受信任的问题。需要将自签名的ca证书发送到所有的docker客户端的指定目录。
关于使用自签名证书配置harbor的具体过程可以参考: https://github.com/goharbor/harbor/blob/master/docs/configure_https.md
3.6.2)harbor仓库的使用
#镜像打标签的命令 # docker tag 镜像名:标签 私服地址/仓库项目名/镜像名:标签 #推送到私服的命令 #docker push 私服地址/仓库项目名/镜像名:标签 #从私服拉取镜像的命令 #docker pull 私服地址/仓库项目名/镜像名:标签
首先在Harbor web界面里最好创建一个自己需要的"项目" (或者使用默认的"library"项目),项目公开和私有:
- Public: 所有用户对于公开项目都有读权限,这种方式对于你想把一些仓库分享给其他人的时候,是非常方便的.
- Private: 私有项目只能被有特定用户权限的人去访问。这种方式对于内部团队来说共享也是比较方便的
比如创建一个公开项目"kevin_bo",点击进去可以看到推送镜像的信息提示
然后就可以在Harbor服务器的终端命令行里进行镜像推送到Harbor仓库的操作了:
在进行harbor镜像推送和拉取操作前,需要事先login登录到harbor仓库里,这样才有harbor镜像的推送和拉取的权限!!
3.6.3)Harbor的https证书启用
通过上面可知,harbor默认安装后采用的是http方式,后面使用的时候可能会发现很多不方面。因为Docker客户端登录harbor进行镜像推送或拉取时默认是https方式!所以http方式下,需要在每一台harbor客户端机器上都要设置"insecure-registries", 感觉很麻烦!所以最好还是将harbor默认的http方式改为https方式!另外,从安全角度考虑,容器的仓库在生产环境中往往也是需要被设定为https的方式,而harbor将这些证书的创建和设定都进行了简单的集成,下面来看一下在harbor下如何使用https的方式。配置记录如下:
在创建证书之前,为了方面验证,需要将前面在客户端机器上/etc/docker/daemon.json文件里添加"insecure-registries"配置删除 [root@client ~]# cat /etc/docker/daemon.json cat: /etc/docker/daemon.json: 没有那个文件或目录 [root@client ~]# rm -rf /root/.docker 重启docker服务 [root@client ~]# systemctl daemon-reload [root@client ~]# systemctl restart docker 将harbor部署机自身的/etc/docker/daemon.json文件里添加"insecure-registries"配置也删除 [root@localhost harbor]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://l5s01364.mirror.aliyuncs.com"] } [root@localhost harbor]# rm -rf /root/.docker 然后重启docker和docker-compose [root@localhost harbor]# systemctl daemon-reload [root@localhost harbor]# systemctl restart docker [root@localhost harbor]# docker-compose down Stopping harbor-log ... done Removing harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-core ... done Removing redis ... done Removing registryctl ... done Removing harbor-portal ... done Removing harbor-db ... done Removing registry ... done Removing network harbor_harbor [root@localhost harbor]# docker-compose up -d Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registry ... done Creating harbor-db ... done Creating registryctl ... done Creating harbor-portal ... done Creating redis ... done Creating harbor-core ... done Creating harbor-jobservice ... Creating nginx ... 1)创建CA [root@localhost harbor]# pwd /root/harbor [root@localhost harbor]# mkdir ssl [root@localhost harbor]# cd ssl/ [root@localhost ssl]# pwd /root/harbor/ssl [root@localhost ssl]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 365 -out ca.crt Generating a 4096 bit RSA private key ..................................++ ..............................................................................................................................................................++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:DevOps Organizational Unit Name (eg, section) []:Tec Common Name (eg, your name or your server's hostname) []:192.168.31.10 Email Address []:1282540684@qq.com 2) 创建证书请求文件csr [root@localhost ssl]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout 192.168.31.10.key -out 192.168.31.10.csr Generating a 4096 bit RSA private key ....................................................................................................................................................................................................................................++ ............................................................................................................................................++ writing new private key to '192.168.31.10.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:DevOps Organizational Unit Name (eg, section) []:Tec Common Name (eg, your name or your server's hostname) []:192.168.31.10 Email Address []:1282540684@qq.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:DevOps 3) 创建证书 [root@localhost ssl]# echo subjectAltName = IP:192.168.31.10 > extfile.cnf [root@localhost ssl]# openssl x509 -req -days 365 -in 192.168.31.10.csr -CA ca.crt -CAkey ca.key -CAcreateserial -extfile extfile.cnf -out 192.168.31.10.crt Signature ok subject=/C=CN/ST=beijing/L=beijing/O=DevOps/OU=Tec/CN=192.168.31.10/emailAddress=1282540684@qq.com Getting CA Private Key 4) 设定证书 & 修改 查看证书所在路径, 后面将harbor.yaml文件中的路径也同样设定 [root@localhost ssl]# pwd /root/harbor/ssl [root@localhost ssl]# ll 总用量 28 -rw-r--r-- 1 root root 2029 5月 2 10:18 192.168.31.10.crt -rw-r--r-- 1 root root 1809 5月 2 10:17 192.168.31.10.csr -rw-r--r-- 1 root root 3272 5月 2 10:17 192.168.31.10.key -rw-r--r-- 1 root root 2110 5月 2 10:15 ca.crt -rw-r--r-- 1 root root 3272 5月 2 10:15 ca.key -rw-r--r-- 1 root root 17 5月 2 10:18 ca.srl -rw-r--r-- 1 root root 34 5月 2 10:18 extfile.cnf 获取192.168.31.10.crt和192.168.31.10.key文件后,可以将它们放入如下目录中/root/cert/: [root@localhost ssl]# cp 192.168.31.10.crt /root/cert/ [root@localhost ssl]# cp 192.168.31.10.key /root/cert/ 5) 修改harbor.yaml文件 先关闭docker-compose [root@harbor-node harbor]# pwd /root/harbor [root@localhost harbor]# docker-compose down -v Stopping harbor-jobservice ... done Stopping harbor-core ... done Stopping registry ... done Stopping redis ... done Stopping registryctl ... done Stopping harbor-portal ... done Stopping harbor-db ... done Stopping harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-core ... done Removing registry ... done Removing redis ... done Removing registryctl ... done Removing harbor-portal ... done Removing harbor-db ... done Removing harbor-log ... done Removing network harbor_harbor [root@localhost harbor]# docker-compose ps Name Command State Ports ------------------------------ [root@harbor-node harbor]# vim harbor.yml ................. ................. hostname: 192.168.31.10 # https related config https: # https port for harbor, default is 443 port: 443 # The path of cert and key files for nginx certificate: /root/cert/192.168.31.10.crt private_key: /root/cert/192.168.31.10.key ................. ................. ================================================================================================================ 特别注意: 上面harbor.yaml文件中修改的配置格式一定要正确!"https"要顶格写,"port:443" 和 "certificate"、"private_key"保持缩进一致! 否则在下面执行"./prepare"更新命令时,会报错。 接着执行prepare脚本,将harbor修改的配置更新到 docker-compose.yml 文件 [root@localhost harbor]# ./prepare prepare base dir is set to /root/harbor Clearing the configuration file: /config/log/logrotate.conf Clearing the configuration file: /config/log/rsyslog_docker.conf Clearing the configuration file: /config/nginx/nginx.conf Clearing the configuration file: /config/core/env Clearing the configuration file: /config/core/app.conf Clearing the configuration file: /config/registry/config.yml Clearing the configuration file: /config/registry/root.crt Clearing the configuration file: /config/registryctl/env Clearing the configuration file: /config/registryctl/config.yml Clearing the configuration file: /config/db/env Clearing the configuration file: /config/jobservice/env Clearing the configuration file: /config/jobservice/config.yml Generated configuration file: /config/log/logrotate.conf Generated configuration file: /config/log/rsyslog_docker.conf Generated configuration file: /config/nginx/nginx.conf Generated configuration file: /config/core/env Generated configuration file: /config/core/app.conf Generated configuration file: /config/registry/config.yml Generated configuration file: /config/registryctl/env Generated configuration file: /config/db/env Generated configuration file: /config/jobservice/env Generated configuration file: /config/jobservice/config.yml loaded secret from file: /secret/keys/secretkey Generated configuration file: /compose_location/docker-compose.yml Clean up the input dir 查看一下docker-compose.yml文件,发现已经将新配置的443端口的https信息更新到docker-compose.yml文件里了 如下80端口和443端口都配置了,所以harbor访问时是http强转到https的 [root@localhost harbor]# cat docker-compose.yml | grep 443 -C3 dns_search: . ports: - 80:8080 - 443:8443 depends_on: - registry - core 重启docker-compose [root@localhost harbor]# docker-compose up -d Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registry ... done Creating harbor-db ... done Creating registryctl ... done Creating harbor-portal ... done Creating redis ... done Creating harbor-core ... done Creating harbor-jobservice ... done Creating nginx ... done [root@localhost harbor]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------- harbor-core /harbor/harbor_core Up (health: starting) harbor-db /docker-entrypoint.sh Up (health: starting) 5432/tcp harbor-jobservice /harbor/harbor_jobservice ... Up (health: starting) harbor-log /bin/sh -c /usr/local/bin/ ... Up (health: starting) 127.0.0.1:1514->10514/tcp harbor-portal nginx -g daemon off; Up (health: starting) 8080/tcp nginx nginx -g daemon off; Up (health: starting) 0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp redis redis-server /etc/redis.conf Up (health: starting) 6379/tcp registry /home/harbor/entrypoint.sh Up (health: starting) 5000/tcp registryctl /home/harbor/start.sh Up (health: starting) 在harbor部署机本机确认login登陆 (使用80端口或443端口都可以,自动跳转的) [root@localhost harbor]# docker login -u admin -p harbor123456 192.168.31.10 WARNING! Using --password via the CLI is insecure. Use --password-stdin. Error response from daemon: Get https://192.168.31.10/v2/: x509: certificate signed by unknown authority 客户端登录: [root@client ~]# docker login -u admin -p harbor123456 192.168.31.10 WARNING! Using --password via the CLI is insecure. Use --password-stdin. Error response from daemon: Get https://192.168.31.10/v2/: x509: certificate signed by unknown authority 以上出现报错,解决办法: 此种情况多发生在自签名的证书,报错含义是签发证书机构未经认证,无法识别。 解决办法: [root@localhost harbor]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem [root@localhost harbor]# cat /root/harbor/ssl/192.168.31.10.crt >> /etc/pki/tls/certs/ca-bundle.crt [root@localhost harbor]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 由于证书是docker的daemon需要用到的,所以需要重启docker服务,进而也要重启docker-compose [root@localhost harbor]# systemctl daemon-reload [root@localhost harbor]# systemctl restart docker [root@localhost harbor]# docker-compose down -v Stopping harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-core ... done Removing harbor-portal ... done Removing redis ... done Removing registry ... done Removing registryctl ... done Removing harbor-db ... done Removing harbor-log ... done Removing network harbor_harbor [root@localhost harbor]# docker-compose up -d Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registry ... done Creating registryctl ... done Creating harbor-db ... done Creating harbor-portal ... done Creating redis ... done Creating harbor-core ... done Creating harbor-jobservice ... done Creating nginx ... done 然后再次尝试在harbor本机登陆, 发现就可以正常登陆了!! [root@localhost harbor]# docker login -u admin -p harbor123456 192.168.31.10 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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/#credentials-store Login Succeeded 登陆的权限信息保存到/root/.docker/config.json 文件里了,只要这个文件不删除,下次就可以不需要输入用户名和密码直接登陆了! [root@localhost harbor]# cat /root/.docker/config.json { "auths": { "192.168.31.10": { "auth": "YWRtaW46aGFyYm9yMTIzNDU2" } }, "HttpHeaders": { "User-Agent": "Docker-Client/18.06.1-ce (linux)" } } [root@harbor-node harbor]# docker login 172.16.60.213 Authenticating with existing credentials... 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/#credentials-store Login Succeeded 上面是使用80端口登陆的,后面加上443端口也是可以登陆的 [root@localhost harbor]#docker login 192.168.31.10:443 Username: admin 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/#credentials-store Login Succeeded [root@localhost harbor]# docker login 192.168.31.10:443 Authenticating with existing credentials... 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/#credentials-store Login Succeeded [root@localhost ssl]# docker login -u admin -p harbor123456 192.168.31.10:443 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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/#credentials-store Login Succeeded ========================================================================================== 上面是在harbor本机尝试的登陆,现在在远程客户机上(这里客户机为192.168.31.20)测试harbor登陆: 首先很重要的一步,这一步极其关键!一定不要忘记操作!! 就是需要将harbor服务端生成的CA证书拷贝到每个远程客户机的"/etc/docker/certs.d/harbor服务器的域名或ip/" 目录下 [root@client ~]# mkdir -p /etc/docker/certs.d/192.168.31.10/ 接着在harbor服务器将CA证书拷贝过来 [root@localhost ssl]# scp /root/harbor/ssl/* 192.168.31.20:/etc/docker/certs.d/192.168.31.10/ 192.168.31.10.crt 100% 2029 1.4MB/s 00:00 192.168.31.10.csr 100% 1809 1.0MB/s 00:00 192.168.31.10.key 100% 3272 1.8MB/s 00:00 ca.crt 100% 2110 1.7MB/s 00:00 ca.key 100% 3272 2.7MB/s 00:00 ca.srl 100% 17 24.9KB/s 00:00 extfile.cnf 100% 34 54.3KB/s 00:00 然后在客户机上查看是否拷贝过来了harbor服务端的CA证书 [root@client 192.168.31.10]# pwd /etc/docker/certs.d/192.168.31.10 [root@client 192.168.31.10]# ll 总用量 28 -rw-r--r--. 1 root root 2029 5月 2 10:55 192.168.31.10.crt -rw-r--r--. 1 root root 1809 5月 2 10:55 192.168.31.10.csr -rw-r--r--. 1 root root 3272 5月 2 10:55 192.168.31.10.key -rw-r--r--. 1 root root 2110 5月 2 10:55 ca.crt -rw-r--r--. 1 root root 3272 5月 2 10:55 ca.key -rw-r--r--. 1 root root 17 5月 2 10:55 ca.srl -rw-r--r--. 1 root root 34 5月 2 10:55 extfile.cnf 进行同样的授权操作, [root@client 192.168.31.10]# chmod 644 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem [root@client 192.168.31.10]# cat /etc/docker/certs.d/192.168.31.10/192.168.31.10.crt >> /etc/pki/tls/certs/ca-bundle.crt [root@client 192.168.31.10]# chmod 444 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 重启docker服务 [root@client 192.168.31.10]# systemctl daemon-reload [root@client 192.168.31.10]# systemctl restart docker 最后进行harbor登陆,就可以正常登陆了! [root@client 192.168.31.10]# docker login -u admin -p harbor123456 192.168.31.10:443 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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/#credentials-store Login Succeeded [root@client 192.168.31.10]# cat /root/.docker/config.json { "auths": { "192.168.31.10:443": { "auth": "YWRtaW46aGFyYm9yMTIzNDU2" } }, "HttpHeaders": { "User-Agent": "Docker-Client/18.06.1-ce (linux)" } } 上面是使用443端口登陆harbor仓库是正常的,如果此时使用80端口登陆,则出现如下报错: [root@client 192.168.31.10]# docker login -u admin -p harbor123456 192.168.31.10 WARNING! Using --password via the CLI is insecure. Use --password-stdin. Error response from daemon: Missing client certificate 192.168.31.10.cert for key 192.168.31.10.key 所以在客户端就使用443端口来登陆harbor仓库了!
[[ 需要注意 ]] 如果Harbor里创建了多个账号,客户端使用A账号登录harbor后,docker pull下载的镜像是在B账号的项目里面的,并且该项目是私有的,那么需要先将A账号添加为该项目的成员后才能正常docker pull下载,否则会出现报错:denied: requested access to the resource is denied
3.6.4) Harbor私仓的高可用
单机部署harbor显然无法满足在生产中需求,必须要保证应用的高可用性。目前有两种主流的方案来解决Harbor高可用问题:
- 双主复制
- 多harbor实例共享后端存储
- Harbor双主复制
-
主从同步
harbor官方默认提供主从复制的方案来解决镜像同步问题,通过复制方式,可以实时将测试环境harbor仓库的镜像同步到生产环境harbor,类似于如下流程:在实际生产运维的中,往往需要把镜像发布到几十或上百台集群节点上。这时,单个Registry已经无法满足大量节点的下载需求,因此要配置多个Registry实例做负载均衡。手工维护多个Registry实例上的镜像,将是十分繁琐的事情。Harbor可以支持一主多从的镜像发布模式,可以解决大规模镜像发布的难题。
只要往一台Registry上发布,镜像就像"仙女散花"般地同步到多个Registry中,高效可靠。如果是地域分布较广的集群,还可以采用层次型发布方式,如从集团总部同步到省公司,从省公司再同步到市公司。
然而单靠主从同步方式仍然解决不了harbor主节点的单点问题。
-
双主复制说明
所谓双主复制其实就是复用主从同步实现两个harbor节点之间的双向同步,来保证数据的一致性,然后在两台harbor前端配置一个负载均衡器将进来的请求分流到不同的实例中去,只要有一个实例中有了新的镜像,就是自动的同步复制到另外的的实例中去,这样实现了负载均衡,也避免了单点故障,在一定程度上实现了Harbor的高可用性。可以使用下面方案:Nginx+Keepalive+Harbor,VIP可以在LB上实现漂移。(或者VIP直接在Harbor之间漂移)。
创建harbor主主复制,可以在harbor的web界面里创建相互之间镜像同步关系,同步关系可以选择相同用户或不同用户之间。这样就保证了harbor双机热备关系。
1)“系统管理”->“仓库管理”->“新建目标”, 填写对端harbor信息
2 “系统管理”->“同步管理”->“新建规则”, 规则里会引用目的Registry,也就是上面一步创建的目标。同步模式有Push-based,Pull-based;触发模式有自动和定时。不过这个方案有一个问题:就是有可能两个Harbor实例中的数据不一致。假设如果一个实例A挂掉了,这个时候有新的镜像进来,那么新的镜像就会在另外一个实例B中,后面即使恢复了挂掉的A实例,Harbor实例B也不会自动去同步镜像,这样只能手动的先关掉Harbor实例B的复制策略,然后再开启复制策略,才能让实例B数据同步,让两个实例的数据一致。所以,在实际生产使用中,主从复制十分的不靠谱。
多harbor实例共享后端存储
共享后端存储算是一种比较标准的方案,就是多个Harbor实例共享同一个后端存储,任何一个实例持久化到存储的镜像,都可被其他实例中读取。通过前置LB进来的请求,可以分流到不同的实例中去处理,这样就实现了负载均衡,也避免了单点故障。
这个方案在实际生产环境中部署需要考虑三个问题:
1.共享存储的选取,Harbor的后端存储目前支持AWS S3、Openstack Swift、Ceph等,[在实验环境里,可以直接使用nfs]
2.Session在不同的实例上共享,这个现在其实已经不是问题了,在最新的harbor中,默认session会存放在redis中,我们只需要将redis独立出来即可。可以通过redis sentinel或者redis cluster等方式来保证redis的可用性。[在实验环境里,仍然使用单台redis]
3.Harbor多实例数据库问题,这个只需要将harbor中的数据库拆出来独立部署即可。让多实例共用一个外部数据库,外部数据库可以通过Mysql 高可用方案保证高可用性。