docker安全配置tls证书加密

[root@localhost ~]# mkdir /tls
[root@localhost ~]# cd /tls
[root@localhost tls]# hostnamectl set-hostname master
[root@localhost tls]# su
[root@master tls]# vim /etc/hosts

// 添加master与IP地址映射 //

// 创建ca密钥 //
[root@master tls]# openssl genrsa -aes256 -out ca-key.pem 4096
Generating RSA private key, 4096 bit long modulus
…++
…++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem:
Verifying - Enter pass phrase for ca-key.pem:

// 创建ca证书 //
[root@master tls]# openssl req -new -x509 -days 1000 -key ca-key.pem -sha256 -subj “/CN=*” -out ca.pem
Enter pass phrase for ca-key.pem:

// 创建服务器私钥 //
[root@master tls]# openssl genrsa -out server-key.pem 4096
Generating RSA private key, 4096 bit long modulus
…++
…++
e is 65537 (0x10001)

// 签名私钥 //
[root@master tls]# openssl req -subj “/CN=*” -sha256 -new -key server-key.pem -out server.csr

// 使用ca证书与私钥证书签名 //
[root@master tls]# openssl x509 -req -days 1000 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem
Signature ok
subject=/CN=*
Getting CA Private Key
Enter pass phrase for ca-key.pem:

// 生成客户端密钥 //
[root@master tls]# openssl genrsa -out key.pem 4096
Generating RSA private key, 4096 bit long modulus
…++
…++
e is 65537 (0x10001)

// 签名客户端 //
[root@master tls]# openssl req -subj “/CN=client” -new -key key.pem -out client.csr

// 创建配置文件 //
[root@master tls]# echo extendedKeyUsage=clientAuth > extfile.cnf

// 签名证书(需要签名客户端,ca证书,ca密钥) //
[root@master tls]# openssl x509 -req -days 1000 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=client
Getting CA Private Key
Enter pass phrase for ca-key.pem:

// 删除多余文件 //
[root@master tls]# rm -rf ca.srl client.csr extfile.cnf server.csr

// 配置docker //
[root@master tls]# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd添加参数:
–tlsverify --tlscacert=/tls/ca.pem --tlscert=/tls/server-cert.pem --tlskey=/tls/server-key.pem

// 重载后重启docker //
[root@master tls]# systemctl daemon-reload
[root@master tls]# systemctl restart docker

// 将三个文件复制到客户端 //
[root@master tls]# scp ca.pem cert.pem key.pem [email protected]:/etc/docker/
[email protected]’s password:
ca.pem 100% 1765 1.8MB/s 00:00
cert.pem 100% 1696 1.5MB/s 00:00
key.pem 100% 3247 2.8MB/s 00:00

// 本地测试 //
[root@master tls]# docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376 version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17

// 客户端测试 //
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376 version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17

你可能感兴趣的:(docker)