本文直接开始介绍使用 CA 认证远程连接 docker,不使用 CA 认证也行,在自己的虚拟机里面可以这么干,但是放到联网的服务器上就不建议这么做了,原因是,你把2375端口暴露在外面,意味着别人随时都可以提取到你服务器的root权限,是很容易被黑客黑的(笔者就是被当成了矿机的一例QwQ)。因此,docker官方推荐使用加密的tcp连接,以Https的方式与客户端建立连接
在 idea 中一键部署的操作我就不再赘述了,如果有不会的小伙伴可以到[这里](%5Bhttps://juneblog.online/articles/2020/01/13/1578924660386.html%5D(https://juneblog.online/articles/2020/01/13/1578924660386.html)来看,前面的步骤都是一样的,区别在后面我会标记出来以便查看。
mkdir -p /usr/local/ca
cd /usr/local/ca/
openssl genrsa -aes256 -out ca-key.pem 4096
输出
...................++
..........................................................................................................................................................................................................................................................................................................................................................++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem: # 此处输入密码
Verifying - Enter pass phrase for ca-key.pem: # 此处输入密码
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
输出
Enter pass phrase for ca-key.pem:
Enter pass phrase for ca-key.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:zj
Locality Name (eg, city) [Default City]:hz
Organization Name (eg, company) [Default Company Ltd]:qdsg
Organizational Unit Name (eg, section) []:qdsg
Common Name (eg, your name or your server's hostname) []:qdsg
Email Address []:1@qq.com
openssl genrsa -out server-key.pem 4096
输出
Generating RSA private key, 4096 bit long modulus
...........................++
................++
e is 65537 (0x10001)
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr
比如
openssl req -subj "/CN=192.168.1.106" -sha256 -new -key server-key.pem -out server.csr
或
openssl req -subj "/CN=www.baidu.com" -sha256 -new -key server-key.pem -out server.csr
我这里使用阿里云服务器进行测试
openssl req -subj "/CN=121.40.176.56" -sha256 -new -key server-key.pem -out server.csr
也就是你接下来要允许那些ip可以连接到服务器的docker,因为已经是ssl连接,所以我推荐配置0.0.0.0,也就是所有ip都可以连接(但只有拥有证书的才可以连接成功),这样配置好之后公司其他人也可以使用。如果你不想这样,那你可以配置ip,用逗号分隔开。下面的$Host依旧是你服务器外网的IP或者域名,请自行替换。
注意!!!!这里我踩了坑
如果你填写的是ip地址的话命令如下echo subjectAltName = IP: H O S T , I P : 0.0.0.0 > > e x t f i l e . c n f 如 果 你 填 写 的 是 域 名 的 话 命 令 如 下 e c h o s u b j e c t A l t N a m e = D N S : HOST,IP:0.0.0.0 >> extfile.cnf 如果你填写的是域名的话命令如下 echo subjectAltName = DNS: HOST,IP:0.0.0.0>>extfile.cnf如果你填写的是域名的话命令如下echosubjectAltName=DNS:HOST,IP:0.0.0.0 >> extfile.cnf
这里我使用ip
echo subjectAltName = IP:121.40.176.56,IP:0.0.0.0 >> extfile.cnf
echo extendedKeyUsage = serverAuth >> 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
输出
Signature ok
subject=/CN=192.168.1.106
Getting CA Private Key
Enter pass phrase for ca-key.pem: # 此处输入密码
openssl genrsa -out key.pem 4096
输出
Generating RSA private key, 4096 bit long modulus
...........................................................................................................................................................................................................................................................................................................................................................................++
......................................................................................................................................++
e is 65537 (0x10001)
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
输出
Signature ok
subject=/CN=client
Getting CA Private Key
Enter pass phrase for ca-key.pem: # 此处输入密码
rm -v client.csr server.csr
输出
rm:是否删除普通文件 "client.csr"?y
已删除"client.csr"
chmod -v 0400 ca-key.pem key.pem server-key.pem
输出
mode of "ca-key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
mode of "key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
mode of "server-key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
chmod -v 0444 ca.pem server-cert.pem cert.pem
输出
mode of "ca.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
mode of "server-cert.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
mode of "cert.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
cp server-*.pem /etc/docker/
cp ca.pem /etc/docker/
# 以上两步操作都在ca文件夹内,如果当前不在ca文件夹,需加上路径
vim /lib/systemd/system/docker.service
添加如下代码到配置文件中
--tlsverify --tlscacert=/etc/docker/ca.pem \
--tlscert=/etc/docker/server-cert.pem \
--tlskey=/etc/docker/server-key.pem \
-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \
systemctl daemon-reload
systemctl restart docker
/sbin/iptables -I INPUT -p tcp --dport 2376 -j ACCEPT
iptables-save
输出 (这里每个人输出的可能都不一样,但不影响)
# Generated by iptables-save v1.4.21 on Mon Feb 3 17:03:02 2020
*nat
:PREROUTING ACCEPT [310:18366]
:INPUT ACCEPT [172:10086]
:OUTPUT ACCEPT [3256:251253]
:POSTROUTING ACCEPT [3299:253557]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.17.0.2/32 -d 172.17.0.2/32 -p tcp -m tcp --dport 3306 -j MASQUERADE
-A POSTROUTING -s 172.17.0.3/32 -d 172.17.0.3/32 -p tcp -m tcp --dport 443 -j MASQUERADE
-A POSTROUTING -s 172.17.0.3/32 -d 172.17.0.3/32 -p tcp -m tcp --dport 80 -j MASQUERADE
-A POSTROUTING -s 172.17.0.4/32 -d 172.17.0.4/32 -p tcp -m tcp --dport 12345 -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 3306 -j DNAT --to-destination 172.17.0.2:3306
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 443 -j DNAT --to-destination 172.17.0.3:443
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 172.17.0.3:80
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 12345 -j DNAT --to-destination 172.17.0.4:12345
COMMIT
# Completed on Mon Feb 3 17:03:02 2020
# Generated by iptables-save v1.4.21 on Mon Feb 3 17:03:02 2020
*filter
:INPUT ACCEPT [13385:2320047]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [34319:6861761]
:DOCKER - [0:0]
:DOCKER-ISOLATION - [0:0]
-A INPUT -p tcp -m tcp --dport 2375 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2376 -j ACCEPT
-A FORWARD -j DOCKER-ISOLATION
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 3306 -j ACCEPT
-A DOCKER -d 172.17.0.3/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 443 -j ACCEPT
-A DOCKER -d 172.17.0.3/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER -d 172.17.0.4/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 12345 -j ACCEPT
-A DOCKER-ISOLATION -j RETURN
COMMIT
# Completed on Mon Feb 3 17:03:02 2020
service docker restart
这里我使用的是 lrzsz(Linux服务器和window互传文件工具)
传的,使用方法可以参考这里
sz ca.pem cert.pem key.pem
注意!如果没有将 tcp 替换成 https 将会出现 IOE 异常,届时请检查以下配置是否正确!
有什么问题大家可以在下面评论区进行交流