1. 程式人生 > >k8s的敏感資訊管理

k8s的敏感資訊管理

 

應用啟動過程中可能需要一些敏感資訊,比如訪問資料庫的使用者名稱密碼或者祕鑰。將這些資訊直接儲存在容器映象中顯然不妥,Kubernetes 提供的解決方案是 Secret。

Secret 會以密文的方式儲存資料,避免了直接在配置檔案中儲存敏感資訊。Secret 會以 Volume 的形式被 mount 到 Pod,容器可通過檔案的方式使用 Secret 中的敏感資料;此外,容器也可以環境變數的方式使用這些資料。

Secret 可通過命令列或 YAML 建立。比如希望 Secret 中包含如下資訊:

  1. 使用者名稱 admin

  2. 密碼 123456

1.建立 Secret方式

有四種方法建立 Secret:

1.1 通過 --from-literal

kubectl create secret generic mysecret --from-literal=username=admin --from-literal=password=123456

每個 --from-literal 對應一個資訊條目。

1.2 通過 --from-file

echo -n admin > ./username
echo -n 123456 > ./password
kubectl create secret generic mysecret 
--from-file=./username --from-file=./password

每個檔案內容對應一個資訊條目。

1.3通過 --from-env-file

cat << EOF > env.txt
username=admin
password=123456
EOF
kubectl create secret generic mysecret --from-env-file=env.txt

檔案 env.txt 中每行 Key=Value 對應一個資訊條目。

1.4通過 YAML 配置檔案:

檔案中的敏感資料必須是通過 base64 編碼後的結果。

 

執行 kubectl apply 建立 Secret:

2.檢視secret

2.1通過 kubectl get secret 檢視存在的 secret

顯示有兩個資料條目

 

2.2通過kubectl describe secret 檢視條目的 Key

 

2.3通過kubectl edit secret mysecret 檢視vlaue