云原生技术与应用-生产环境构建高可用Harbor私有镜像仓库
目录
#1.1Harbor简介
1.1.1Harbor的架构
1.1.2Harbor高可用部署
#2.1安装Harbor(worker节点)
2.1.1安装nginx(master)
1.1Harbor简介
Docker容器应用的开发和运行离不开可靠的镜像管理,Docker官方提供了原生的Registry,但其功能比较简单,而且没有可视化界面,自然无法满足企业级的需求。虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署私有环境内的Registry也是非常必要的。
为了解决以上需求,VMware公司推出了Harbor,Harbor 是为企业用户设计的容器镜像仓库开源项目,包括了权限管理(RBAC)、LDAP、审计、安全漏洞扫描、镜像验真、管理界面、自我注册、HA 等企业必需的功能,同时针对中国用户的特点,设计镜像复制和中文支持等功能。
1.1.1Harbor的架构
Harbor的GitHub仓库地址如下:
https://github.com/goharbor/harbor
Wiki:https://github.com/goharbor/harbor/wiki
1.1.2Harbor高可用部署
Harbor 高可用部署是指通过一系列架构设计和技术手段,确保 Harbor 镜像仓库在面临硬件故障、软件异常、网络问题等情况时,仍能保持稳定运行和服务连续性的部署方式。其核心目标是消除单点故障,提升系统的可靠性、可用性和容错能力。
准备工作:
系统版本 | IP地址 | 节点角色 | CPU | Memory | Hostname |
CentOS-7.9 | 192.168.10.101 | Nginx | \>=2 | \>=2G | Nginx, Docker |
CentOS-7.9 | 192.168.10.102 | Harbor1 | \>=2 | \>=2G | Docker01 |
CentOS-7.9 | 192.168.10.103 | Harbor2 | \>=2 | \>=2G | Docker02 |
2.1安装Harbor(worker节点)
1:下载安装包
https://github.com/goharbor/harbor/releases/tag/v2.4.3
https://github.com/goharbor/harbor/releases/download/v2.4.3/harbor-offline-installer-v2.4.3.tgz
2:下载完成后,将压缩包上传到两个worker节点:
[root@localhost ~]# ls
harbor-offline-installer-v2.4.3.tgz
[root@localhost ~]# ls
harbor-offline-installer-v2.4.3.tgz
3:然后对其进行解压:
[root@localhost ~]# tar -zxvf harbor-offline-installer-v2.4.3.tgz
解压后的目录文件如下:
[root@localhost ~]# cd harbor
[root@localhost harbor]# ls
common.sh harbor.v2.4.3.tar.gz harbor.yml.tmpl install.sh LICENSE prepare
4:配置harbor
将配置文件模板拷贝一份,并命名为harbor.yml,这是默认的配置文件名称:
[root@localhost harbor]# cp harbor.yml.tmpl harbor.yml
编辑harbor.yml文件,按照如下说明修改几处配置项:
[root@localhost harbor]# vim harbor.yml
# 修改为当前所在节点的ip
hostname: 192.168.10.102
# 登录界面的密码
harbor_admin_password: pwd123
# 将https相关的配置给注释掉,这里为了简单只使用http,而且也可以在nginx那一层去做https
# https related config
#https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path注意:
注释掉https相关的行
5:安装docker-compose
准备好配置文件之后,安装docker-compose,因为Harbor的安装脚本是基于docker-compose去安装的。下载docker-compose然后放到/usr/local/bin/目录下,再更改一下权限即可:
[root@localhost ~]# curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod 755 /usr/local/bin/docker-compose
6:运行Harbor的安装脚本
[root@localhost harbor]# ./install.sh
[Step 0]: checking if docker is installed ...
Note: docker version: 19.03.12
[Step 1]: checking docker-compose is installed ...
Note: docker-compose version: 1.26.2
[Step 2]: loading Harbor images ...
Loaded image: goharbor/prepare:v2.4.3
Loaded image: goharbor/harbor-jobservice:v2.4.3
Loaded image: goharbor/harbor-registryctl:v2.4.3
Loaded image: goharbor/registry-photon:v2.4.3
Loaded image: goharbor/harbor-core:v2.4.3
Loaded image: goharbor/notary-signer-photon:v2.4.3
Loaded image: goharbor/clair-photon:v2.4.3
Loaded image: goharbor/trivy-adapter-photon:v2.4.3
Loaded image: goharbor/harbor-log:v2.4.3
Loaded image: goharbor/nginx-photon:v2.4.3
Loaded image: goharbor/clair-adapter-photon:v2.4.3
Loaded image: goharbor/chartmuseum-photon:v2.4.3
Loaded image: goharbor/harbor-portal:v2.4.3
Loaded image: goharbor/harbor-db:v2.4.3
Loaded image: goharbor/redis-photon:v2.4.3
Loaded image: goharbor/notary-server-photon:v2.4.3
[Step 3]: preparing environment ...
[Step 4]: preparing harbor configs ...
prepare base dir is set to /usr/local/src/harbor
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
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/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir
[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db ... done
Creating harbor-portal ... done
Creating redis ... done
Creating registryctl ... done
Creating registry ... done
Creating harbor-core ... done
Creating harbor-jobservice ... done
Creating nginx ... done
✔ ----Harbor has been installed and started successfully.----
7:安装完成,使用浏览器访问Harbor,正常情况下应能进入登录界面:
2.1.1安装nginx(master)
1:拉取nginx的镜像:
[root@m1 ~]# docker pull nginx:1.13.12
创建一个nginx配置文件,定义一些简单的配置:
[root@m1 ~]# mkdir nginx
[root@m1 ~]# cd nginx
[root@m1 nginx]# vim nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
stream {
upstream hub{
server 192.168.10.102:80;
}
server {
listen 80;
proxy_pass hub;
proxy_timeout 300s;
proxy_connect_timeout 5s;
}
}
2:然后为了方便操作,我们写一个简单的启动脚本:
[root@m1 nginx]# vim restart.sh
#!/bin/bash
docker stop harbor-nginx
docker rm harbor-nginx
docker run -itd --net=host --name harbor-nginx -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf nginx:1.13.12
执行该脚本:
[root@m1 ~/nginx]# sh restart.sh
3:使用浏览器访问master节点的ip看看能否正常进入到Harbor的登录页:
(1)创建Harbor项目和用户
(2)到“用户管理”新建一个用户:
将该用户添加到新建的项目中:
(2)所有的docker主机设置daemon.json文件
回到命令行上测试一下push和pull。由于我们自己搭建的私有仓库默认是不受Docker信任的,所以需要先在配置文件中增加如下配置项让Docker信任该registry:
[root@m1 ~]# vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.10.101"]
}
[root@m1 ~]# systemctl daemon-reload
[root@m1 ~]# systemctl restart docker
(3)找一个docker主机,登录到我们的Harbor仓库:
[root@m1 ~]# docker login 192.168.10.101
Username: pusher
Password:
Login Succeeded
[root@m1 ~]#
(4)然后尝试使用命令行push一个镜像到Harbor上:
[root@m1 ~]# docker pull cirros
[root@m1 ~]# docker tag cirros 192.168.10.101/kubernetes/cirros:v1
[root@m1 ~]# docker push 192.168.10.101/kubernetes/cirros:v1
The push refers to repository [192.168.10.101/kubernetes/nginx]
7ab428981537: Pushed
82b81d779f83: Pushed
d626a8ad97a1: Pushed
1.13.12: digest: sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90 size: 948
[root@m1 ~]#
(5)测试pull
到另一台机器上用同样方式配置daemon.json并docker login到Harbor,然后使用docker pull从Harbor上拉取镜像:
[root@localhost ~]# docker pull 192.168.10.101/kubernetes/cirros:v1
1.13.12: Pulling from kubernetes/nginx
f2aa67a397c4: Pull complete
3c091c23e29d: Pull complete
4a99993b8636: Pull complete
Digest: sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90
Status: Downloaded newer image for 192.168.243.138/kubernetes/nginx:1.13.12
192.168.243.138/kubernetes/nginx:1.13.12
[root@localhost ~]#
4:配置Harbor节点互相复制(两个节点都设置,要连接对方的ip)
测试完Harbor的基本功能后,我们接下来配置一下Harbor节点之间的互相复制功能,让两个节点能够同步镜像数据。首先到第一个节点上的“仓库管理”界面中新建一个目标,这个目标就是另一台Harbor节点,然后到“复制管理”界面中新建复制规则,如下: