**环境配置** | IP地址 | docker版本 | docker-compose版本 | 内核 | Centos | | :------------: | :------------: | :------------: | :------------: | :------------: | |10.60.60.24 | 20.10.9 | v2.4.0 | 3.10.0 | 7.9 | |10.60.60.23(测试客户端) | 20.10.9 | | 3.10.0 | 7.9 | ------------ ------------ # 一、介绍 Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境的Registry也是非常必要的。 Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。 # 二、组件 用于部署 Harbor 的 Docker Compose 模板 docker-compose.yml. 打开这个模板文件,会发现 Harbor 由 5 个容器组成: - proxy:由 Nginx 服务器构成的反向代理。 - registry:由 Docker 官方的开源 registry 镜像构成的容器实例。 - ui:即架构中的 core services, 构成此容器的代码是 Harbor 项目的主体。 - mysql:由官方 MySql 镜像构成的数据库容器。 - log: 运行着 rsyslogd 的容器,通过 log-driver 的形式收集其他容器的日志。 这几个容器通过 Docker link 的形式连接在一起,这样,在容器之间可以通过容器名字互相访问。对终端用户而言,只需要暴露 proxy (即 Nginx)的服务端口。 # 三、工作原理 假设我们将 Harbor 部署在主机名为 registry.abcdocker.com 的虚机上。用户通过 docker login 命令向这个 Harbor 服务发起登录请求:docker login registry.abcdocker.com当用户输入所需信息并点击回车后,Docker 客户端会向地址“registry.abcdocker.com/v2/” 发出 HTTP GET 请求。 **Harbor 的各个容器会通过以下步骤处理:** ## (1) Docker login (a) 首先,这个请求会由监听 80 端口的 proxy 容器接收到。根据预先设置的匹配规则,容器中的 Nginx会将请求转发给后端的 registry 容器; (b) 在 registry 容器一方,由于配置了基于 token 的认证,registry 会返回错误代码 401,提示 Docker客户端访问 token 服务绑定的 URL。在 Harbor 中,这个 URL 指向 Core Services; (c) Docker 客户端在接到这个错误代码后,会向token服务的URL发出请求,并根据HTTP协议的BasicAuthentication 规范,将用户名密码组合并编码,放在请求头部(header); (d)类似地,这个请求通过 80 端口发到 proxy 容器后,Nginx 会根据规则把请求转发给 ui 容器,ui 容器监听 token 服务网址的处理程序接收到请求后,会将请求头解码,得到用户名、密码; (e) 在得到用户名、密码后,ui 容器中的代码会查询数据库,将用户名、密码与 mysql 容器中的数据进行比对(注:ui 容器还支持 LDAP 的认证方式,在那种情况下 ui 会试图和外部 LDAP 服务进行通信并校验用户名/密码)。比对成功,ui 容器会返回表示成功的状态码, 并用密钥生成 token,放在响应体中返回给 Docker 客户端。 这个过程中组件间的交互过程如下图所示 ![Docker_login.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/3177057296.png) **至此,一次 docker login 成功地完成了,Docker 客户端会把步骤(c)中编码后的用户名密码保存在本地的隐藏文件中。** ## (2) Docker Push 用户登录成功后用 docker push 命令向 Harbor 推送一个 Docker 镜像:docker push registry.abcdocker.com/library/hello-world (a) 首先,docker 客户端会重复 login 的过程,首先发送请求到 registry,之后得到 token 服务的地址; (b) 之后,Docker 客户端在访问ui容器上的token服务时会提供额外信息,指明它要申请一个对imagelibrary/hello-world 进行 push 操作的 token; (c) token 服务在经过 Nginx 转发得到这个请求后,会访问数据库核实当前用户是否有权限对该 image进行 push。如果有权限,它会把 image 的信息以及 push 动作进行编码,并用私钥签名,生成 token返回给 Docker 客户端; (d) 得到 token 之后 Docker 客户端会把 token 放在请求头部,向 registry 发出请求,试图开始推送image。 Registry 收到请求后会用公钥解码 token 并进行核对,一切成功后,image 的传输就开始了。 我们省去 proxy 转发的步骤,下图描述了这个过程中各组件的通信 ![Docker_Push.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/1039799232.png) **注意:**Harbor的所有服务组件都是在Docker中部署的,所以官方安装使用Docker-compose快速部署,所以我们需要安装Docker、Docker-compose. 由于Harbor是基于Docker Registry V2版本,所以就要求Docker版本不小于1.10.0,Docker-compose版本不小于1.6.0 # 四、环境准备 设置主机名 ```shell hostnamectl set-hostname harbor ``` 时间同步 ```shell yum -y install ntp systemctl enable ntpd systemctl start ntpd ntpdate -u cn.pool.ntp.org hwclock --systohc timedatectl set-timezone Asia/Shanghai ``` 永久关闭swap分区,注释掉SWAP分区项,即可 ```shell vim /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 UUID=689840c5-310d-4268-b022-bec382d26884 /boot xfs defaults 0 0 #/dev/mapper/centos-swap swap swap defaults 0 0 ``` 关闭防火墙 Selinux ```shell systemctl stop firewalld systemctl disable firewalld sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config ``` 重启下,使配置生效 ```shell reboot ``` # 五、Docker Install 下载Docker安装包,下载不了多试几次 ```shell wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.9-3.el7.x86_64.rpm wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-rootless-extras-20.10.9-3.el7.x86_64.rpm wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.5.11-3.1.el7.x86_64.rpm wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-20.10.9-3.el7.x86_64.rpm wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-scan-plugin-0.9.0-3.el7.x86_64.rpm ``` 安装Docker,其余的依赖可以自己下载下,应该还需要这个包(policycoreutils-python) ```shell rpm -ivh container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm rpm -ivh containerd.io-1.5.11-3.1.el7.x86_64.rpm rpm -ivh docker-ce-rootless-extras-20.10.9-3.el7.x86_64.rpm --nodeps --force rpm -ivh docker-scan-plugin-0.9.0-3.el7.x86_64.rpm --nodeps --force rpm -ivh docker-ce-cli-20.10.9-3.el7.x86_64.rpm rpm -ivh docker-ce-20.10.9-3.el7.x86_64.rpm ``` 设置开机启动并启动docker ```shell systemctl enable docker systemctl start docker ``` # 六、Docker-compose Install Github地址:https://github.com/docker/compose/releases **Docker Compose简介** - Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。 **安装Docker-compose** 使用curl下载速度太慢了,去GitHub下载也慢,最好用vpn下载或者用我上传的2.4.0的附件 下载链接:[docker-compose-linux-x86_64-v2_4_0](https://www.xiaoleizhang.com/usr/uploads/2022/04/docker-compose-linux-x86_64-v2_4_0) ```shell curl -L https://github.com/docker/compose/releases/download/v2.4.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose #如果使用我的下载链接的,可以先下载后面再上传到服务器,然后重命名到执行目录下 mv /root/docker-compose-linux-x86_64-v2_4_0 /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose [root@harbor ~]# docker-compose --version Docker Compose version v2.4.0 [root@harbor ~]# ``` # 七、Harbor Install Harbor官方地址:https://github.com/vmware/harbor/releases 下载安装包,注意一个是offline(离线的),另一个是online(在线的) 同样的也是需要vpn下载会快点,我这里也放个我自己的下载链接 下载链接:[harbor-offline-installer-v2.4.2.tar](https://www.xiaoleizhang.com/usr/uploads/2022/04/harbor-offline-installer-v2.4.2.tar) ```shell wget https://github.com/goharbor/harbor/releases/download/v2.4.2/harbor-offline-installer-v2.4.2.tgz tar xf harbor-offline-installer-v2.4.2.tar ``` **配置Harbor** 解压之后,配置文件名称harbor.yml.tmpl,该文件就是Harbor的配置文件,需要复制下把tmpl去掉。 ```shell cp /root/harbor/harbor.yml.tmpl /root/harbor/harbor.yml ``` 编辑harbor.yml 实际上我们只需要修改Hostname字段,同时注释https因为这里我们用的是80端口 ```shell cd /root/harbor/ vim harbor.yml hostname: 10.60.60.24 # https related config #https: # https port for harbor, default is 443 # port: 443 # The path of cert and key files for nginx ``` **harbor.yml相关配置解释** ```shell # hostname 设置访问地址,可以使用IP/域名,不可以设置127.0.0.1 && localhost hostname: 10.60.60.24 # 证书路径 certificate: /your/certificate/path private_key: /your/private/key/path # Harbor的用户admin的密码 harbor_admin_password: Harbor12345 # 数据库配置 database: password: root123 max_idle_conns: 100 max_open_conns: 900 data_volume: /data # Job 最大进程数 max_job_workers: 10 # 日志大小和位置 rotate_count: 50 rotate_size: 200M location: /var/log/harbor # Harbor版本 _version: 2.4.0 ``` 本次我们使用离线安装,安装包上面我已经放了 ```shell cd /root/harbor/ ./install.sh ``` 安装完成后可以执行以下命令查看下,组件是否正常 ```shell docker-compose ps docker images ``` ![docker-compose_ps.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/3964717949.png) ![docker_images.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/3232804719.png) # 八、访问Harbor 默认端口设置的是80,账号admin,默认密码Harbor12345 ![访问Harbor.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/3176693850.png) # 九、Harbor仓库简单使用 登录后,新建项目 ![新建项目.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/1025668372.png) 上传镜像到Harbor仓库,我们新建一个名称为harbor的项目,设置不公开。当项目设为公开后,任何人都有此项目下镜像的读权限。命令行用户不需要docker login就可以拉取此项目下的镜像。 ![harbor项目.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/813712358.png) 登录10.60.60.23(测试客户端),修改下daemon.json(没有就自己创建下) ```shell vim /etc/docker/daemon.json #修改成如下的,因为是80端口,所以要加 "insecure-registries":["10.60.60.24:80"],其它的则国内镜像源 { "insecure-registries":[ "10.60.60.24:80" ], "registry-mirrors" : [ "https://registry.docker-cn.com", "https://docker.mirrors.ustc.edu.cn", "http://hub-mirror.c.163.com", "https://cr.console.aliyun.com/" ] } ``` 重启下docker ```shell systemctl restart docker.service ``` docker info可以查看镜像源是否生效 ```shell [root@localhost ~]# docker info | grep -A 5 "Registry" Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 10.60.60.24:80 127.0.0.0/8 Registry Mirrors: https://registry.docker-cn.com/ https://docker.mirrors.ustc.edu.cn/ http://hub-mirror.c.163.com/ https://cr.console.aliyun.com/ Live Restore Enabled: false [root@localhost ~]# ``` 拉取一个nginx的镜像 ```shell docker pull nginx [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 3 months ago 141MB [root@localhost ~]# ``` 命令行登录Harbor ```shell docker login 10.60.60.24:80 --username admin --password Harbor12345 [root@localhost ~]# docker login 10.60.60.24:80 --username admin --password Harbor12345 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@localhost ~]# ``` 重新打下tag ```shell docker tag nginx:latest 10.60.60.24:80/harbor/xiaolei_nginx:v1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.60.60.24:80/harbor/xiaolei_nginx v1 605c77e624dd 3 months ago 141MB nginx latest 605c77e624dd 3 months ago 141MB [root@localhost ~]# ``` 上传到Harbor ```shell docker push 10.60.60.24:80/harbor/xiaolei_nginx:v1 [root@localhost ~]# docker push 10.60.60.24:80/harbor/xiaolei_nginx:v1 The push refers to repository [10.60.60.24:80/harbor/xiaolei_nginx] d874fd2bc83b: Pushed 32ce5f6a5106: Pushed f1db227348d0: Pushed b8d6e692a25e: Pushed e379e8aedd4d: Pushed 2edcec3590a4: Pushed v1: digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3 size: 1570 [root@localhost ~]# ``` 查看网页可以看到这个镜像 ![push成功.png](https://www.xiaoleizhang.com/usr/uploads/2022/04/350101370.png) 客户端把镜像删除,再pull下来试下 ```shell docker image rmi 10.60.60.24:80/harbor/xiaolei_nginx:v1 docker pull 10.60.60.24:80/harbor/xiaolei_nginx:v1 ``` **测试ok!!!** 最后修改:2022 年 04 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏
1 条评论
nn