这篇文章主要讲解了“CoreOS怎么配置Docker API TLS认证”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CoreOS怎么配置Docker API TLS认证”吧!
我们经常会利用Portainer来管理docker环境,也经常会用Jenkins来自动构建和部署docker,远程管理都会使用到Docker API,通常我们只是开启了没有安全保护的2375(通常)端口,这个比较危险,会导致远程劫持攻击。那么我们就需要配置TLS认证的2376(通常)端口。
下面我们针对CoreOS系统进行配置:
我们利用脚本自动生成,这样非常便捷,脚本(auto-tls-certs.sh)如下:
#!/bin/bash # # ------------------------------------------------------------- # 自动创建 Docker TLS 证书 # ------------------------------------------------------------- # 以下是配置信息 # --[BEGIN]------------------------------ CODE="dp" IP="docker服务器ip" PASSWORD="证书密码" COUNTRY="CN" STATE="BEIJING" CITY="BEIJING" ORGANIZATION="公司" ORGANIZATIONAL_UNIT="Dev" COMMON_NAME="$IP" EMAIL="邮箱" # --[END]-- # Generate CA key openssl genrsa -aes256 -passout "pass:$PASSWORD" -out "ca-key-$CODE.pem" 4096 # Generate CA openssl req -new -x509 -days 365 -key "ca-key-$CODE.pem" -sha256 -out "ca-$CODE.pem" -passin "pass:$PASSWORD" -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$COMMON_NAME/emailAddress=$EMAIL" # Generate Server key openssl genrsa -out "server-key-$CODE.pem" 4096 # Generate Server Certs. openssl req -subj "/CN=$COMMON_NAME" -sha256 -new -key "server-key-$CODE.pem" -out server.csr echo "subjectAltName = IP:$IP,IP:127.0.0.1" >> extfile.cnf echo "extendedKeyUsage = serverAuth" >> extfile.cnf openssl x509 -req -days 365 -sha256 -in server.csr -passin "pass:$PASSWORD" -CA "ca-$CODE.pem" -CAkey "ca-key-$CODE.pem" -CAcreateserial -out "server-cert-$CODE.pem" -extfile extfile.cnf # Generate Client Certs. rm -f extfile.cnf openssl genrsa -out "key-$CODE.pem" 4096 openssl req -subj '/CN=client' -new -key "key-$CODE.pem" -out client.csr echo extendedKeyUsage = clientAuth >> extfile.cnf openssl x509 -req -days 365 -sha256 -in client.csr -passin "pass:$PASSWORD" -CA "ca-$CODE.pem" -CAkey "ca-key-$CODE.pem" -CAcreateserial -out "cert-$CODE.pem" -extfile extfile.cnf rm -vf client.csr server.csr chmod -v 0400 "ca-key-$CODE.pem" "key-$CODE.pem" "server-key-$CODE.pem" chmod -v 0444 "ca-$CODE.pem" "server-cert-$CODE.pem" "cert-$CODE.pem" # 打包客户端证书 mkdir -p "tls-client-certs-$CODE" cp -f "ca-$CODE.pem" "cert-$CODE.pem" "key-$CODE.pem" "tls-client-certs-$CODE/" cd "tls-client-certs-$CODE" tar zcf "tls-client-certs-$CODE.tar.gz" * mv "tls-client-certs-$CODE.tar.gz" ../ cd .. rm -rf "tls-client-certs-$CODE" # 拷贝服务端证书 mkdir -p /etc/docker/certs.d cp "ca-$CODE.pem" "server-cert-$CODE.pem" "server-key-$CODE.pem" /etc/docker/certs.d/
对脚本中的变量进行修改后运行,自动会创建好tls证书,服务器的证书在/etc/docker/certs.d/目录下:
客户端的证书在运行脚本的目录下,同时还自动打好了一个.tar.gz的包,很方便。
注意修改证书路径。
Create a file called /etc/systemd/system/docker-tls-tcp.socket
to make Docker available on a secured TCP socket on port 2376.
[Unit] Description=Docker Secured Socket for the API [Socket] ListenStream=2376 BindIPv6Only=both Service=docker.service [Install] WantedBy=sockets.target
Then enable this new socket:
systemctl enable docker-tls-tcp.socket systemctl stop docker systemctl start docker-tls-tcp.socket
Create /etc/systemd/system/docker.service.d/10-tls-verify.conf
drop-in for systemd Docker service:
[Service] Environment="DOCKER_OPTS=--tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server.pem --tlskey=/etc/docker/server-key.pem"
Reload systemd config files and restart docker service:
sudo systemctl daemon-reload sudo systemctl restart docker.service
证书对应选择:
ca.pem
cert.pem
key.pem
这样就完成了。注意如果之前开启了未认证的2375端口,请关闭并禁用,重启docker服务。
# 停止不安全的2375端口 systemctl stop docker-tcp.socket # 禁用该端口 systemctl disable docker-tcp.socket # 重启docker服务 systemctl restart docker.service
感谢各位的阅读,以上就是“CoreOS怎么配置Docker API TLS认证”的内容了,经过本文的学习后,相信大家对CoreOS怎么配置Docker API TLS认证这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。