共计 2534 个字符,预计需要花费 7 分钟才能阅读完成。
服务端生成证书
#!/bin/sh
# ip 改成服务器 IP
ip=192.168.252.128
password=mysecret
dir=/etc/docker/cert
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
# 创建根证书RSA私钥
openssl genrsa -aes256 -passout pass:$password -out ca-key.pem 4096
# 创建CA证书
openssl req -new -x509 -days 3650 -key ca-key.pem -passin pass:$password -sha256 -out ca.pem -subj "/C=NL/ST=./L=./O=./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 3650 -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' -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth >> extfile.cnf
echo extendedKeyUsage = clientAuth > extfile-client.cnf
# 创建签名生效的客户端证书文件
openssl x509 -req -days 3650 -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 service
root@k3s-128 systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
✘ ⚡ 二 20 2月 - 21:16 /etc/docker
root@k3s-128 cat /lib/systemd/system/docker.service;
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2376 --tlsverify --tlscacert=/etc/docker/cert/ca.pem --tlscert=/etc/docker/cert/server-cert.pem --tlskey=/etc/docker/cert/server-key.pem
新增
-H tcp://0.0.0.0:2376 --tlsverify --tlscacert=/etc/docker/cert/ca.pem --tlscert=/etc/docker/cert/server-cert.pem --tlskey=/etc/docker/cert/server-key.pem
验证
⚡ 二 20 2月 - 21:17 /etc/docker/cert
root@k3s-128 pwd
/etc/docker/cert
⚡ 二 20 2月 - 21:17 /etc/docker/cert
root@k3s-128 docker -H tcp://0.0.0.0:2376 --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
842e2f3978f7 deeplythink/ninja:latest "/bin/ninja run --ar…" 8 hours ago Up 3 minutes 0.0.0.0:8999->7999/tcp ninja-frontend
客户端
下载证书
cleverhu@cleverhu MINGW64 /g/codes/go/src/cocopilot-proxy
$ ls C:/Users/cleverhu/.docker/cert
ca.pem cert.pem key.pem
# 设置环境变量
export DOCKER_HOST=tcp://192.168.252.128:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=C:/Users/cleverhu/.docker/cert
正文完