> /etc/hosts把yourdomain.com换成实际使用的域名或者ip或者ip:port,要跟harbor.yml文件中的配置信息保_为什么harbor需要域名访">
当前位置:   article > 正文

Harbor仓库配置https访问_为什么harbor需要域名访问

为什么harbor需要域名访问

注:高版本(14以上)docker执行login命令,默认使用https,且harbor必须使用域名,只是用ip访问是不行的。

假设使用的网址是:www.harbor.mobi,本机ip是172.16.5.45

因为这个网址是虚拟的,所以需要在本机hosts文件中添加

echo "172.16.5.45  www.harbor.mobi" >> /etc/hosts

把yourdomain.com换成实际使用的域名或者ip或者ip:port,要跟harbor.yml文件中的配置信息保持一致

  1. #set hostname
  2. hostname: www.harbor.mobi
  3. #http:
  4. # port: 80
  5. https:
  6. # https port for harbor, default is 443
  7. port: 443
  8. # The path of cert and key files for nginx
  9. certificate: /data/cert/www.harbor.mobi.crt
  10. private_key: /data/cert/www.harbor.mobi.key
  11. # 注意证书路径

一键脚本文件:

  1. #!/bin/bash
  2. # 在该目录下操作生成证书,正好供harbor.yml使用
  3. mkdir -p /data/cert
  4. cd /data/cert
  5. openssl genrsa -out ca.key 4096
  6. openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.harbor.mobi" -key ca.key -out ca.crt
  7. openssl genrsa -out www.harbor.mobi.key 4096
  8. openssl req -sha512 -new -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.harbor.mobi" -key www.harbor.mobi.key -out www.harbor.mobi.csr
  9. cat > v3.ext <<-EOF
  10. authorityKeyIdentifier=keyid,issuer
  11. basicConstraints=CA:FALSE
  12. keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  13. extendedKeyUsage = serverAuth
  14. subjectAltName = @alt_names
  15. [alt_names]
  16. DNS.1=www.harbor.mobi
  17. DNS.2=harbor
  18. DNS.3=ks-allinone
  19. EOF
  20. openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in www.harbor.mobi.csr -out www.harbor.mobi.crt
  21. openssl x509 -inform PEM -in www.harbor.mobi.crt -out www.harbor.mobi.cert
  22. cp www.harbor.mobi.crt /etc/pki/ca-trust/source/anchors/www.harbor.mobi.crt
  23. update-ca-trust
  1. # 把这三个复制到docke下
  2. mkdir -p /etc/docker/certs.d/www.harbor.mobi/
  3. cp www.harbor.mobi.cert /etc/docker/certs.d/www.harbor.mobi/
  4. cp www.harbor.mobi.key /etc/docker/certs.d/ywww.harbor.mobi/
  5. cp ca.crt /etc/docker/certs.d/www.harbor.mobi/
  6. 最终docker目录结构:
  7. /etc/docker/certs.d/
  8. └── www.harbor.mobi
  9. ├── www.harbor.mobi.cert <-- Server certificate signed by CA
  10. ├── www.harbor.mobi.key <-- Server key signed by CA
  11. └── ca.crt <-- Certificate authority that signed the registry certificate
  12. # 重启docker
  13. systemctl restart docker.service
  14. # 停止
  15. docker-compose down -v
  16. # 重新生成配置文件
  17. ./prepare --with-notary --with-clair --with-chartmuseum
  18. # 启动
  19. docker-compose up -d

官方步骤示例:

  1. #set hostname
  2. hostname: yourdomain.com
  3. http:
  4. port: 80
  5. https:
  6. # https port for harbor, default is 443
  7. port: 443
  8. # The path of cert and key files for nginx
  9. certificate: /data/cert/yourdomain.com.crt
  10. private_key: /data/cert/yourdomain.com.key
  1. # 生成使用的相关证书
  2. openssl genrsa -out ca.key 4096
  3. openssl req -x509 -new -nodes -sha512 -days 3650 \
  4. -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
  5. -key ca.key \
  6. -out ca.crt
  7. openssl genrsa -out yourdomain.com.key 4096
  8. openssl req -sha512 -new \
  9. -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
  10. -key yourdomain.com.key \
  11. -out yourdomain.com.csr
  12. cat > v3.ext <<-EOF
  13. authorityKeyIdentifier=keyid,issuer
  14. basicConstraints=CA:FALSE
  15. keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  16. extendedKeyUsage = serverAuth
  17. subjectAltName = @alt_names
  18. [alt_names]
  19. DNS.1=yourdomain.com
  20. DNS.2=yourdomain
  21. DNS.3=hostname
  22. EOF
  23. openssl x509 -req -sha512 -days 3650 \
  24. -extfile v3.ext \
  25. -CA ca.crt -CAkey ca.key -CAcreateserial \
  26. -in yourdomain.com.csr \
  27. -out yourdomain.com.crt
  28. openssl x509 -inform PEM -in yourdomain.com.crt -out yourdomain.com.cert
  29. # 把这三个复制到docke下
  30. cp yourdomain.com.cert /etc/docker/certs.d/yourdomain.com/
  31. cp yourdomain.com.key /etc/docker/certs.d/yourdomain.com/
  32. cp ca.crt /etc/docker/certs.d/yourdomain.com/
  33. 最终docker目录结构:
  34. /etc/docker/certs.d/
  35. └── yourdomain.com:port
  36. ├── yourdomain.com.cert <-- Server certificate signed by CA
  37. ├── yourdomain.com.key <-- Server key signed by CA
  38. └── ca.crt <-- Certificate authority that signed the registry certificate
  39. # 重启docker
  40. systemctl restart docker.service
  41. cp 192.168.75.100.crt /etc/pki/ca-trust/source/anchors/192.168.75.100.crt
  42. update-ca-trust
  43. # harbor证书配置
  44. cp yourdomain.com.crt /data/cert/
  45. cp yourdomain.com.key /data/cert/
  46. # 重新生成配置文件
  47. ./prepare --with-notary --with-clair --with-chartmuseum
  48. # 停止
  49. docker-compose down -v
  50. # 启动
  51. docker-compose up -d

然后docker直接login即可

  1. root@eb7023:/data/certs>docker login www.harbor.mobi
  2. Username: admin
  3. Password:
  4. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  5. Configure a credential helper to remove this warning. See
  6. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  7. Login Succeeded

这里我的docker和harbor是在同一台机器上的,如果是其他机器也复制crt文件即可

  1. root@eb7023:/data/certs>scp www.harbor.mobi.crt root@eb7045:/etc/docker/certs.d/www.harbor.mobi/www.harbor.mobi.crt
  2. root@eb7045's password:
  3. www.harbor.mobi.crt 100% 1830 2.1MB/s 00:00

在eb7045可以登录验证一下:

  1. root@eb7045:/etc/docker/certs.d/harbor23.com>docker login www.harbor.mobi
  2. Username: admin
  3. Password:
  4. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  5. Configure a credential helper to remove this warning. See
  6. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  7. Login Succeeded

到这里配置完成 !

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/977383
推荐阅读
相关标签
  

闽ICP备14008679号