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
[[email protected] ~]# kubectl create configmap test-config --from-literal=key-name=dayi123 --from-literal=key-passwd=dayi1234
configmap/test-config created
# 檢視建立的configmap的配置清單檔案
[[email protected] ~]# 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,
[[email protected] ~]# kubectl create configmap test-nginxconfig --from-file=nginxconfig=/etc/nginx/nginx.conf
configmap/test-nginxconfig created
# 檢視建立的nginx配置檔案的configmap,值為nginx配置檔案内容
[[email protected] ~]# 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:
[[email protected] ~]# kubectl create configmap nginx-dir-config --from-file=/etc/nginx/
configmap/nginx-dir-config created
4)使用資源配置清單建立configmap
使用資源配置清單定義configmap配置檔案時,需要定義的字段通常包括apiVersion、kind、metadata字段及用于存儲資料的關鍵字段“data”。
# 定義configmap資源的配置清單
[[email protected] 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鍵值資料
[[email protected] 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中的程序
[[email protected] configmap]# kubectl apply -f test-env-configmap.yaml
[[email protected] 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存儲卷進行挂載
[[email protected] 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中的容器存在該配置檔案時,該檔案不會被
[[email protected] 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對象
[[email protected] configmap]# kubectl create secret generic auth --from-literal=username=admin --from-literal=password=dayi123
secret/auth created
# 檢視建立的secret對象時,其值是加密的
[[email protected] 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=”指令來建立。
# 生成用于測試的私鑰和自簽證書
[[email protected] volumes]# openssl genrsa -out tls.key 2048
[[email protected] 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資源對象
[[email protected] 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使用加密後的密碼
[[email protected] 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中
[[email protected] 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
[[email protected] volumes]# kubectl create secret docker-registry aliyun-registry --docker-username=dayi123 --docker-password=dayi123 [email protected]
# 定義一個pod資源,使用上面建立的docker-registry做認證去阿裡鏡像倉庫拉取鏡像
[[email protected] 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