Harbor仓库搭建
Harbor 搭建
Harbor介绍
Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署私有环境内的Registry也是非常必要的。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能
环境准备
Harbor的所有服务组件都是在Docker中部署的,所以官方安装使用Docker-compose快速部署,所以需要安装Docker、Docker-compose。由于Harbor是基于Docker Registry V2版本,所以就要求Docker版本不小于1.10.0,Docker-compose版本不小于1.6.0
安装过程
安装docker
参考:https://www.cnblogs.com/zippo123/p/11064084.html
安装Docker-compose
- 下载指定版本的docker-compose
[root@localhost harbor]# sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 633 100 633 0 0 778 0 --:--:-- --:--:-- --:--:-- 779 100 11.2M 100 11.2M 0 0 37451 0 0:05:13 0:05:13 --:--:-- 42285
- 对二进制文件进行赋权
[root@localhost harbor]# chmod +x /usr/local/bin/docker-compose
- 测试下docker-compose是否安装成功
[root@localhost harbor]# docker-compose --version docker-compose version 1.22.0, build f46880fe
Harbor服务搭建及启动
下载Harbor安装文件
从GitHub上https://github.com/goharbor/harbor/releases下载指定版本的安装包
[root@localhost harbor]# wget https://github.com/vmware/harbor/releases/download/v1.1.2/harbor-online-installer-v1.1.2.tgz [root@localhost harbor]# tar -zxf harbor-online-installer-v1.1.2.tgz
配置Harbor
[root@localhost harbor]# cd harbor [root@localhost harbor]# ls common docker-compose.notary.yml docker-compose.yml harbor_1_1_0_template harbor.cfg install.sh LICENSE NOTICE prepare upgrade [root@localhost harbor]# vi harbor.cfg
配置文件harbor.cfg详解:
# hostname设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost hostname = 192.168.126.162 # 访问协议,默认是http,也可以设置https,如果设置https,则nginx ssl需要设置on ui_url_protocol = http # mysql数据库root用户默认密码root123,实际使用时修改下 db_password = 123456 max_job_workers = 3 customize_crt = on ssl_cert = /data/cert/server.crt ssl_cert_key = /data/cert/server.key secretkey_path = /data admiral_url = NA # 邮件设置,发送重置密码邮件时使用 email_identity = email_server = smtp.mydomain.com email_server_port = 25 email_username = sample_admin@mydomain.com email_password = abc email_from = admin <sample_admin@mydomain.com> email_ssl = false # 启动Harbor后,管理员UI登录的密码,默认是Harbor12345 harbor_admin_password = 123456 # 认证方式,这里支持多种认证方式,如LADP、本次存储、数据库认证。默认是db_auth,mysql数据库认证 auth_mode = db_auth # LDAP认证时配置项 ldap_url = ldaps://ldap.mydomain.com #ldap_searchdn = uid=searchuser,ou=people,dc=mydomain,dc=com #ldap_search_pwd = password ldap_basedn = ou=people,dc=mydomain,dc=com #ldap_filter = (objectClass=person) ldap_uid = uid ldap_scope = 3 ldap_timeout = 5 # 是否开启自注册 self_registration = on # token有效时间,默认30分钟 token_expiration = 30 # 用户创建项目权限控制,默认是everyone(所有人),也可以设置为adminonly(只能管理员) project_creation_restriction = everyone verify_remote_cert = on
启动Harbor
修改完配置文件后,在的当前目录执行./install.sh,Harbor服务就会根据当期目录下的docker-compose.yml开始下载依赖的镜像,检测并按照顺序依次启动各个服务
[root@localhost harbor]#./install.sh
Harbor依赖的镜像及启动服务如下:
[
root@localhost harbor]# pwd /root/harbor/harbor [root@localhost harbor]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ harbor-adminserver /harbor/harbor_adminserver Up harbor-db docker-entrypoint.sh mysqld Up 3306/tcp harbor-jobservice /harbor/harbor_jobservice Up harbor-log /bin/sh -c crond && rm -f ... Up 127.0.0.1:1514->514/tcp harbor-ui /harbor/harbor_ui Up nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:4443->4443/tcp,:::4443->4443/tcp, 0.0.0.0:30050->80/tcp,:::30050->80/tcp registry /entrypoint.sh serve /etc/ ... Up 5000/tcp [root@localhost harbor]#
如果执行命令报如下错误,检查下当前执行命令所在目录是否正确(要在docker-compose.yml 所在目录下执行)
启动完成后,访问刚设置的hostname即可,默认是80端口,如果端口占用,可以去修改docker-compose.yml文件中,对应服务的端口映射
修改端口
在公网上,一般情况下都不暴露默认端口,避免被攻击!
以下修改harbor的默认80端口为其他端口!
我这里示例修改为30050端口!
1、修改docker-compose.yml文件映射为30050端口:
vim docker-compose.yml
proxy:
image: vmware/nginx:1.11.5-patched
container_name: nginx
restart: always
volumes:
- ./common/config/nginx:/etc/nginx:z
networks:
- harbor
ports:
- 30050:80
- 443:443
- 4443:4443
depends_on:
- mysql
- registry
- ui
- log
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "proxy"
2.修改common/templates/registry/config.yml文件加入30050端口
auth: token: issuer: harbor-token-issuer realm: $ui_url:30050/service/token rootcertbundle: /etc/registry/root.crt service: harbor-registry
- 停止harbor,重新启动并生成配置文件:
# docker-compose stop Stopping nginx ... done Stopping harbor-jobservice ... done Stopping harbor-ui ... done Stopping harbor-db ... done Stopping registry ... done Stopping harbor-adminserver ... done Stopping harbor-log ... # ./install.sh
测试是否修改成功
[root@localhost docker]# docker login 192.168.130.12:30050 Username: admin Password: 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
出现如上信息则登录成功,如果出现如下报错
则修改/etc/docker/daemon.json 文件,加入"insecure-registries":["192.168.130.12:30050"]
{ "registry-mirrors": ["https://5md0553g.mirror.aliyuncs.com"] , "insecure-registries":["192.168.130.12:30050"] }
再重启服务
systemctl daemon-reload
systemctl restart docker