版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:[https://blog.csdn.net/boling\_cavalry/article/details/100601169](https://blog.csdn.net/boling_cavalry/article/details/100601169)
《Docker远程连接设置》一文讲述了开启Docker远程连接的方法,但那种方法不安全,因为任何客户端都可以通过Docker服务的IP地址连接上去,今天我们就来学习Docker官方推荐的安全的远程连接方式:TLS加密连接,通过证书来保证安全性。
这里是官方的权威文档:https://docs.docker.com/engine/security/https/
本次实战的环境信息如下:
本次实战的操作步骤如下:
openssl genrsa -aes256 -out ca-key.pem 4096
openssl req -new -x509 -days 1000 -key ca-key.pem -sha256 -subj "/CN=*" -out ca.pem
此时生成的ca.pem文件就是CA证书;
openssl genrsa -out server-key.pem 4096
此时生成的server-key.pem文件就是服务端私钥;
openssl req -subj "/CN=*" -sha256 -new -key server-key.pem -out server.csr
此时生成的server.csr文件就是服务端证书;
openssl x509 -req -days 1000 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem
此时生成的server-cert.pem文件就是已盖章生效的服务端证书;
openssl genrsa -out key.pem 4096
此时生成的key.pem文件就是客户私钥;
openssl req -subj "/CN=client" -new -key key.pem -out client.csr
此时生成的client.csr文件就是客户端证书签名请求;
echo extendedKeyUsage=clientAuth > extfile.cnf
openssl x509 -req -days 1000 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
rm -rf ca.srl client.csr extfile.cnf server.csr
文件名 | 作用 |
---|---|
ca.pem | CA机构证书 |
ca-key.pem | 根证书RSA私钥 |
cert.pem | 客户端证书 |
key.pem | 客户私钥 |
server-cert.pem | 服务端证书 |
server-key.pem | 服务端私钥 |
至此,所有证书文件制作完成,接下来对Docker做TLS安全配置;
ExecStart=/usr/bin/dockerd-current --tlsverify --tlscacert=/root/work/ca.pem --tlscert=/root/work/server-cert.pem --tlskey=/root/work/server-key.pem -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock \
systemctl daemon-reload && systemctl restart docker
配置完成,接下来验证远程TLS连接。
scp /root/work/ca.pem root@192.168.121.132:/root/work \
&& scp /root/work/cert.pem root@192.168.121.132:/root/work \
&& scp /root/work/key.pem root@192.168.121.132:/root/work
192.168.121.138 docker-daemon
docker --tlsverify --tlscacert=/root/work/ca.pem --tlscert=/root/work/cert.pem --tlskey=/root/work/key.pem -H tcp://docker-daemon:2376 version
控制台显示以下信息,其中Server部分就是A机器的Docker信息:
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: b2f74b2/1.13.1
Built: Wed May 1 14:55:20 2019
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Mon Aug 5 15:09:42 2019
OS/Arch: linux/amd64
Experimental: false
[root@centos7 work]# docker -H tcp://192.168.121.138:2375 images
Cannot connect to the Docker daemon at tcp://192.168.121.138:2375. Is the docker daemon running?
[root@centos7 work]# docker -H tcp://docker-daemon:2375 images
Cannot connect to the Docker daemon at tcp://docker-daemon:2375. Is the docker daemon running?
[root@centos7 work]# docker -H tcp://192.168.121.138:2376 images
Get http://192.168.121.138:2376/v1.26/images/json: net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x03\x01\x00\x02\x02".
* Are you trying to connect to a TLS-enabled daemon without TLS?
[root@centos7 work]# docker -H tcp://docker-daemon:2376 images
Get http://docker-daemon:2376/v1.26/images/json: net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x03\x01\x00\x02\x02".
* Are you trying to connect to a TLS-enabled daemon without TLS?
至此,TLS加密远程连接Docker的实战就完成了,希望您在设置安全的Docker远程连接是,本文能给您提供参考。