1.生成TLS证书
# mkdir -p /opt/sh /opt/cert/docker
# touch /opt/sh/createcert.sh
# vim /opt/sh/createcert.sh
在createcret.sh添加内容
#!/bin/bash
set -e
if [ -z $1 ];then
echo "请输入Docker服务器主机名"
exit 0
fi
HOST=$1
mkdir -p /opt/cert/docker
cd /opt/cert/docker
openssl genrsa -aes256 -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr
# 配置白名单,推荐配置0.0.0.0,允许所有IP连接但只有证书才可以连接成功
echo subjectAltName = IP:$HOST,IP:0.0.0.0 > extfile.cnf
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
openssl genrsa -out key.pem 4096
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth > extfile.cnf
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
rm -v client.csr server.csr
chmod -v 0400 ca-key.pem key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem cert.pem
执行 createcert.sh 脚本,生成证书放置 /opt/cert/docker 目录中
# a.youlai.store是服务器的主机名
sh /opt/sh/createcert.sh 192.168.9.14
执行以下命令,输入密码,然后依次输入国家是 CN,省例如是Shanghai、市Shanghai、组织名称、组织单位、姓名或服务器名、邮件地址,都可以随意填写
参考 https://blog.csdn.net/oceanyang520/article/details/101563309
2.修改docker配置, 启用TLS
如果是离线手动安装的docker,通常情况下是这个文件:
vim /etc/systemd/system/docker.service
然后在 ExecStart=/usr/bin/dockerd 后面追加一个 \ ,然后黏贴证书的配置,大概如下:
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 \
3.重载服务并重启docker
systemctl daemon-reload && systemctl restart docker
查看2376端口是否启动
netstat -nltp | grep 2376
参考 https://blog.csdn.net/oceanyang520/article/details/101563309
网友评论