k8s筆記六(kubernetes中容器的配置)

1、使用ConfigMap配置管理應用程序

       Kubernetes基於ConfigMap對象實現了將配置文件從容器中解耦出來,並將配置數據以鍵值對的形式進行存儲,這些數據可以在Pod中使用或者爲系統組件提供配置。

(1)創建ConfigMap

       ConfigMap的創建可以通過命令創建或者資源清單定義文件創建,通過命令創建時的命令語法格式如下:

Kubectl create configmap <map-name> <data-source>

       通過命令創建時,map-name爲ConfigMap對象的名稱;<data-source>爲數據源,它可以通過值,文件或目錄獲取;無論它的值是什麼,它都要轉化爲ConfigMap對象中的ke-value數據。

1)使用值直接創建

      使用”kubectl create configmap”命令創建時,可使用”—from-literal”選項在命令行直接給出鍵值對來創建ConfigMap對象。

# 創建一個名稱爲test-config的configmap 
[root@master01 ~]# kubectl create configmap test-config --from-literal=key-name=dayi123 --from-literal=key-passwd=dayi1234
configmap/test-config created
# 查看創建的configmap的配置清單文件
[root@master01 ~]# kubectl get configmaps test-config -o yaml
apiVersion: v1
data:
  key-name: dayi123
  key-passwd: dayi1234
kind: ConfigMap
metadata:
  creationTimestamp: "2019-01-28T06:23:22Z"
  name: test-config
  namespace: default
  resourceVersion: "2649395"
  selfLink: /api/v1/namespaces/default/configmaps/test-config
  uid: 365e5478-22c5-11e9-9f5c-000c298d15e0

       2)基於文件創建

       爲“kubectl create configmap”命令使用”—from-file”選項可以基於文件內容來創建ConfigMap對象;”—from-file”選項可以使用多次以傳遞多個文件內容。命令使用格式如下:

Kubectl create configmap <configmap_name> --from-file=<path-to-file>

       如果需要自行指定鍵名則需要在”—from-file”選項中直接指定自定義的鍵,命令格式如下:

Kubectl create configmap <configmap_name> --from-file=<my-key-name>=<path-to-file>

基於文件使用configmap創建nginx的配置文件:

# 基於文件創建configmap,
[root@master01 ~]# kubectl create configmap test-nginxconfig --from-file=nginxconfig=/etc/nginx/nginx.conf
configmap/test-nginxconfig created
# 查看創建的nginx配置文件的configmap,值爲nginx配置文件內容
[root@master01 ~]# kubectl get configmap test-nginxconfig -o yaml
apiVersion: v1
data:
  nginxconfig: |+
    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/

    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
. . . . . .

       3)基於目錄創建

       若配置文件較多並且存在於同一個目錄時,可以將該目錄創建爲configmap。“—from-file”選項後面所跟的路徑指向一個目錄路徑就能將目錄下的所有文件一同創建於同一ConfigMap資源中,命令格式如下:

Kubectl create configmap <configmap_name> --from-file=<path-to-directory>

       當該目錄下有多個文件時,它們會分別被存儲爲多個不同的鍵值數據,如將nginx的配置文件目錄”/etc/nginx”創建爲configmap:

[root@master01 ~]# kubectl create configmap nginx-dir-config --from-file=/etc/nginx/
configmap/nginx-dir-config created

       4)使用資源配置清單創建configmap

       使用資源配置清單定義configmap配置文件時,需要定義的字段通常包括apiVersion、kind、metadata字段及用於存儲數據的關鍵字段“data”。

# 定義configmap資源的配置清單
[root@master01 configmap]# cat test01-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata: 
  name: configmap-demo
data:
  log_level: INFO
  log_file: /var/log/nginx/access.log
  config_file: /etc/nginx/nginx.conf

(2)向Pod環境變量傳遞ConfigMap對象鍵值數據

       Pod資源獲取環境變量時可以引用ConfigMap對象中的數據,具體做法是通過在env字段中爲valueFrom內嵌configMapKeyRef對象實現。

# 定義一個configmap資源,在定義一個pod資源通過環境變量傳遞configmap鍵值數據
[root@master01 configmap]# cat test-env-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-env-configmap
data:
  httpd_port: "8080"
---
apiVersion: v1
kind: Pod
metadata: 
  name: test-envconfigmap-demo
spec:
  containers:
  - image: busybox
    name: busybox-httpd
    command: ["/bin/httpd"]
    args: ["-f","-p","$(HTTPD_PORT)"]
    env:
    - name: HTTPD_PORT
      valueFrom:
        configMapKeyRef:
          name: test-env-configmap
          key: httpd_port
