玩k8s? ----管理應用程式配置

一往無前,未來可期發表於2020-10-15

1.1: Secret資源

secret加密資料並存放Etcd中,讓Pod的容器以掛載Volume方式訪問。

應用場景:
1、https證書
2、secret存放docker registry認證資訊
3、存放檔案內容或者字串,例如使用者名稱密碼

Pod使用secret兩種方式:

  • 變數注入

  • 掛載

例如:建立一個secret用於儲存應用程式用到的使用者名稱和密碼

官方文件: https://kubernetes.io/docs/concepts/configuration/secret/

示例1: 建立一個secret用於儲存應用程式用到的使用者名稱和密碼

//不加密,建立secret資源
[root@master demo]# echo -n 'admin'>./username.txt ""
[root@master demo]# echo -n '1f2d1e2e676f'>./password.txt
[root@master demo]# kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
secret/db-user-pass created
[root@master demo]# kubectl get secret
NAME                   TYPE                                  DATA   AGE
db-user-pass           Opaque                                2      62s
default-token-mpxqj    kubernetes.io/service-account-token   3      16d
registry-pull-secret   kubernetes.io/dockerconfigjson        1      2d23h
[root@master demo]# kubectl describe secret db-user-pass
Name:         db-user-pass
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password.txt:  12 bytes
username.txt:  5 bytes

示例2: 變數注入方式在Pod中使用secret:

//建立加密secret資源
[root@master demo]# echo -n 'admin' | base64
YWRtaW4=
[root@master demo]# echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm

[root@master demo]# vim secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

[root@master demo]# kubectl apply -f secret.yaml 
secret/secret created
[root@master demo]# kubectl delete secret secret
secret "secret" deleted
[root@master demo]# kubectl get secret
NAME                   TYPE                                  DATA   AGE
db-user-pass           Opaque                                2      51m
default-token-mpxqj    kubernetes.io/service-account-token   3      16d
my-secret              Opaque                                2      66s
registry-pull-secret   kubernetes.io/dockerconfigjson        1      3d


[root@master demo]# kubectl get secret my-secret -o yaml
apiVersion: v1
data:
  password: MWYyZDFlMmU2N2Rm
  username: YWRtaW4=
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"password":"MWYyZDFlMmU2N2Rm","username":"YWRtaW4="},"kind":"Secret","metadata":{"annotations":{},"name":"my-secret","namespace":"default"},"type":"Opaque"}
  creationTimestamp: 2020-10-15T10:26:47Z
  name: my-secret
  namespace: default
  resourceVersion: "186838"
  selfLink: /api/v1/namespaces/default/secrets/my-secret
  uid: edf7eaf0-0ed0-11eb-b567-000c29a0cac9
type: Opaque

  • pod 中使用 secret資源

    方法一: 變數方式,引用上文的secret資源

//方法一: 變數方式,引用上文的secret資源
[root@master demo]# vim secret-var.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    env: "呼叫變數"
      - name: SECRET_USERNAME "呼叫的變數賦給它"
        valueFrom: "鍵值來源"
          secretKeyRef: "固定鍵值欄位"
            name: my-secret "呼叫資源名稱"
            key: username "呼叫username變數"
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: my-secret
            key: password
            
[root@master demo]# kubectl apply -f secret-var.yaml
pod/mypod created

[root@master demo]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          3m26s
[root@master demo]# kubectl exec -it mypod bash
root@mypod:/# echo $SECRET_USERNAME
admin
root@mypod:/# echo $SECRET_PASSWORD
1f2d1e2e67df
root@mypod:/# 

#key: username賦值給SECRET_USERNAME

#key: password 賦值給SECRET_PASSWORD

​ 方法二: 資料掛載方式在Pod中使用secret

[root@master demo]# vim secret-vol.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts: "掛載點"
    - name: li "掛載的卷名稱,與下面的掛載裝置名一致"
      mountPath: "/etc/li" "掛載點目錄"
      readOnly: true "只讀"
  volumes:  "掛載裝置"
  - name: li "掛載裝置卷自定義名,與上面掛載點的名字要一致"
    secret: "掛載資料型別"
      secretName: my-secret "掛載資料卷資源名,你建立的secret資源名"
