准备

apt install python3-pip
ln -s /usr/bin/python3 /usr/bin/python
安装docker-compose
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U docker-compose
查看docker-compose版本
**docker-compose -v**

安装Harbor

下载harbor
wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz
解压离线安装包
tar -zxf harbor-offline-installer-v2.5.0.tgz
修改配置文件
cd harbor
cp harbor.yml.tmpl harbor.yml

  1. # mkdir -p /opt/application/harbor //用于存放harbor的持久化数据
  2. harbor.yml配置文件主要修改参数如下:
  3. vim harbor.yml
  4. hostname: 192.168.0.8 //设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost。默认情况下,harbor使用的端口是80,若使用自定义的端口,除了要改docker-compose.yml文件中的配置外,这里的hostname也要加上自定义的端口,否则在docker login、push时会报错
  5. #http配置
  6. http:
  7. # port for http, default is 80. If https enabled, this port will redirect to https port
  8. port: 9999
  9. #https配置(如不需要可不配置,注释掉)
  10. # https related config
  11. #https:
  12. # https port for harbor, default is 443
  13. #port: 443
  14. # The path of cert and key files for nginx
  15. #certificate: /your/certificate/path
  16. #private_key: /your/private/key/path
  17. #external_url: https://reg.mydomain.com:8433 //如果要启用外部代理,比如外层的NGINX、LB等,请取消注释external_url,当它启用时,hostname将不再使用。
  18. harbor_admin_password: Harbor12345 //admin密码
  19. #数据库配置
  20. database:
  21. # The password for the root user of Harbor DB. Change this before any production use.
  22. password: root123
  23. # The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
  24. max_idle_conns: 50
  25. # The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
  26. # Note: the default number of connections is 100 for postgres.
  27. max_open_conns: 100
  28. #持久化数据目录
  29. data_volume: /opt/application/harbor
  1. 默认是使用 HTTP 协议,我们可以配置证书并使用 HTTPS 来访问 Harbor
  2. 2.3 配置HTTPS
  3. 说实话我开始是抵触的,我不想去使用它,但是当我考虑到,我不可能暂停我所有已经运行的容器来修改配置,来解决 docker login的问题,所有我只能老老实实的来配置 HTTPS ,这个烦人的 HTTPS
  4. 官方配置文档: https://github.com/goharbor/harbor/blob/master/docs/configure_https.md
  5. 我们这里演示的是创建自己的 证书,实际生产环境中我们可以去阿里云或者其他云服务器厂商申请免费的 证书。
  6. 创建证书
  7. # 创建存放证书的目录
  8. mkdir -p /data/cert/
  9. cd /data/cert/
  10. # 创建自签名证书key文件
  11. openssl genrsa -out ca.key 4096
  12. openssl req -x509 -new -nodes -sha512 -days 3650 \
  13. -subj "/C=TW/ST=Taipei/L=Taipei/O=example/OU=Personal/CN=192.168.15.170" \
  14. -key ca.key \
  15. -out ca.crt #CN 替换为你的仓库域名
  16. 修改配置
  17. #配置 HTTPS 配置
  18. https:
  19. # # https port for harbor, default is 443
  20. port: 443
  21. # # The path of cert and key files for nginx
  22. certificate: /data/cert/ca.crt
  23. private_key: /data/cert/ca.key
  24. 重新初始化 Harbor
  25. # 暂停
  26. docker-compose down -v
  27. prepare # 生成配置文件,根据 harbor.yml 配置生成docker-compose文件。
  28. docker-compose up -d # 后台启动
  29. 客户端配置

开始安装
./install.sh
用浏览器访问前面配置的hostname测试是否安装成功

Harbor如何停止与启动
# cd /root/harbor //切换到harbor安装包目录
# docker-compose stop //停止Harbor
# docker-compose start //启动Harbor
# docker-compose restart //重启Harbor

上传和下载

客户机添加安全仓库
docker1.4版本以后docker registry使用的是https,但是Harbor默认使用的是http方式,上传下载时会报错,可以通过修改本机/etc/docker/daemon.json文件解决

  1. vim /etc/docker/daemon.json
  2. # 将安全仓库设为服务器ip
  3. {
  4. "insecure-registries": ["192.168.68.20:8520"]
  5. }
  6. # 重启docker使配置生效
  7. systemctl restart docker

进入网页创建用户和一个私有项目,并把用户加入私有项目中

将本地镜像重写tag
docker tag hello-world:latest 192.168.68.20:8520/cs/hello-world:v1
新tag格式为:服务器IP/项目名/镜像名:版本

