欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Docker启用TLS实现安全配置的步骤

程序员文章站 2022-03-18 13:28:19
前言 之前开启了docker的2375 remote api,接到公司安全部门的要求,需要启用授权,翻了下官方文档 protect the docker daemon...

前言

之前开启了docker的2375 remote api,接到公司安全部门的要求,需要启用授权,翻了下官方文档

protect the docker daemon socket

启用tls

在docker服务器,生成ca私有和公共密钥

$ 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:

$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.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) [au]:
state or province name (full name) [some-state]:queensland
locality name (eg, city) []:brisbane
organization name (eg, company) [internet widgits pty ltd]:docker inc
organizational unit name (eg, section) []:sales
common name (e.g. server fqdn or your name) []:$host
email address []:sven@home.org.au

有了ca后,可以创建一个服务器密钥和证书签名请求(csr)

$host 是你的服务器ip

$ 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

接着,用ca来签署公共密钥:

$ echo subjectaltname = dns:$host,ip:$host:127.0.0.1 >> extfile.cnf

 $ echo extendedkeyusage = serverauth >> extfile.cnf

生成key:

$ 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=your.host.com
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

修改extfile.cnf:

echo extendedkeyusage = clientauth > extfile-client.cnf

生成签名私钥:

$ openssl x509 -req -days 365 -sha256 -in client.csr -ca ca.pem -cakey ca-key.pem \
 -cacreateserial -out cert.pem -extfile extfile-client.cnf
signature ok
subject=/cn=client
getting ca private key
enter pass phrase for ca-key.pem:

将docker服务停止,然后修改docker服务文件

[unit]
description=docker application container engine
documentation=http://docs.docker.io

[service]
environment="path=/opt/kube/bin:/bin:/sbin:/usr/bin:/usr/sbin"
execstart=/opt/kube/bin/dockerd --tlsverify --tlscacert=/root/docker/ca.pem --tlscert=/root/docker/server-cert.pem --tlskey=/root/docker/server-key.pem -h unix:///var/run/docker.sock -h tcp://0.0.0.0:2375
execstartpost=/sbin/iptables -i forward -s 0.0.0.0/0 -j accept
execreload=/bin/kill -s hup $mainpid
restart=on-failure
restartsec=5
limitnofile=infinity
limitnproc=infinity
limitcore=infinity
delegate=yes
killmode=process

[install]
wantedby=multi-user.target

然后重启服务

systemctl daemon-reload
systemctl restart docker.service 

重启后查看服务状态:

systemctl status docker.service
● docker.service - docker application container engine
  loaded: loaded (/etc/systemd/system/docker.service; enabled; vendor preset: enabled)
  active: active (running) since thu 2019-08-08 19:22:26 cst; 1 min ago

已经生效。

使用证书连接:

复制ca.pem,cert.pem,key.pem三个文件到客户端

docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -h=$host:2375 version连接即可

docker-java 启用tls

项目里使用docker的java客户端docker-java调用docker,为了支持tls,在创建客户端时,需要增加tls设置。

首先将ca.pem cert.pem key.pem这三个文件拷贝到本地,例如e:\\docker\\",

然后defaultdockerclientconfig里withdockertlsverify设为true,并设置certpath为刚拷贝的目录。 

defaultdockerclientconfig.builder builder =
        defaultdockerclientconfig.createdefaultconfigbuilder()
          .withdockerhost("tcp://" + server + ":2375")
          .withapiversion("1.30");
      if (containerconfiguration.getdockertlsverify()) {
        builder = builder.withdockertlsverify(true)
          .withdockercertpath("e:\\docker\\");
      }
  return dockerclientbuilder.getinstance(builder.build()).build()

大工搞定。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。