docker---TLS加密通讯

一、TLS加密通讯
■为了防止链路劫持、会话劫持等问题导致 Docker 通信时被中 间人攻击,c/s 两端应该通过加密方式通讯。

在master服务器上
创建目录
mkdir /tls
cd /tls

修改主机名
hostnamectl set-hostname master
su

添加映射
echo “127.0.0.1 master” >>/etc/hosts

创建ca密钥
openssl genrsa -aes256 -out ca-key.pem 4096
//输入123123
ca-key.pem

创建ca证书
openssl req -new -x509 -days 1000 -key ca-key.pem -sha256 -subj “/CN=*” -out ca.pem //输入123123
ca-key.pem ca.pem

创建服务器私钥
openssl genrsa -out server-key.pem 4096
ca-key.pem ca.pem server-key.pem

签名私钥
openssl req -subj “/CN=*” -sha256 -new -key server-key.pem -out server.csr
ca-key.pem ca.pem server.csr server-key.pem

使用ca证书与私钥证书签名
openssl x509 -req -days 1000 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem
ca-key.pem ca.pem ca.srl server-cert.pem server.csr server-key.pem

生成客户端密钥
openssl genrsa -out key.pem 4096
ca-key.pem ca.pem ca.srl key.pem server-cert.pem server.csr server-key.pem

签名客户端
openssl req -subj “/CN=client” -new -key key.pem -out client.csr
ca-key.pem ca.srl key.pem server.csr
ca.pem client.csr server-cert.pem server-key.pem

创建配置文件
echo extendedKeyUsage=clientAuth > extfile.cnf

签名证书(签名客户端,ca证书,ca密钥)
openssl x509 -req -days 1000 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
ca-key.pem ca.srl client.csr key.pem server.csr
ca.pem cert.pem extfile.cnf server-cert.pem server-key.pem

删除多余文件
rm -rf ca.srl client.csr extfile.cnf server.csr
ca-key.pem ca.pem cert.pem key.pem server-cert.pem server-key.pem

配置docker
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 -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock

重启进程和docker服务
systemctl daemon-reload
systemctl restart docker
将 /tls/ca.pem /tls/cert.pem /tls/key.pem 三个文件复制到另一台主机
scp ca.pem root@20.0.0.25:/etc/docker/
scp cert.pem root@20.0.0.25:/etc/docker/
scp key.pem root@20.0.0.25:/etc/docker/

本地验证
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376 version
##########################################
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
#####################################################

在client上
添加映射
echo “20.0.0.24 master” >>/etc/hosts

客户端登录操作
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376 version
##########################################
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
#####################################################

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值