mkdir /datamkdir /root/datacd /root/data# 创建自已的CA证书openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt#------------------------------------------------------------Country Name (2 letter code) [AU]:CNState or Province Name (full name) [Some-State]:HarbinLocality Name (eg, city) []:HarbinOrganization Name (eg, company) [Internet Widgits Pty Ltd]:ydgwOrganizational Unit Name (eg, section) []:ydgwCommon Name (e.g. server FQDN or YOUR name) []:10.240.4.159Email Address []:liuyajun@ydgw.cn#------------------------------------------------------------# 生成一个证书签名请求openssl req -newkey rsa:4096 -nodes -sha256 -keyout 10.240.4.159.key -out 10.240.4.159.csr#------------------------------------------------------------Country Name (2 letter code) [AU]:CNState or Province Name (full name) [Some-State]:HarbinLocality Name (eg, city) []:HarbinOrganization Name (eg, company) [Internet Widgits Pty Ltd]:ydgwOrganizational Unit Name (eg, section) []:ydgwCommon Name (e.g. server FQDN or YOUR name) []:10.240.4.159Email Address []:liuyajun@ydgw.cnPlease enter the following 'extra' attributesto be sent with your certificate requestA challenge password []: #密码留空即可An optional company name []:#------------------------------------------------------------# 创建文件夹和辅助内容mkdir demoCAcd demoCAtouch index.txtecho '01' > serialcd ..ll#------------------------------------------------------------total 28drwxr-xr-x3 root root 4096 Jan 3022:11 ./drwx------ 5 root root 4096 Jan 3022:09 ../-rw-r--r-- 1 root root 1740 Jan 3101:3810.240.4.159.csr-rw-r--r-- 1 root root 3272 Jan 3101:3810.240.4.159.key-rw-r--r-- 1 root root 2098 Jan 3101:37 ca.crt-rw-r--r-- 1 root root 3272 Jan 3101:37 ca.keydrwxr-xr-x2 root root 4096 Jan 3101:39 demoCA/#------------------------------------------------------------# 签名证书echo subjectAltName = IP:10.240.4.159 > extfile.cnfopenssl ca -in 10.240.4.159.csr -out 10.240.4.159.crt -cert ca.crt -keyfile ca.key -extfile extfile.cnf -days 3650 -outdir .#------------------------------------------------------------Using configuration from /usr/lib/ssl/openssl.cnfCheck that the request matches the signatureSignature okCertificate Details: Serial Number: 1 (0x1) Validity Not Before: Jan 3106:39:392018 GMT Not After : Jan 3106:39:392019 GMT Subject: countryName = CN stateOrProvinceName = Harbin organizationName = ydgw organizationalUnitName = ydgw commonName = 10.240.4.159 emailAddress = liuyajun@ydgw.cn X509v3 extensions: X509v3 Subject Alternative Name: IP Address:10.240.4.159Certificate is to be certified until Jan 3106:39:392019 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated#------------------------------------------------------------ll#------------------------------------------------------------total 48drwxr-xr-x 3 root root 4096 Jan 30 22:20 ./drwx------ 5 root root 4096 Jan 3022:09 ../-rw-r--r-- 1 root root 6873 Jan 3101:3901.pem-rw-r--r-- 1 root root 6873 Jan 3101:3910.240.4.159.crt-rw-r--r-- 1 root root 1740 Jan 3101:3810.240.4.159.csr-rw-r--r-- 1 root root 3272 Jan 3101:3810.240.4.159.key-rw-r--r-- 1 root root 2098 Jan 3101:37 ca.crt-rw-r--r-- 1 root root 3272 Jan 3101:37 ca.keydrwxr-xr-x2 root root 4096 Jan 3101:39 demoCA/-rw-r--r-- 1 root root 33 Jan 3101:39 extfile.cnf#------------------------------------------------------------# 证书加入本机信任cp 10.240.4.159.crt /usr/local/share/ca-certificates/update-ca-certificates# 重启docker使证书生效systemctl daemon-reloadsystemctl restart docker
上述安装使用的IP地址曾用域名配置,但启动harbor后,docker login总会报类似以下错误信息,调了两天也没有找到解决办法,最后只好放弃 docker login reg.ydgw.cn Username: admin Password: Error response from daemon: Get https://reg.ydgw.cn/v2/: x509: certificate is not valid for any names, but wanted to match reg.ydgw.cn
配置安装启动harbor
# 进入harbor的触压后的目录cd harbor#------------------------------------------------------------lltotal 934372drwxr-xr-x3 root root 4096 Jan 3022:40 ./drwx------ 6 root root 4096 Jan 3022:40 ../drwxr-xr-x3 root root 4096 Jan 3022:40 common/-rw-r--r-- 1 root root 1119 Jan 405:33 docker-compose.clair.yml-rw-r--r-- 1 root root 1702 Jan 405:33 docker-compose.notary.yml-rw-r--r-- 1 root root 3303 Jan 405:33 docker-compose.yml-rw-r--r-- 1 root root 4304 Jan 405:33 harbor_1_1_0_template-rw-r--r-- 1 root root 5008 Jan 405:33 harbor.cfg-rw-r--r-- 1 root root 955424047 Jan 405:39 harbor.v1.3.0.tar.gz-rwxr-xr-x1 root root 5332 Jan 405:33 install.sh*-rw-r--r-- 1 root root 1284054 Jan 405:33 LICENSE-rw-r--r-- 1 root root 481 Jan 405:33 NOTICE-rwxr-xr-x1 root root 18882 Jan 405:33 prepare*-rwxr-xr-x1 root root 4550 Jan 405:33 upgrade*#------------------------------------------------------------vi harbor.cfg# 更改以下几项内容#------------------------------------------------------------hostname = reg.ydgw.cnui_url_protocol = httpsssl_cert = /root/data/10.240.4.159.crtssl_cert_key = /root/data/10.240.4.159.keydb_password = xxxxxxx # MYSQL数据库密码,可以改复杂些的harbor_admin_password = xxxxxxxx # harbor admin用户密码,后在WEB界面也能改#------------------------------------------------------------# 生成配置文件./prepare# 启动harbor(第一次启动,需要pull一些镜像)docker-compose up -d
ll /data#------------------------------------------------------------total 36drwxr-xr-x8 root root 4096 Jan 3101:42 ./drwxr-xr-x25 root root 4096 Jan 3021:02 ../drwxr-xr-x210000100004096 Jan 3101:42 ca_download/drwxr-xr-x210000100004096 Jan 3101:42 config/drwxr-xr-x510000100004096 Jan 3101:42 database/drwxr-xr-x210000100004096 Jan 3101:42 job_logs/drwxr-xr-x210000100004096 Jan 3101:42 psc/drwxr-xr-x210000100004096 Jan 3101:42 registry/-rw------- 1100001000016 Jan 3101:42 secretkey#------------------------------------------------------------
# 客户端不安装证书直接登陆会报以下错误docker login 10.240.4.159Username: adminPassword: Error response from daemon: Get https://10.240.4.159/v2/: x509: certificate signed by unknown authority# 将证书拷贝到如10.240.4.160客户机上并信任scp 10.240.4.159.crt 10.240.4.160:/usr/local/share/ca-certificates/# 在10.240.4.160客户机上执行update-ca-certificates# 重启docker使证书生效systemctl daemon-reloadsystemctl restart docker# 之后就可以正常登陆了docker login 10.240.4.159Username: adminPassword: Login Succeeded
上传镜像到harbor
在harbor中新建一个os的项目,访问级别设置为公开
点击os项目,推送镜像可以看到命令提示
# 先下载官方的centos镜像docker pull centos:7.4.1708# 修改TAG标签docker tag centos:7.4.170810.240.4.159/os/centos:7.4.1708docker images | grep centos10.240.4.159/os/centos 7.4.17083afd47092a0e 2 months ago 197MBcentos 7.4.17083afd47092a0e 2 months ago 197MB# 推送镜像(需要login)docker push10.240.4.159/os/centos:7.4.1708
重新刷新后,harbor中已经能看到推送的镜像了
Harbor的生命周期
cd harbor# 停止和启动docker-compose stopdocker-compose start# 要更改Harbor的配置,请首先停止现有的Harbor实例并进行更新harbor.cfg。然后运行prepare脚本来填充配置。最后重新创建并启动Harbor的实例:docker-compose down -v # 删除Harbor 的容器,同时保留图像数据和Harbor的数据库文件在文件系统上vi harbor.cfg./preparedocker-compose up -d