1. 程式人生 > >docker筆記6-使用tsl方式連線docker和CA證書的安裝使用

docker筆記6-使用tsl方式連線docker和CA證書的安裝使用

記錄使用tsl連線docker, CA證書的安裝使用

生成安裝證書

參照原文連結:https://segmentfault.com/a/1190000012510820 

auto-tls-certs.sh Bash 指令碼程式碼
#!/bin/bash
# 
# Created by L.STONE <[email protected]>
# -------------------------------------------------------------
# 自動建立 Docker TLS 證書
# -------------------------------------------------------------

# 以下是配置資訊
# --[BEGIN]------------------------------

CODE="字尾標識碼"
IP="IP 地址"
PASSWORD="密碼"
COUNTRY="CN"
STATE="省"
CITY="市"
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/

# /etc/docker/daemon.json
# {
#   "tlsverify": true,
#   "tlscacert": "/etc/docker/certs.d/ca.pem",
#   "tlscert": "/etc/docker/certs.d/server-cert.pem",
#   "tlskey": "/etc/docker/certs.d/server-key.pem",
#   "hosts": ["tcp://0.0.0.0:2376", "unix:///var/run/docker.sock"]
# }

echo " - 修改 /etc/docker/daemon.json 檔案"
cat <<EOF
vi /etc/docker/daemon.json
{
  "tlsverify": true,
  "tlscacert": "/etc/docker/certs.d/ca-$CODE.pem",
  "tlscert": "/etc/docker/certs.d/server-cert-$CODE.pem",
  "tlskey": "/etc/docker/certs.d/server-key-$CODE.pem",
  "hosts": ["tcp://0.0.0.0:2376", "unix:///var/run/docker.sock"]
}
EOF

# 拷貝客戶端證書檔案
# cp -v {ca,cert,key}.pem ~/.docker

# 客戶端遠端連線
# docker -H 192.168.1.130:2376 --tlsverify --tlscacert ~/.docker/ca.pem --tlscert ~/.docker/cert.pem --tlskey ~/.docker/key.pem ps -a
echo "docker -H $IP:2376 --tlsverify --tlscacert ~/.docker/ca-$CODE.pem --tlscert ~/.docker/cert-$CODE.pem --tlskey ~/.docker/key-$CODE.pem ps -a"

# 客戶端使用 cURL 連線
# curl --cacert ~/.docker/ca.pem --cert ~/.docker/cert.pem --key ~/.docker/key.pem https://192.168.1.130:2376/containers/json
echo "curl --cacert ~/.docker/ca-$CODE.pem --cert ~/.docker/cert-$CODE.pem --key ~/.docker/key-$CODE.pem https://$IP:2376/containers/json"

echo -e "\e[1;32mAll be done.\e[0m"

執行命令

./auto-tls-certs.sh 

修改docker.service

vim /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2376

執行

systemctl daemon-reload
systemctl restart docker.service

將3個pem證書放置到客戶端(eg:C:\Users\docker-ca)

下載ca.pem ,cert.pem ,key.pem複製到客戶端C:\Users\docker-ca(路徑自定義)

複製客戶端的證書位置(eg:C:\Users\docker-ca)

如果idea 安裝了docker外掛配置如圖 

如果使用的docker-maven-plugin配置證書位置C:\Users\docker-ca

<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>1.2.0</version>
        <configuration>
            <serverId>docker-hub</serverId>
            ...略
            <dockerCertPath>C:\Users\docker-ca</dockerCertPath>
            ...略
        </configuration>
</plugin>

作        者:天空藍藍的 
網址導航:http://www.lskyf.com 
個人部落格:http://www.lskyf.xyz 
版權所有,歡迎保留原文連結進行轉載:)