# 創建定義的資源並查看pod中的進程
[root@master01 configmap]# kubectl apply -f test-env-configmap.yaml
[root@master01 configmap]# kubectl exec test-envconfigmap-demo ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/httpd -f -p 8080

       向pod中傳遞configmap中定義的資源時,env字段中valueFrom字段內嵌的configMapKeyRef字段各內嵌字段作用如下:

           name:爲要引用configmap對象的名稱

           key:指定要引用ConfigMap對象中某鍵的名稱

           optional:用於爲當前Pod資源指名此引用是否可選

       configmap是名稱空間級別的資源,它必須要與應用它的pod資源在同一名稱空間中;當configmap中存在較多的鍵值數據時,pod資源支持在容器中使用envFrom字段直接將configmap資源中的所有鍵值一次性的完成導入。爲了避免多個configmap應用鍵值數據時產生鍵名衝突,可以在每個應用中將被導入的鍵使用prefix字段指定一個特殊的前綴;同時,prefix字段可以省略,省略時,所有變量名同ConfigMap中的鍵名。

(3)configmap存儲卷

       當configmap對象中的鍵值來源於較長的文件內容時,將其內容直接作爲文件進行應用是較好的選擇;實現方式是在定義pod資源時,將此類的configmap對象配置爲onfigmap類型的存儲卷,然後掛載至指定的目錄進行訪問。

       1)掛載整個存儲卷

       當configmap對象關聯爲pod資源的存儲卷時,configmap對象中的每個鍵都對應的表現爲一個文件,鍵名會轉爲文件名,鍵值爲文件內容;當pod資源使用該存儲卷時,僅需指明存儲卷名稱及要引用的configmap對象的名稱即可。

# 定義一個基於nginx的pod資源,配置文件使用前面創建的名稱爲nginx-dir-config的configmap存儲捲進行掛載
[root@master01 configmap]# cat test-nginx-configmap-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-nginx-voldemo
spec:
  containers:
  - image: nginx:1.12
    name: ngin-service
    volumeMounts:
    - name: ngxconfig
      mountPath: /etc/nginx/
      readOnly: true
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config

       2)掛載存儲卷中的部分鍵值

       在某些時候,可能不期望在容器中掛載某configmap存儲卷後於掛載點目錄導出所有文件;此時,可以使用configMap字段嵌套的的items字段完成該需求,該items字段嵌套的字段主要有以下三個字段:

              key:必選字段,要應用的鍵名稱

              path:對應的鍵於掛載點目錄中生成的文件的相對路徑

              mode:文件的權限模型

# 如果上面的配置文件中只使用nginx的主配置文件等部分配置,則volumes的定義如下
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config
      items:
      - key: nginx.conf
        path: nginx.conf
        mode: 0644
      - key: fastcgi.conf
        path: fastcgi.conf
      - key: mime.types
        path: mime.types

       3)獨立掛載存儲卷中的鍵值

       前面無論是掛載所有文件還是部分文件,掛載點目錄下的所有文件都會被隱藏;如果希望將configmap對象提供的配置文件補充於掛載點目錄,這種方式是最適合的,而這種方式是通過configmap字段來實現的。

# 只將nginx.conf配置文件補充掛載,其他的配置文件不動,當pod中的容器存在該配置文件時,該文件不會被
[root@master01 configmap]# cat test-nginx-configmap-vol03.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-nginx-voldemo03
spec:
  containers:
  - image: nginx:1.12
    name: ngin-service
    volumeMounts:
    - name: ngxconfig
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
      readOnly: true
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config

2、Secret資源的使用

      Secret資源的使用同configmap資源的使用方法基本相同,secret資源用於存放敏感數據。

(1)secret資源

      Secret資源也是通過鍵值得方式存儲數據,在pod資源中通過環境變量或存儲捲進行數據訪問,但secret對象僅會被分發至調用了此對象的pod資源所在的工作節點且只能由節點將其存儲於內存中,而在master節點上,secret對象以非加密的格式存儲於etcd中。

      Secret對象主要由兩種用途,一是作爲存儲卷注入到pod上由容器應用程序所使用,二是用於kubelet爲pod裏的容器拉取鏡像時向私有倉庫提供認證信息。Secret資源主要是由以下四種類型組成的:

          Opaque:自定義數據內容,base64編碼,用來定義存儲密碼、祕鑰、證書等數據,類型的標識符爲generic

          Kubernetes.io/service-account-token:service Account的認證信息,可在創建   service account時由kubernetes自動創建

          Kubernetes.io/dockerconfigjson:用來存儲docker鏡像倉庫的認證信息,類型表示   爲docker-registry

          Kuberbetes.io/tls:用於爲SSL通信模式存儲證書和私鑰文件,命令式創建時類型   標識爲tls。

