k8s應用機密信息與配置管理(九)--技術流ken

 

secret

 

應用啓動過程當中可能須要一些敏感信息,好比訪問數據庫的用戶名密碼或者祕鑰。將這些信息直接保存在容器鏡像中顯然不妥,Kubernetes 提供的解決方案是 Secret。數據庫

Secret 會以密文的方式存儲數據,避免了直接在配置文件中保存敏感信息。Secret 會以 Volume 的形式被 mount 到 Pod,容器可經過文件的方式使用 Secret 中的敏感數據;此外,容器也能夠環境變量的方式使用這些數據。api

Secret 可經過命令行或 YAML 建立。好比但願 Secret 中包含以下信息:app

 

用戶名 adminui

密碼 123456this

 

建立 Secret

有四種方法建立 Secret:編碼

 

1. 經過 --from-literal:spa

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

每一個 --from-literal 對應一個信息條目。命令行

 

2. 經過 --from-file:3d

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

每一個文件內容對應一個信息條目。code

 

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 對應一個信息條目。

 

4. 經過 YAML 配置文件:

 

文件中的敏感數據必須是經過 base64 編碼後的結果。

 

[root@ken ~]# echo -n admin | base64
YWRtaW4=
[root@ken ~]# echo -n 123456 | base64
MTIzNDU2

 

執行 kubectl apply 建立 Secret

[root@ken ~]# kubectl apply -f mysecret.yml
secret/mysecret created

 

查看secret

 

第一步:經過kubectl get secret查看

[root@ken ~]# kubectl get secret mysecret
NAME       TYPE     DATA   AGE
mysecret   Opaque   2      99s

 

第二步:顯示有兩個數據條目,kubectl describe secret 查看條目的 Key:

[root@ken ~]# kubectl describe secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  
Type:         Opaque

Data
====
password:  6 bytes
username:  5 bytes

 

第三步:若是還想查看 Value,能夠用 kubectl edit secret mysecret:

[root@ken ~]# kubectl edit secret mysecret
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  password: MTIzNDU2
  username: YWRtaW4=
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"password":"MTIzNDU2","username":"YWRtaW4="},"kind":"Secret","metadata":{"annotations":{},"name":"mysecret","namespace":"default"}}
  creationTimestamp: "2019-01-30T12:28:34Z"
  name: mysecret
  namespace: default
  resourceVersion: "111466"
  selfLink: /api/v1/namespaces/default/secrets/mysecret
  uid: 8f86e1f4-248a-11e9-9172-000c292d5bb8
type: Opaque

 

第四步:而後經過 base64 將 Value 反編碼:

[root@ken ~]# echo -n MTIzNDU2 | base64 --decode
123456

[root@ken ~]# echo -n YWRtaW4= | base64 --decode
admin

 

volume 方式使用 Secret

 

Pod 能夠經過 Volume 或者環境變量的方式使用 Secret

 

第一步:Pod 的配置文件以下所示:

① 定義 volume foo,來源爲 secret mysecret。

② 將 foo mount 到容器路徑 /etc/foo,可指定讀寫權限爲 readOnly。

 

第二步:建立 Pod 並在容器中讀取 Secret:

[root@ken ~]# kubectl apply -f mypod.yml
pod/mypod created
[root@ken ~]# kubectl exec -it mypod sh
/ # ls /etc/foo
password  username
/ # cat /etc/foo/password 
/ # cat /etc/foo/password 
123456/ # 
/ # cat /etc/foo/username 
admin/ #

能夠看到,Kubernetes 會在指定的路徑 /etc/foo 下爲每條敏感數據建立一個文件,文件名就是數據條目的 Key,這裏是 /etc/foo/username /etc/foo/passwordValue 則以明文存放在文件中。

 

第三步:咱們也能夠自定義存放數據的文件名,好比將配置文件改成:

這時數據將分別存放在 /etc/foo/my-group/my-username 和 /etc/foo/my-group/my-password 中。

 

以 Volume 方式使用的 Secret 支持動態更新:Secret 更新後,容器中的數據也會更新。

 

第四步:將 password 更新爲 abcdefbase64 編碼爲 YWJjZGVm

更新 Secret。

[root@ken ~]# kubectl apply -f mysecret.yml
secret/mysecret configured

 

第五步:幾秒鐘後,新的 password 會同步到容器。

[root@ken ~]# kubectl exec -it mypod sh
/ # ls /etc/foo
password  username

/ # cat /etc/foo/password 
abcdef/ # 

 

ConfigMap 管理配置

 

Secret 能夠爲 Pod 提供密碼、Token、私鑰等敏感數據;對於一些非敏感數據,好比應用的配置信息,則能夠用 ConfigMap。

 

ConfigMap 的建立和使用方式與 Secret 很是相似,主要的不一樣是數據以明文的形式存放。

與 Secret 同樣,ConfigMap 也支持四種建立方式:

 

1. 經過 --from-literal:

kubectl create configmap myconfigmap --from-literal=config1=xxx --from-literal=config2=yyy

每一個 --from-literal 對應一個信息條目。

 

2. 經過 --from-file:

echo -n xxx > ./config1
echo -n yyy > ./config2
kubectl create configmap myconfigmap --from-file=./config1 --from-file=./config2

每一個文件內容對應一個信息條目。

 

3. 經過 --from-env-file:

cat << EOF > env.txt
config1=xxx
config2=yyy
EOF
kubectl create configmap myconfigmap --from-env-file=env.txt
文件 env.txt 中每行 Key=Value 對應一個信息條目。

 

4. 經過 YAML 配置文件:

 


文件中的數據直接以明文輸入。

與 Secret 同樣,Pod 也能夠經過 Volume 或者環境變量的方式使用 Secret。

 

Volume 方式:

 

 

大多數狀況下,配置信息都以文件形式提供,因此在建立 ConfigMap 時一般採用 --from-file 或 YAML 方式,讀取 ConfigMap 時一般採用 Volume 方式。

 

與 Secret 同樣,Volume 形式的 ConfigMap 也支持動態更新。

相關文章
相關標籤/搜索