這篇文章主要講解了“CoreOS怎么配置Docker API TLS認(rèn)證”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“CoreOS怎么配置Docker API TLS認(rèn)證”吧!
成都創(chuàng)新互聯(lián)主營(yíng)冠縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app開發(fā)定制,冠縣h5微信小程序定制開發(fā)搭建,冠縣網(wǎng)站營(yíng)銷推廣歡迎冠縣等地區(qū)企業(yè)咨詢
我們經(jīng)常會(huì)利用Portainer來(lái)管理docker環(huán)境,也經(jīng)常會(huì)用Jenkins來(lái)自動(dòng)構(gòu)建和部署docker,遠(yuǎn)程管理都會(huì)使用到Docker API,通常我們只是開啟了沒(méi)有安全保護(hù)的2375(通常)端口,這個(gè)比較危險(xiǎn),會(huì)導(dǎo)致遠(yuǎn)程劫持攻擊。那么我們就需要配置TLS認(rèn)證的2376(通常)端口。
下面我們針對(duì)CoreOS系統(tǒng)進(jìn)行配置:
我們利用腳本自動(dòng)生成,這樣非常便捷,腳本(auto-tls-certs.sh)如下:
#!/bin/bash # # ------------------------------------------------------------- # 自動(dòng)創(chuàng)建 Docker TLS 證書 # ------------------------------------------------------------- # 以下是配置信息 # --[BEGIN]------------------------------ CODE="dp" IP="docker> 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" # 拷貝服務(wù)端證書 mkdir -p /etc/docker/certs.d cp "ca-$CODE.pem" "server-cert-$CODE.pem" "server-key-$CODE.pem" /etc/docker/certs.d/
對(duì)腳本中的變量進(jìn)行修改后運(yùn)行,自動(dòng)會(huì)創(chuàng)建好tls證書,服務(wù)器的證書在/etc/docker/certs.d/目錄下:
客戶端的證書在運(yùn)行腳本的目錄下,同時(shí)還自動(dòng)打好了一個(gè).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
證書對(duì)應(yīng)選擇:
ca.pem
cert.pem
key.pem
這樣就完成了。注意如果之前開啟了未認(rèn)證的2375端口,請(qǐng)關(guān)閉并禁用,重啟docker服務(wù)。
# 停止不安全的2375端口 systemctl stop docker-tcp.socket # 禁用該端口 systemctl disable docker-tcp.socket # 重啟docker服務(wù) systemctl restart docker.service
感謝各位的閱讀,以上就是“CoreOS怎么配置Docker API TLS認(rèn)證”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)CoreOS怎么配置Docker API TLS認(rèn)證這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!