文件保存成.sh 可以在ubuntu中通过bash xxx.sh执行。
ip=xx.xx.xx.xx
password=Aa654321 # 3个字节以上,不然生成文件数不对
dir=/etc/docker/tls # 证书生成位置
validity_period=3650 # 证书有效期10年
COUNTRY="CN"
STATE=""
CITY=""
ORGANIZATION=""
# 将此shell脚本在安装docker的机器上执行,作用是生成docker远程连接加密证书
if [ ! -d "$dir" ]; then
echo ""
echo "$dir , not dir , will create"
echo ""
mkdir -p $dir
else
echo ""
echo "$dir , dir exist , will delete and create"
echo ""
rm -rf $dir
mkdir -p $dir
fi
cd $dir || exit
# 创建根证书RSA私钥
openssl genrsa -aes256 -passout pass:"$password" -out ca-key.pem 4096
# 创建CA证书
openssl req -new -x509 -days $validity_period -key ca-key.pem -passin pass:"$password" -sha256 -out ca.pem -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/CN=$ip"
# 创建服务端私钥
openssl genrsa -out server-key.pem 4096
# 创建服务端签名请求证书文件
openssl req -subj "/CN=$ip" -sha256 -new -key server-key.pem -out server.csr
echo subjectAltName = IP:$ip,IP:0.0.0.0 >>extfile.cnf
echo extendedKeyUsage = serverAuth >>extfile.cnf
# 创建签名生效的服务端证书文件
openssl x509 -req -days $validity_period -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$password" -CAcreateserial -out server-cert.pem -extfile extfile.cnf
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# 创建客户端私钥
openssl genrsa -out key.pem 4096
# 创建客户端签名请求证书文件
openssl req -subj '/CN=client' -sha256 -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth >extfile-client.cnf
# 创建签名生效的客户端证书文件
openssl x509 -req -days $validity_period -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$password" -CAcreateserial -out cert.pem -extfile extfile-client.cnf
# 删除多余文件
rm -f -v client.csr server.csr extfile.cnf extfile-client.cnf
chmod -v 0400 ca-key.pem key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem cert.pem
########## 配置 Docker 守护进程 ##########
echo "Configuring Docker daemon..."
# 备份 Docker 配置文件
DOCKER_SERVICE_FILE="/usr/lib/systemd/system/docker.service"
DOCKER_SERVICE_BACKUP="/usr/lib/systemd/system/docker.service.bak"
cp "$DOCKER_SERVICE_FILE" "$DOCKER_SERVICE_BACKUP"
# 更新 Docker 配置文件
DOCKER_TLS_OPTS="-H fd:// -H tcp://0.0.0.0:2375 --tlsverify --tlscacert=$dir/ca.pem --tlscert=$dir/server-cert.pem --tlskey=$dir/server-key.pem --containerd=/run/containerd/containerd.sock"
sed -i "s|^ExecStart=.*|ExecStart=/usr/bin/dockerd $DOCKER_TLS_OPTS|" "$DOCKER_SERVICE_FILE"
# 重新加载 systemd 配置
systemctl daemon-reload
########## 完成 ##########
echo "Docker TLS certificates and configuration have been generated successfully."
########## 重启Docker ##########
sudo systemctl restart docker
sudo ufw allow 2375