声明:
本博客欢迎转发,但请保留原作者信息!
博客地址:http://blog.csdn.net/jimmyxian
新浪微博:@线超博
内容系本人学习、研究和总结,如有雷同,实属荣幸!
================================
为了保证swarm集群的通信安全,可采用TLS协议进行加密传输,如下所示。
本例中,采用4个centos7主机,其中两个主机用于运行docker daemon,节点名称分别为node1、node2;一个主机用来运行swarm集群管理节点,名称为swarm;最后一个节点运行docker client,用来访问swarm
在swarm节点上,修改/etc/hosts,增加node1、node2对应的ip
在docker daemon节点上,修改/etc/hosts,增加swarm对应的ip
在docker client节点上(运行docker命令访问swarm的节点),修改/etc/hosts,增加swarm对应的ip
在配置过程中,需要生成3类证书,分别用于dockerclient、swarm以及docker deamon,具体生成方法如下:
openssl genrsa -out CAkey.pem 2048
openssl req -new -key CAkey.pem -x509 -days 3650 -out ca.pem
echo "extendedKeyUsage = clientAuth,serverAuth" > openssl.cnf
如果不配置,后续会出现Error,tls: client's certificate's extended key usage doesn't permit it to be used for client authentication
openssl genrsa -out swarmKEY.pem 2048
openssl req -subj "/CN=swarm" -new -key swarmKEY.pem -out swarm.csr
openssl x509 -req -days 3650 -in swarm.csr -CA ca.pem -CAkey CAkey.pem -CAcreateserial -out swarmCRT.pem -extfile openssl.cnf
openssl rsa -in swarmKEY.pem -out swarmKEY.pem
openssl genrsa -out node01KEY.pem 2048
openssl req -subj "/CN=nodex" -new -key node01KEY.pem -out node01.csr
openssl x509 -req -days 3650 -in node01.csr -CA ca.pem -CAkey CAkey.pem -CAcreateserial -out node01CRT.pem -extfile openssl.cnf
openssl rsa -in node01KEY.pem -out node01KEY.pem
备注:如果有多个docker deamon节点,需要改变node01为实际的节点名,多次生成。本次试验采用两个节点node1、node2
openssl genrsa -out localKEY.pem 2048
openssl req -subj "/CN=HOSTNAME" -new -key localKEY.pem -out local.csr
备注:HOSTNAME为docker client节点的主机名
openssl x509 -req -days 3650 -in local.csr -CA ca.pem -CAkey CAkey.pem -CAcreateserial -out localCRT.pem -extfile openssl.cnf
openssl rsa -in localKEY.pem -out localKEY.pem
OPTIONS='--tlsverify --tlscacert=/home/key/ca.pem --tlscert=/home/key/node0xCRT.pem --tlskey=/home/key/node0xKEY.pem -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock'
systemctl restart docker
swarm manage --tlsverify --tlscacert=/home/key/ca.pem --tlscert=/home/key/swarmCRT.pem --tlskey=/home/key/swarmKEY.pem --host=0.0.0.0:2376 node1:2375 node2:2375
docker --tlsverify --tlscacert=/home/key/ca.pem --tlscert=/home/key/localCRT.pem --tlskey=/home/key/localKEY.pem -H swarm:2376 info
在docker client主机上运行以下测试:
docker -H swarm:2376 info
提示出错,需要TLS认证
FATA[0000] Get http://swarm:2376/v1.18/containers/json: malformed HTTP response "\x15\x03\x01\x00\x02\x02". Are you trying to connect to a TLS-enabled daemon without TLS?
docker --tlsverify --tlscacert=/home/key/ca.pem --tlscert=/home/key/localCRT.pem --tlskey=/home/key/localKEY.pem -H swarm:2376 info
可以看到两个节点的信息
docker --tlsverify --tlscacert=/home/key/ca.pem --tlscert=/home/key/localCRT.pem --tlskey=/home/key/localKEY.pem -H swarm:2376 run -d busybox sleep 1000
可以看到成功创建容器