(2)創建secret資源

      1)通過命令創建

      使用命令創建secret資源的方法同使用命令創建configmap資源的方法基本一致,通過命令創建secret對象的命令格式如下:

Kubectl create secret generic <SECRET_NAME> --from-literal=key=value

      創建完成一個secret對象,其數據會以base64的編碼格式進行加密。

# 創建一個用戶名和密碼的secret對象
[root@master01 configmap]# kubectl create secret generic auth --from-literal=username=admin --from-literal=password=dayi123
secret/auth created
# 查看創建的secret對象時,其值是加密的
[root@master01 configmap]# kubectl get secrets auth -o yaml
apiVersion: v1
data:
  password: ZGF5aTEyMw==
  username: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2019-01-29T09:24:28Z"
  name: auth
  namespace: default
  resourceVersion: "2799717"
  selfLink: /api/v1/namespaces/default/secrets/auth
  uid: ad5b869d-23a7-11e9-9f5c-000c298d15e0
type: Opaque

      使用命令創建時也可以使用”—from-file”命令從文件中直接加載;如果要基於證書文件創建用於SSL/TLS通信的Secret對象,則需要使用”kubectl create secret tls <SECRET_NAME> --cert—key=”命令來創建。

# 生成用於測試的私鑰和自簽證書
[root@master01 volumes]# openssl genrsa -out tls.key 2048
[root@master01 volumes]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Shanghai/L=Shanghai/O/dev/CN=www.dayi123.com -days 736
# 將生成的證書創建爲secret資源對象
[root@master01 volumes]# kubectl create secret tls test-ssl --key=tls.key --cert=tls.crt 
secret/test-ssl created

      2)通過定義資源清單的方式創建

      通過資源清單的方式定義secret資源對象時,除了定義標準的apiVersion、kind、metadata字段外,還需要定義如下的字段:

         data:”kev:value”格式的數據,通常是敏感的數據,數據格式是以base64編碼的數   據,需要用戶提前編碼。

         stringData:以名文格式定義的”key:value”數據,在創建Secret對象時會自動編碼   並保存於data字段中,如果使用”kubectl apply”命令創建,註解信息中可能還是會輸出這些信息。

         type:爲了便於變成方式處理Secret數據而提供的類型標識

# 定義一個secret資源清單,password使用加密後的密碼
[root@master01 volumes]# cat test-secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  password: ZGF5aTEyMwo=
stringData:
  username: admin
type: Opaque

      3)secret的使用

      Secret資源對象使用方法同configmap資源使用方法基本一致,可以注入爲環境變量,也可以以存儲卷的形式掛載使用,但是不建議以環境變量的方式使用。

      在pod中將secret掛載爲存儲卷使用時,除了類型及標識需要替換爲Secret及secretName外,其他的用法同configmap存儲卷的用法基本類似。

# 定義一個基於nginx鏡像的pod資源,將前面創建的nginx-test掛載到nginx中
[root@master01 volumes]# cat test-nginx-secret-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-nginx-secret
spec:
  containers:
  - name: secret-nginx
    image: nginx:1.12
    volumeMounts:
    - name: nginxcert
      mountPath: /etc/nginx/certs
      readOnly: true
  volumes:
  - name: nginxcert
    secret:
      secretName: test-ssl

      4)imagePullSecret資源對象的使用

      imagePullSecret資源可以輔助kubelet從需要認證的私有倉庫完成認證並獲取鏡像。使用imagePullSecret的方式有兩種,都需要創建docker-registry類型的Secret對象,一種是通過定義pod資源時明確通過”imagePullSecret”字段給出;另一種是將其添加到特定的ServiceAccount對象中。

      在創建docker-registry類型的Secret對象時,使用如下的命令格式創建

kubectl create secret docker-registry <SECRET_NAME> --docker-user=<USERNAME> --docker-password=<PASSWORD> --docker-email=<DOCKER_USER_EMAIL>

       如下,創建一個阿里雲認證的docker-registry,並創建pod資源去阿里雲拉取鏡像。

# 創建docker-registry
[root@master01 volumes]# kubectl create secret docker-registry aliyun-registry --docker-username=dayi123 --docker-password=dayi123 [email protected]
# 定義一個pod資源,使用上面創建的docker-registry做認證去阿里鏡像倉庫拉取鏡像
[root@master01 volumes]# cat docker-pull-ali.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: secret-imagepull-ail
spec:
  imagePullSecrets:
  - name: aliyun-registry
  containers:
  - name: test-busybox
image: registry.cn-hangzhou.aliyuncs.com/dayi123/busybox:v1.1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章