为什么需要 Docker 私有仓库
虽然 Docker 官方提供了 Docker 镜像仓库服务 ,但是由于网络原因或者公司内部的原因我们需要一个私有的 Docker 仓库来管理自己的 Docker 镜像。
高可用 Docker 私有仓库架构的介绍
本例中的 Docker 私有仓库是基于开源的软件 Harbor 来搭建的,只对 Harbor 自带的 Nginx 做了替换(推荐使用 Tengine)。集群的服务器使用了两台滴滴云的 DC2 实例。
- SLB 负责提供带外的服务地址以及 DC2 实例的负载均衡。
- Nginx 对 Harbor-UI 和 Registry 做负载均衡以及健康检查,确保一台 DC2 上面的组件不可用时不影响整体的服务。
- Harbor-UI 提供 Web 和生成鉴权 Token 的功能。
- Registry 提供了 Docker 镜像的管理功能。
- Harbor-Jobservice 异步处理 job 任务。
- Harbor-Adminserver 配置管理。
- Harbor-Log 日志收集。
- Redis 用于数据的缓存和消息队列的实现。
- MySQL 存储用户信息和仓库信息。
- S3 存储 Docker 镜像。
环境准备
在滴滴云官网依次购买以下组件:
服务部署
登陆滴滴云服务器进行如下操作 ( 本例使用的操作系统版本为 Centos7.4 ):
1. 安装部署 Docker、Docker-Compose
1 2 3 4 5 6 7 |
yum install docker docker-compose -y systemctl enable docker systemctl start docker |
2. 下载 Harbor
1 2 3 4 5 6 7 8 9 10 11 |
mkdir -p /home/docker cd /home/docker wget -S http://harbor.orientsoft.cn/harbor-v1.5.0/harbor-offline-installer-v1.5.0.tgz tar xzvf harbor-offline-installer-v1.5.0.tgz cd harbor |
3. 修改 Harbor.cfg 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
vim harbor.cfg ... hostname = 滴滴云SLB地址 ... db_host = 滴滴云MySQL地址 db_password = 滴滴云MySQL账号密码 db_user = 滴滴云MySQL账号 ... redis_url = 滴滴云Redis地址:6379 ... registry_storage_provider_name = s3 registry_storage_provider_config = accesskey: S3对象存储密钥的SecretID, secretkey: S3对象存储密钥的SecretKey, region: S3所在的区域, regionendpoint: s3.didiyunapi.com, bucket: s3存储桶的名字, chunksize: 5242880, multipartcopychunksize: 33554432, multipartcopymaxconcurrency: 100, multipartcopythresholdsize: 33554432 ... |
4. 修改 Nginx 配置文件(Nginx 的版本需要支持 check 模块)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
vim common/templates/nginx/nginx.http.conf ... upstream registry { server 第一台滴滴云主机内网IP:5000; server 第二台滴滴云主机内网IP:5000; check interval=5000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx http_4xx; } upstream ui { server 第一台滴滴云主机内网IP:8080; server 第二台滴滴云主机内网IP:8080; check interval=5000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx http_4xx; } ... location /v2/ { proxy_pass http://registry; proxy_set_header Host $$http_host; proxy_set_header X-Real-IP $$remote_addr; proxy_set_header X-Forwarded-For $$proxy_add_x_forwarded_for; # When setting up Harbor behind other proxy, such as an Nginx instance, remove the below line if the proxy already has similar settings. proxy_set_header X-Forwarded-Proto $$scheme; proxy_buffering off; proxy_request_buffering off; } |
5. 修改 Docker-Compose.yml 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
... registry: image: vmware/registry-photon:v2.6.2-v1.5.0 ... ports: - 5000:5000 depends_on: - log ... ... ui: image: vmware/harbor-ui:v1.5.0 ... ports: - 8080:8080 depends_on: - log - adminserver - registry ... ... proxy: image: 实际使用的nginx版本 ... |
6. 安装 Harbor
1 2 3 4 5 |
sh install.sh docker ps //查看进程运行的状态 |
7. 鉴权公钥私钥同步
用一台机器上的 common/config/ui/private_key.pem
和 common/config/registry/root.crt
文件替换另外一台机器上的文件。否则会导致集群内鉴权失败。
8. 配置滴滴云 SLB 和安全组规则
9. 服务验证
- 浏览器中输入 SLB_IP 即可访问 Harbor 的 Web 服务。
- 通过 Web 页面注册用户、创建项目即可通过
docker login
、docker push
命令上传 Docker 镜像了。
本文作者:杜建成