这篇文章主要介绍“Linux下如何部署Harbor”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Linux下如何部署Harbor”文章能帮助大家解决问题。

Harbor 是为企业用户设计的容器镜像仓库开源项目,包括了权限管理(RBAC)、LDAP、审计、安全漏洞扫描、镜像验真、管理界面、自我注册、HA 等企业必需的功能,同时针对中国用户的特点,设计镜像复制和中文支持等功能。

配置架构图

如上图所示,harbor由6大模块级成:

Proxy: Harbor的registry、UI、token services等组件,都处在一个反向代理后边。该代理将来自浏览器、docker clients的请求转发到后端服务上。

Registry: 负责存储Docker镜像,以及处理Docker push/pull请求。因为Harbor强制要求对镜像的访问做权限控制, 在每一次push/pull请求时,Registry会强制要求客户端从token service那里获得一个有效的token。

Core services: Harbor的核心功能,主要包括如下3个服务:

UI: 作为Registry Webhook, 以图像用户界面的方式辅助用户管理镜像。

\1) WebHook是在registry中配置的一种机制, 当registry中镜像发生改变时,就可以通知到Harbor的webhook endpoint。Harbor使用webhook来更新日志、初始化同步job等。

\2) Token service会根据该用户在一个工程中的角色,为每一次的push/pull请求分配对应的token。假如相应的请求并没有包含token的话,registry会将该请求重定向到token service。

\3) Database 用于存放工程元数据、用户数据、角色数据、同步策略以及镜像元数据。

Job services: 主要用于镜像复制,本地镜像可以被同步到远程Harbor实例上。

Log collector: 负责收集其他模块的日志到一个地方

组件说明

需要说明的是,harbor的每个组件都是以Docker容器的形式构建的,可以使用Docker Compose来进行部署,当然,如果你的环境中使用了kubernetes,harbor也提供了kubernetes的配置文件。

harbor共有8个容器组成:

ui:harbor的核心服务。

log:运行着rsyslog的容器,进行日志收集。

mysql:由官方mysql镜像构成的数据库容器

nginx:使用Nginx做反向代理

registry:官方的Docker registry

adminserver:harbor的配置数据管理器

jobservice:Harbor的任务管理服务。

redis:用于存储session

下面我们所有的配置都以harbor 1.5.2版本作配置说明。

harbor可以支持http和https,建议使用https,https证书最好是受信任的ca颁发的证书,这样,在配置docker的时候,就不需要添加”insecure-registries”配置项。我们这里就直接使用自签名证书。

生成证书:

opensslgenrsa-des3-outserver.key1024opensslrsa-inserver.key-outserver.key#去除server.key的加密口令opensslreq-new-keyserver.key-outserver.csropensslx509-req-days365-inserver.csr-signkeyserver.key-outserver.crt

将证书放入/data/certs/目录下:

cpserver.crtserver.key/data/certsharbor.cfg配置文件示例

harbor的核心配置文件是harbor.cfg,路径在源码目录下的make目录下,示例配置如下:

_version=1.5.0#harbor的访问地址hostname=hub.dz11.com#使用https协议ui_url_protocol=https#harbor的数据库密码db_password=xxxxxmax_job_workers=50customize_crt=on#证书相关路径ssl_cert=/data/certs/dz11.com.crtssl_cert_key=/data/certs/dz11.com.keysecretkey_path=/dataadmiral_url=NA#定义日志滚动log_rotate_count=50log_rotate_size=200Mhttp_proxy=https_proxy=no_proxy=127.0.0.1,localhost,ui#email相关配置email_identity=email_server=smtp.163.comemail_server_port=25email_username=yan_ruo_gu@163.comemail_password=yan_ruo_gu0711email_from=adminemail_ssl=false#登录harbor的管理员密码harbor_admin_password=xxxxxx#harbor的验证方式,支持db_auth和ldap_auth,这里使用的是ldap_auth,如果使用db_auth的话,直接将auth_mode修改为db_auth即可,ldap的配置不再需要。auth_mode=ldap_authldap_url=ldap://10.1.1.1:389ldap_searchdn=CN=jenkins,OU=LDAP,OU=Dev-wh.xxx.com,DC=dev-wh,DC=xxx,DC=comldap_search_pwd=xxxxldap_basedn=OU=Dev-wh.xxx.com,DC=dev-wh,DC=xxx,DC=comldap_filter=(objectClass=person)ldap_uid=0ldap_scope=2ldap_timeout=5self_registration=ontoken_expiration=30project_creation_restriction=everyoneverify_remote_cert=on#数据库相关配置,默认如果不需要使用自建的数据库,这些配置就都不需要db_host=mysqldb_password=root123db_port=3306db_user=rootredis_url=redis:6379clair_db_host=postgresclair_db_password=passwordclair_db_port=5432clair_db_username=postgresclair_db=postgresuaa_endpoint=uaa.mydomain.orguaa_clientid=iduaa_clientsecret=secretuaa_verify_cert=trueuaa_ca_cert=/path/to/ca.pemregistry_storage_provider_name=filesystemregistry_storage_provider_config=部署harbor