[root@master demo]# kubectl create -f secret-vol.yaml
pod/mypod created
//進容器,進掛載點檢視掛載的檔案
[root@master demo]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          4m14s
[root@master demo]# kubectl exec -it mypod bash
root@mypod:/# ls /etc/li
password  username
root@mypod:/# cat /etc/li/password 
1f2d1e2e67dfroot@mypod:/# cat /etc/li/username
adminroot@mypod:/# 

1.2: ConfigMap

  • 與Secret類似,區別在於ConfigMap儲存的是不需要加密配置的資訊

    應用場景:應用配置, 不涉及加密

  • 第一種方法: 掛載資料卷的方式

    建立一個configmap用於儲存應用程式用到的欄位值

[root@master demo]# vim redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.password=123456
~                        
  • 建立configmap資源
[root@master demo]# kubectl create configmap redis.config --from-file=redis.properties
configmap/redis.config created
//檢視資源
[root@master demo]# kubectl get configmap
NAME           DATA   AGE
redis.config   1      9s
//用縮寫檢視
[root@master demo]# kubectl get cm
NAME           DATA   AGE
redis.config   1      67s  "configmap的資源名,用於後面引入"
[root@master demo]# kubectl describe cm redis.config
Name:         redis.config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data "資源資料"
====
redis.properties: "資源的檔案"
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

Events:  <none>

​ 資料掛載方式pod引入configmap資源

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
      volumeMounts: "掛載點"
      - name: config-volume
        mountPath: /etc/config
  volumes: "掛載裝置"
    - name: config-volume
      configMap:
        name: redis.config "引入的configmap資源名"
  restartPolicy: Never

[root@master demo]# kubectl apply -f configmap.yaml 
pod/mypod created
[root@master demo]# kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
mypod   0/1     ContainerCreating   0          3s
[root@master demo]# kubectl get pods
NAME    READY   STATUS      RESTARTS   AGE
mypod   0/1     Completed   0          57s
[root@master demo]# kubectl logs mypod
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

  • 第二種方法:變數引數形式

    建立configmap資源

[root@master demo]# vim myconfig.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello

[root@master demo]# kubectl apply -f myconfig.yaml
configmap/myconfig created
[root@master demo]# kubectl get configmap
NAME           DATA   AGE
myconfig       2      5m32s "這裡configmap資源名myconfig"
redis.config   1      23m

​ 引用configmap 資源

[root@master demo]# vim config-var.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: myconfig "引入的configmap的資源名myconfig"
              key: special.level "引入資源的變數"
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig "引入的configmap資源名myconfig"
              key: special.type "引入資源的變數"
  restartPolicy: Never

//清除已有的mypod資源
[root@master demo]# kubectl delete pod mypod
[root@master demo]# kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
mypod   0/1     ContainerCreating   0          11s
[root@master demo]# kubectl get pods
NAME    READY   STATUS      RESTARTS   AGE
mypod   0/1     Completed   0          58s
[root@master demo]# kubectl logs mypod "檢視輸入結果"
info hello

1.3: 應用程式如何動態更新配置?

ConfigMap更新時,業務也隨之更新的方案:

  • 當ConfigMap發生變更時,應用程式動態載入
  • 觸發滾動更新,即重啟服務

mark

示例:

//congigmap資料更新
[root@k8s-master ~]# vim configMap-volume-pod.yaml
...
data:
  redis.properties: |
    redis.host=192.168.100.200 "地址發生變化"
    redis.port=6379
    redis.password=123456
    ...
//重新載入配置pod    
[root@k8s-master ~]# kubectl apply -f configMap-volume-pod.yaml 
configmap/redis-config configured
pod/mypod configured
//檢視應用程式引用的資料
[root@k8s-master ~]# kubectl logs mypod
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
//可以發現資料沒有更新,三種方法解決
1.重建pod
2.應用程式configmap本身實現監聽本地配置檔案,如果configmap發生變化觸發配置熱更新
3.業務端採用sidecar 監聽configmap資源的變化,有變化則告訴業務更新

相關文章