一:安装docker
二:安装docker-compose
三:安装Harbor
四:配置Harbor使用https和2376端口
在/etc/docker 目录下面创建文件:create_tls_certs.sh的文件,内容如下:
#创建 Docker TLS 证书
#!/bin/bash
#相关配置信息
SERVER="101.200.154.43"
PASSWORD="pass123456"
COUNTRY="CN"
STATE="广州省"
CITY="广州市"
ORGANIZATION="sefon"
ORGANIZATIONAL_UNIT="Dev"
EMAIL="edrp@qq.com"
###开始生成文件###
echo "开始生成文件"
#切换到生产密钥的目录
cd /etc/docker
#生成ca私钥(使用aes256加密)
openssl genrsa -aes256 -passout pass:$PASSWORD -out ca-key.pem 2048
#生成ca证书,填写配置信息
openssl req -new -x509 -passin "pass:$PASSWORD" -days 3650 -key ca-key.pem -sha256 -out ca.pem -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$SERVER/emailAddress=$EMAIL"
#生成server证书私钥文件
openssl genrsa -out server-key.pem 2048
#生成server证书请求文件
openssl req -subj "/CN=$SERVER" -new -key server-key.pem -out server.csr
#使用CA证书及CA密钥以及上面的server证书请求文件进行签发,生成server自签证书
openssl x509 -req -days 3650 -in server.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out server-cert.pem
#生成client证书RSA私钥文件
openssl genrsa -out key.pem 2048
#生成client证书请求文件
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
sh -c 'echo "extendedKeyUsage=clientAuth" > extfile.cnf'
#生成client自签证书(根据上面的client私钥文件、client证书请求文件生成)
openssl x509 -req -days 3650 -in client.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out cert.pem -extfile extfile.cnf
#更改密钥权限
chmod 0400 ca-key.pem key.pem server-key.pem
#更改密钥权限
chmod 0444 ca.pem server-cert.pem cert.pem
#删除无用文件
#rm client.csr server.csr
echo "生成文件完成"
###生成结束###
然后执行:
sh create_tls_certs.sh
修改docker配置:
vim /usr/lib/systemd/system/docker.service
修改
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
为:
ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server-cert.pem --tlskey=/etc/docker/server-key.pem -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock
重新加载Docker配置和重启Docker服务:
systemctl daemon-reload && systemctl restart docker
使用netstat -tunlp查看2376端口是否配置成功
同时使用客户端电脑,telnet登录下,是否成功
Harbor配置https
生成证书:
创建目录:
mkdri /data/cert
openssl req -newkey rsa:4096 \
-nodes -sha256 -keyout ca.key -x509 -days 365 \
-out ca.crt -subj "/C=CN/L=guangzhou/O=chenfeng/CN=sefon"
(CN=sefon,此外网上好多是使用域名,我多测试后,不能使用域名)
openssl req -newkey rsa:4096 \
-nodes -sha256 -keyout sefon.cferp.cn.key \
-out sefon.cferp.cn.csr -subj "/C=CN/L=guangzhou/O=chenfeng/CN=sefon.cferp.cn"
openssl x509 -req -days 365 \
-in sefon.cferp.cn.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out sefon.cferp.cn.crt
停止Harbor服务
docker-compose down -v
修改Harbor配置
vim harbor.cfg
hostname = sefon.cferp.cn
ui_url_protocol = https
ssl_cert = /data/cert/sefon.cferp.cn.crt
ssl_cert_key = /data/cert/sefon.cferp.cn.key
./prepare
服务器上直接访问:
[root@iZ2ze8mgdif90ed6xrhdx4Z /]# docker login sefon.cferp.cn
Username: admin
Password:
Error response from daemon: Get https://sefon.cferp.cn/v2/: x509: certificate signed by unknown authority
解决:
mkdir/certs.d
-bash: mkdir/certs.d: No such file or directory
[root@iZ2ze8mgdif90ed6xrhdx4Z docker]# mkdir certs.d
[root@iZ2ze8mgdif90ed6xrhdx4Z docker]# cd certs.d
[root@iZ2ze8mgdif90ed6xrhdx4Z certs.d]# mkdri sefon.cferp.cn
-bash: mkdri: command not found
[root@iZ2ze8mgdif90ed6xrhdx4Z certs.d]# mkdir sefon.cferp.cn
[root@iZ2ze8mgdif90ed6xrhdx4Z certs.d]# cd /data/cert
[root@iZ2ze8mgdif90ed6xrhdx4Z cert]# cp sefon.cferp.cn.crt /etc/docker/certs.d/sefon.cferp.cn
[root@iZ2ze8mgdif90ed6xrhdx4Z cert]# docker login sefon.cferp.cn
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