Docker---docker生產環境之TLS通信加密

Docker---docker生產環境之TLS通信加密

Docker---docker生產環境之TLS通信加密
一: 在docker中搭建TLS加密
- 在公司的docker業務中,通常爲了防止鏈路劫持、會話劫持等問題致使docker通訊時被中間人***,c/s兩端應該經過加密方式通信。
- 流程: 祕鑰key---> 身份前面csr---->(服務器/客戶端) (結合ca.pem) 製做證書pem                    
#實驗環境
| 角色                   | IP地址             | 軟件包    |
| ---------------------- | ------------------ | --------- |
| master(docker服務端) | 192.168.100.200/24 | docker-ce |
| client(docker客戶端) | 192.168.100.190/24 | docker-ce |

經過在服務端上建立tls密鑰證書,再下發給客戶端,客戶端經過私鑰訪問容器,這樣就保證的docker通信的安全性

CA證書只是一個官方認證的證書
接下來要建立server、client節點的證書
此時建立證書有三步
1.設置私鑰 確保安全加密
2.私鑰簽名 確保身份真實不可抵賴
3.製做證書
csr是一個簽名文件
#---------------------------------------master操做
1.環境部署
echo "127.0.0.1   master" >> /etc/hosts 
ping master "要能ping通"
2.建立ca祕鑰
mkdir /root/tls && cd /root/tls
openssl genrsa -aes256 -out ca-key.pem 4096     '//使用rsa非對稱祕鑰,位數256位,-out 輸出密鑰文件ca-key.pem'
ls
"包含ca-key.pem"
3.建立ca證書  '//輸入CA祕鑰密碼123123'
openssl req -new -x509 -days 1000 -key ca-key.pem -sha256 -subj "/CN=*" -out ca.pem
ls
"包含ca-key.pem  ca.pem"
4.建立服務器server私鑰
openssl genrsa -out server-key.pem 4096 '//建立服務器祕鑰'
ls
"ca-key.pem  ca.pem  server-key.pem"
5.建立服務器server簽名私鑰
openssl req -subj "/CN=*" -sha256 -new -key server-key.pem -out server.csr
ls
"ca-key.pem  ca.pem  server.csr  server-key.pem"
6.使用ca證書和簽名私鑰,建立server-cert.pem證書  "輸入123123密碼"
openssl x509 -req -days 1000 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem   
ls
"ca-key.pem  ca.pem  ca.srl  server-cert.pem  server.csr  server-key.pem"
7.生成客戶端client密鑰
openssl genrsa -out key.pem 4096
ls
"ca-key.pem  ca.pem  ca.srl  key.pem  server-cert.pem  server.csr  server-key.pem"
8.生成客戶端client簽名
openssl req -subj "/CN=client" -new -key key.pem -out client.csr
ls
"ca-key.pem  ca.pem  ca.srl  client.csr  key.pem  server-cert.pem  server.csr  server-key.pem"
9.建立配置文件
echo extendedKeyUsage=clientAuth > extfile.cnf
ls
"ca-key.pem  ca.srl      extfile.cnf  server-cert.pem  server-key.pem ca.pem      client.csr  key.pem      server.csr"
10.建立簽名證書,輸入123123密碼
openssl x509 -req -days 1000 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf '//基於ca證書,ca祕鑰生成簽名證書'
ls
"ca-key.pem  ca.srl    client.csr   key.pem          server.csr ca.pem      cert.pem  extfile.cnf  server-cert.pem  server-key.pem"
11.刪除多餘文件(也能夠不刪)
rm -rf ca.srl client.csr extfile.cnf server.csr /tmp
ls
"ca-key.pem  ca.pem  cert.pem  key.pem  server-cert.pem  server-key.pem"
12.修改docker的配置文件,而且重啓服務
vi /usr/lib/systemd/system/docker.service
    #ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock "註釋"
添加ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/root/tls/ca.pem --tlscert=/root/tls/server-cert.pem --tlskey=/root/tls/server-key.pem -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock
systemctl daemon-reload
systemctl restart docker
13.將(ca.pem)ca證書,(cert.pem)簽名證書,(key.pem)客戶端密鑰複製到client的/etc/docker目錄下
scp ca.pem root@192.168.100.190:/etc/docker/
scp cert.pem root@192.168.100.190:/etc/docker/
scp key.pem root@192.168.100.190:/etc/docker/

#--------------------------Client操做
1.在client上設置基本環境,且驗證TLS
echo "192.168.100.200   master" >> /etc/hosts
ping master
ls /etc/docker/
    "ca.pem  cert.pem  daemon.json  key.json  key.pem"
cd /etc/docker/     "在/etc/docker/執行如下命令"
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376 version

2.測試
----master
docker pull nginx
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376  images
"master能查看到本身本地的鏡像"
----client
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2376  images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              7e4d58f0e5f3        2 weeks ago         133MB
"client也能查看到"
//報錯內容
[root@master tls]# docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H tcp://master:2375 images
Cannot connect to the Docker daemon at tcp://master:2375. Is the docker daemon running?
[root@master tls]# docker --tlsverify --tlscacert=ca.pem --tlskey=server-key.pem --tlscert=server-cert.pem -H tcp://server:2376 images
error during connect: Get https://server:2376/v1.40/images/json: dial tcp: lookup server on 114.114.114.114:53: no such host

//報錯通常是端口錯誤或者證書錯誤
相關文章
相關標籤/搜索