大佬整理的Shell我的感受用着有問題,改進了一下。
客戶端用的key保存到~/.docker/tls-client-certs.tar.gz
但願你們用的開心git
#!/bin/bash # # Created by L.STONE <web.developer.network@gmail.com> # Mod By Ryan.L <github-benzBrake@woai.ru> # ------------------------------------------------------------- # 自動建立 Docker TLS 證書 # ------------------------------------------------------------- # 如下是配置信息 # Config start IP="8.8.8.8" PASSWORD="123456" COUNTRY="CN" STATE="Beijing" CITY="" ORGANIZATION="iPlayLoli" ORGANIZATIONAL_UNIT="Dev" COMMON_NAME="$IP" EMAIL="github-benzBrake@woai.ru" # Config end # 工做目錄 mkdir -p /etc/docker ~/.docker cd ~/.docker # 中止 docker service docker stop # 生成 CA 密鑰 if [[ ! -f ca-key.pem ]]; then echo " - 生成 CA 密鑰" openssl genrsa -aes256 -passout "pass:$PASSWORD" -out "ca-key.pem" 4096 fi # 生成 CA if [[ ! -f ca.pem ]]; then echo " - 生成 CA" openssl req -new -x509 -days 365 -key "ca-key.pem" -sha256 -out "ca.pem" -passin "pass:$PASSWORD" -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$COMMON_NAME/emailAddress=$EMAIL" fi # 生成服務器密鑰 & 服務器證書 if [[ ! -f server-key.pem ]]; then echo " - 生成服務器密鑰" openssl genrsa -out "server-key.pem" 4096 fi if [[ ! -f server.csr ]]; then openssl req -subj "/CN=$COMMON_NAME" -sha256 -new -key "server-key.pem" -out server.csr fi if [[ ! -f server-cert.pem ]]; then echo " - 生成服務器證書" 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.pem" -CAkey "ca-key.pem" -CAcreateserial -out "server-cert.pem" -extfile extfile.cnf fi rm -f extfile.cnf # 生成客戶端證書 if [[ ! -f key.pem ]]; then openssl genrsa -out "key.pem" 4096 fi if [[ ! -f cert.pem ]]; then openssl req -subj '/CN=client' -new -key "key.pem" -out client.csr echo extendedKeyUsage = clientAuth >> extfile.cnf openssl x509 -req -days 365 -sha256 -in client.csr -passin "pass:$PASSWORD" -CA "ca.pem" -CAkey "ca-key.pem" -CAcreateserial -out "cert.pem" -extfile extfile.cnf fi chmod -v 0400 "ca-key.pem" "key.pem" "server-key.pem" chmod -v 0444 "ca.pem" "server-cert.pem" "cert.pem" # 打包客戶端證書 echo " - 打包客戶端證書爲 tls-client-certs.tar.gz" mkdir -p "tls-client-certs" cp -f "ca.pem" "cert.pem" "key.pem" "tls-client-certs/" cd "tls-client-certs" tar zcf "tls-client-certs.tar.gz" * mv "tls-client-certs.tar.gz" ../ cd .. rm -rf "tls-client-certs" # 拷貝服務端證書 mkdir -p /etc/docker/certs.d cp -f "ca.pem" "server-cert.pem" "server-key.pem" /etc/docker/certs.d/ echo " - 修改 /etc/docker/daemon.json 文件" if [[ -f /etc/docker/daemon.json ]]; then grep "/etc/docker/certs.d/server-key.pem" /etc/docker/daemon.json > /dev/null if [[ ! $? -eq 0 ]]; then cat >/etc/docker/daemon.json<<EOF { "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"] } EOF fi else cat >/etc/docker/daemon.json<<EOF { "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"] } EOF fi # 覆蓋啓動參數,解決 docker 啓動失敗 if [[ ! -z $(command -v systemctl) ]];then mkdir -p /etc/systemd/system/docker.service.d if [[ ! -f /etc/systemd/system/docker.service.d/override.conf ]]; then cat >/etc/systemd/system/docker.service.d/override.conf<<EOF [Service] ExecStart= ExecStart=/usr/bin/dockerd EOF systemctl daemon-reload fi fi # 清理 rm -vf client.csr server.csr extfile.cnf ca.srl server-cert.pem server-key.pem cert.pem # 啓動 docker service docker start # 客戶端遠程鏈接 echo "Connect to server via docker-cli:" echo "docker -H $IP:2376 --tlsverify --tlscacert ~/.docker/ca.pem --tlscert ~/.docker/cert.pem --tlskey ~/.docker/key.pem ps -a" # 客戶端使用 cURL 鏈接 echo "Connect to server via curl:" echo "curl --cacert ~/.docker/ca.pem --cert ~/.docker/cert.pem --key ~/.docker/key.pem https://$IP:2376/containers/json" echo -e "\e[1;32mAll be done.\e[0m"