harbor支持docker-compose和kubernetes的部署方式,默认采用docker-compose作单机部署。

先执行./prepare,然后执行./install.sh进行启动。执行./install.sh的时候,即调用了docker-compose运行了当前目录下的docker-compose.yml文件。

在执行.prepare的时候抛出如下异常:

root@ubuntu:~/harbor#./prepareGeneratedandsavedsecrettofile:/data/secretkeyGeneratedconfigurationfile:./common/config/nginx/nginx.confGeneratedconfigurationfile:./common/config/adminserver/envGeneratedconfigurationfile:./common/config/ui/envGeneratedconfigurationfile:./common/config/registry/config.ymlGeneratedconfigurationfile:./common/config/db/envGeneratedconfigurationfile:./common/config/jobservice/envGeneratedconfigurationfile:./common/config/jobservice/config.ymlGeneratedconfigurationfile:./common/config/log/logrotate.confGeneratedconfigurationfile:./common/config/jobservice/config.ymlGeneratedconfigurationfile:./common/config/ui/app.confFailtogeneratekeyfile:./common/config/ui/private_key.pem,certfile:./common/config/registry/root.crt

需要修改prepare文件,将第498行:

empty_subj="/C=/ST=/L=/O=/CN=/"修改如下:empty_subj="/C=US/ST=California/L=PaloAlto/O=VMware,Inc./OU=Harbor/CN=notarysigner"

在实际启动过程中,出现过registry启动失败的情况,/var/log/harbor/registry.log输出如下:

May3021:06:00172.18.0.1registry[3218]:panic:unabletoconfigureauthorization(token):unabletoopentokenauthrootcertificatebundlefile"/etc/registry/root.crt":open/etc/registry/root.crt:permissiondeniedMay3021:06:00172.18.0.1registry[3218]:May3021:06:00172.18.0.1registry[3218]:goroutine1[running]:May3021:06:00172.18.0.1registry[3218]:panic(0xb4cd40,0xc4203ae160)May3021:06:00172.18.0.1registry[3218]:#011/usr/local/go/src/runtime/panic.go:500+0x1a1May3021:06:00172.18.0.1registry[3218]:github.com/docker/distribution/registry/handlers.NewApp(0x1067820,0xc4203a8630,0xc4202df180,0x1067820)May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/registry/handlers/app.go:302+0x1b6aMay3021:06:00172.18.0.1registry[3218]:github.com/docker/distribution/registry.NewRegistry(0x7fcfa30dd198,0xc4203a8630,0xc4202df180,0xe,0x0,0x0)May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/registry/registry.go:86+0x213May3021:06:00172.18.0.1registry[3218]:github.com/docker/distribution/registry.glob..func1(0x108f1a0,0xc42036d240,0x1,0x1)May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/registry/registry.go:55+0x106May3021:06:00172.18.0.1registry[3218]:github.com/docker/distribution/vendor/github.com/spf13/cobra.(*Command).execute(0x108f1a0,0xc42036d1f0,0x1,0x1,0x108f1a0,0xc42036d1f0)May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/vendor/github.com/spf13/cobra/command.go:495+0x190May3021:06:00172.18.0.1registry[3218]:github.com/docker/distribution/vendor/github.com/spf13/cobra.(*Command).Execute(0x108f340,0xc4201d7f40,0xc4200001a0)May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/vendor/github.com/spf13/cobra/command.go:560+0x3c3May3021:06:00172.18.0.1registry[3218]:main.main()May3021:06:00172.18.0.1registry[3218]:#011/go/src/github.com/docker/distribution/cmd/registry/main.go:24+0x2d

这是因为registry容器没有/etc/registry/root.crt的访问权限导致。这个文件默认是挂载的./common/config/registry/root.crt这个文件,所以我们需要对这个文件作授权。

通过观察可知,harbor的容器启动用户非root身份,而是以一个uid和gid都为10000的用户。所以只需要为该用户授权即可:

chown10000.10000./common/config/registry/root.crt

正常启动之后,配置完成。

注意事项

需要说明的是,harbor支持http和https,但如果使用http的话,在拉取镜像的时候,会抛出仓库不受信任的异常。需要在所有的docker客户端的docker配置文件/etc/docker/daemon.json中添加如下配置:

{"insecure-registries":["https://hub.dz11.com"],}

如果使用自签名的https证书,仍然会提示证书不受信任的问题。需要将自签名的ca证书发送到所有的docker客户端的指定目录。

关于“Linux下如何部署Harbor”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。