Kubernetes -- secret (敏感數據管理)

https://www.kubernetes.org.cn/secretnode

secret 主要解決密碼、token、密鑰等敏感數據的配置問題,而不須要把這些敏感數據暴露到鏡像或者Pod Spec中
Secret能夠以Volume或者環境變量的方式使用(共兩種方式)mysql

Secret有三種類型:sql

  • Service Account:用來訪問Kubernetes API,由Kubernetes自動建立,而且會自動掛載到Pod的/run/secrets/kubernetes.io/serviceaccount目錄中;
  • Opaque:base64編碼格式的Secret,用來存儲密碼、密鑰等;
  • kubernetes.io/dockerconfigjson:用來存儲私有docker registry的認證信息;

實驗 (Opaque)

第一步:加密用戶及密碼docker

╭─root@node1 ~  
╰─➤  echo "123" | base64
MTIzCg==
╭─root@node1 ~  
╰─➤  echo "node1" | base64
bm9kZTEK

第二步:編寫secret的yml文件json

apiVersion: v1
kind: Secret
metadata:
   name: mysecret
data: 
   hostname: bm9kZTEK
   password: MTIzCg==

第三步:執行secret文件api

╭─root@node1 ~  
╰─➤  kubectl apply -f secret.yml 
secret/mysecret created

第四步:查看bash

╭─root@node1 ~  
╰─➤  kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-ngn4n   kubernetes.io/service-account-token   3      10d
mysecret              Opaque                                2      2m4s
╭─root@node1 ~  
╰─➤  kubectl describe secret mysecret
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  
Type:         Opaque

Data
====
hostname:  6 bytes
password:  4 bytes

第五步:獲取加密數據app

╭─root@node1 ~  
╰─➤  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:
  hostname: bm9kZTEK    # 加密數據
  password: MTIzCg==    # 加密數據
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"hostname":"bm9kZTEK","password":"MTIzCg=="},"kind":"Secret","metadata":{"annotations":{},"name":"mysecret","namespace":"default"}}
  creationTimestamp: "2019-08-30T08:00:24Z"
  name: mysecret
  namespace: default
  resourceVersion: "244709"
  selfLink: /api/v1/namespaces/default/secrets/mysecret
  uid: f8a21f4c-18ce-4b13-814a-c20ee5efbe23
type: Opaque

第六步:解碼ui

╭─root@node1 ~  
╰─➤  echo "MTIzCg==" | base64 --decode
123
╭─root@node1 ~  
╰─➤  echo "bm9kZTEK" | base64 --decode
node1

使用secret

以volume的形式掛載到pod

第一步:編寫pod的yml文件this

apiVersion: v1
kind: Pod
metadata:
   name: pod-secret
spec:
   containers:
   - name: busybox
     image: busybox
     imagePullPolicy: IfNotPresent
     command: ["/bin/sh","-c","touch test;sleep 60000"]
     volumeMounts:
     - name: du 
       mountPath: /tmp
   volumes:
   - name: du
     secret:
        secretName: mysecret

第二步:執行

╭─root@node1 ~  
╰─➤  kubectl apply -f busybox-secret.yml
pod/pod-secret created

第三步:進入pod查看

╭─root@node1 ~  
╰─➤  kubectl exec -it pod-secret /bin/sh
/ # ls
bin   dev   etc   home  proc  root  sys   test  tmp   usr   var
/ # cd tmp
/tmp # ls
hostname  password
/tmp # cat hostname
node1
/tmp # cat password
123
/tmp #

第四步:動態更新密碼

一、生成新密碼

╭─root@node1 ~  
╰─➤  echo 1234 | base64
MTIzNAo=

二、修改secret文件

apiVersion: v1
kind: Secret
metadata:
   name: mysecret
data: 
   hostname: bm9kZTEK
   password: MTIzNAo=

三、從新執行secret的yml文件

╭─root@node1 ~  
╰─➤  kubectl apply -f secret.yml 
secret/mysecret configured

第五步:查看密碼

╭─root@node1 ~  
╰─➤  kubectl exec -it pod-secret /bin/sh
/ # cd tmp
/tmp # ls
hostname  password
/tmp # cat password
1234

以環境變量的方式使用secret

第一步:編寫yml文件

apiVersion: v1
kind: Pod
metadata:
   name: pod-mysql
spec:
   containers:
   - name: mysql
     image: mysql
     imagePullPolicy: IfNotPresent
     env:
     - name: MYSQL_ROOT_PASSWORD
       valueFrom:
          secretKeyRef:
            name: mysecret
            key: password

第二步:執行

╭─root@node1 ~  
╰─➤  kubectl apply -f mysql-secret.yml 
pod/pod-mysql created

第三步:查看

╭─root@node1 ~  
╰─➤  kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
pod-mysql   1/1     Running   0          8s
╭─root@node1 ~  
╰─➤  kubectl exec -it pod-mysql bash    
root@pod-mysql:/# env
...
MYSQL_ROOT_PASSWORD=1234 
...
root@pod-mysql:/#

相關文章
相關標籤/搜索