登陆harbor仓库,并上传下载

  1. docker login 192.168.68.20:8520
  2. # 然后根据提示输入用户名和密码
  3. docker pull hello-world:latest
  4. docker tag hello-world:latest 192.168.68.20:8520/cs/hello-world:v1
  5. # 下载和上传
  6. docker push 192.168.68.20:8520/cs/hello-world:v1
  7. docker pull 192.168.68.20:8520/cs/hello-world:v1

harbor开机自启

  1. sudo vim /etc/rc.local
  2. 添加
  3. cd $HOME/harbor && docker-compose up -d

Harbor使用外部代理

harbor默认只能使用harbor.yml中hostname指定的ip或主机名作为web访问地址,但在实际使用过程中,一般不允许ip地址或者主机名直接暴露在外访问,故需要配置nginx代理,通过代理后指定的地址进行访问。
配置方式:
修改harbor.yml,把https相关的注释(如果没有注释,http会自动重定向到https,导致多次重定向),然后添加external_url配置:

  1. # Configuration file of Harbor
  2. # The IP address or hostname to access admin UI and registry service.
  3. # DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
  4. hostname: 192.168.0.8:9999
  5. # http related config
  6. http:
  7. # port for http, default is 80. If https enabled, this port will redirect to https port
  8. port: 9999
  9. # https related config
  10. #https:
  11. # https port for harbor, default is 443
  12. #port: 443
  13. # The path of cert and key files for nginx
  14. #certificate: /your/certificate/path
  15. #private_key: /your/private/key/path
  16. # # Uncomment following will enable tls communication between all harbor components
  17. # internal_tls:
  18. # # set enabled to true means internal tls is enabled
  19. # enabled: true
  20. # # put your cert and key files on dir
  21. # dir: /etc/harbor/tls/internal
  22. # Uncomment external_url if you want to enable external proxy
  23. # And when it enabled the hostname will no longer used
  24. external_url: https://harbor.xxx.cn #如果这里是https,nginx代理就需要配置ssl……

修改配置后docker-compose down停止所有服务,删除当前配置目录:rm -rf ./common/config下配置清单,重新执行install.sh生成配置

NGINX外部代理配置文件:

  1. server {
  2. listen 80;
  3. server_name harbor.xxx.cn;
  4. #client_max_body_size 1000M;
  5. access_log /data/wwwlogs/harbor.xxx.cn_access.log combined;
  6. rewrite ^(.*) https://$server_name$1 permanent;
  7. location / {
  8. #proxy_redirect off;
  9. #proxy_set_header Host $host;
  10. #proxy_set_header X-Real-IP $remote_addr;
  11. #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. proxy_pass http://127.0.0.1:9999;
  13. }
  14. }
  15. ##########################ssl#######################
  16. server {
  17. listen 443 ssl;
  18. server_name harbor.xxx.cn;
  19. ssl_certificate sslkey/harbor.xxx.cn_chain.crt;
  20. ssl_certificate_key sslkey/harbor.xxx.cn_key.key;
  21. ssl_session_cache shared:SSL:1m;
  22. ssl_session_timeout 5m;
  23. ssl_ciphers HIGH:!aNULL:!MD5;
  24. ssl_prefer_server_ciphers on;
  25. access_log /data/wwwlogs/harbor.xxx.cn_access.log combined;
  26. location / {
  27. #proxy_redirect off;
  28. #proxy_set_header Host $host;
  29. #proxy_set_header X-Real-IP $remote_addr;
  30. #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31. proxy_pass http://127.0.0.1:9999;
  32. }
  33. }

注意:这几项配置都不要,注释掉,否则在pull和push镜像的时候会报错。

  1. #proxy_redirect off;
  2. #proxy_set_header Host $host;
  3. #proxy_set_header X-Real-IP $remote_addr;
  4. #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

具体报错如下:
# docker push harbor.xxx.cn/xxx/ubuntu
The push refers to repository [harbor.xxx.cn/xxx/ubuntu]
7555a8182c42: Pushing [==================================================>] 72.78MB/72.78MB
unknown blob

docker push harbor.xxx.cn/xxx/ubuntu
The push refers to repository [harbor.xxx.cn/xxx/ubuntu]
7555a8182c42: Pushing [==================================================>] 72.78MB/72.78MB
dial tcp 127.0.0.1:9999: connect: connection refused

另外一个报错:push的镜像文件比较大的时候
error parsing HTTP 413 response body: invalid character ‘<’ looking for beginning of value: “\r\n\r\n\r\n

413 Request Entity Too Large

\r\n
nginx
\r\n\r\n\r\n”
解决方式:
修改外部代理nginx配置文件:nginx.conf
client_max_body_size默认为0, 修改0为特定的大小即可。如 client_max_body_size 102400M