(十)Kubernetes ConfigMap和Secret

ConfigMap資源

介紹

ConfigMap是讓配置文件從鏡像中解耦,讓鏡像的可移植性和可複製性。許多應用程序會從配置文件、命令行參數或環境變量中讀取配置信息。這些配置信息須要與docker image解耦,你總不能每修改一個配置就重作一個image吧?ConfigMap API給咱們提供了向容器中注入配置信息的機制,ConfigMap能夠被用來保存單個屬性,也能夠用來保存整個配置文件或者JSON二進制大對象。ConfigMap保存的值爲鍵值方式,value長度沒有限制。html

建立ConfigMap

ConfigMap支持命令建立和使用清單建立。有如下四種方式建立。node

一、命令直接建立 --from-literal:mysql

「kubectl create configmap」命令使用「--from-literal」選項可在命令行直接給出鍵值來建立ConfigMap對象,重複使用此選項能夠傳遞多個鍵值對。格式以下:linux

kubectl create configmap NAME --from-literal=key1=value1 --from-literal=key2=value2
[root@k8s-master ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=www.ilinux.cn    #建立cm資源nginx-config,並指定兩個鍵值
configmap/nginx-config created
[root@k8s-master ~]# kubectl get cm    #查看cm資源
NAME           DATA   AGE
nginx-config   2      4s
[root@k8s-master ~]# kubectl describe cm/nginx-config    #查看cm資源nginx-config的詳細信息
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
80
server_name:
----
www.ilinux.cn
Events:  <none>

二、命令行基於文件建立 --from-file:nginx

「kubectl create configmap」命令使用「--from-file」選項便可基於文件內容來建立ConfigMap對象,一樣能夠重複屢次使用。格式以下:web

kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
[root@k8s-master ~]# mkdir configmap && cd configmap    #建立一個測試目錄
[root@k8s-master configmap]# vim www.conf    #編輯文件內容用做cm的value
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}
[root@k8s-master configmap]# kubectl create configmap nginx-www --from-file=www.conf=./www.conf    #使用上面建立的文件來建立cm資源對象
configmap/nginx-www created
[root@k8s-master configmap]# kubectl get cm    #查看cm資源對象
NAME           DATA   AGE
nginx-config   2      7m1s
nginx-www      1      5s
[root@k8s-master configmap]# kubectl get cm nginx-www -o yaml    #查看cm資源對象nginx-www的現象信息
apiVersion: v1
data:
  www.conf: |
    server {
        server_name www.ilinux.cn;
        listen 80;
        root /data/web/html/;
    }
kind: ConfigMap
......

三、命令行基於目錄建立 --from-file:redis

若是配置文件數量較多且存儲於有限的目錄之中,kubectl還提供了基於目錄直接將多個文件分別收納爲鍵值數據的ConfigMap資源建立方式。將「--from-file」選項後面所跟的路徑指向一個目錄路徑就能將目錄下的全部文件一同建立於同一ConfigMap資源中,命令格式以下:sql

kubectl create configmap <configmap_name> --from-file=<path-to-directory>
這裏假設/data/configs/nginx/conf.d/這個目錄下有許多的nginx的配置文件,下面這條命令則將這個目錄下的全部配置文件在建立ConfigMap資源時,會分別存儲爲對應的鍵值數據。
# kubectl create configmap nginx-config-files --from-file=/data/configs/nginx/conf.d/

四、使用資源清單建立:docker

基於清單文件建立ConfigMap資源時,它所使用的字段包括一般的apiVersionkindmetadata字段,以及用於存儲數據的關鍵字段「data」編程

[root@k8s-master ~]# kubectl explain cm
KIND:     ConfigMap
VERSION:  v1
FIELDS:
apiVersion    <string>
kind    <string>
metadata    <Object>
binaryData    <map[string]string>
data    <map[string]string>
[root@k8s-master configmap]# vim configmap-demo.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo
  namespace: default
data:
  log_level: INFO
  log_file: /var/log/test.log
[root@k8s-master configmap]# kubectl apply -f configmap-demo.yaml 
configmap/configmap-demo created
[root@k8s-master configmap]# kubectl get cm
NAME             DATA   AGE
configmap-demo   2      6s
nginx-config     2      23m
nginx-www        1      16m
[root@k8s-master configmap]# kubectl get cm/configmap-demo -o yaml
apiVersion: v1
data:
  log_file: /var/log/test.log
  log_level: INFO
kind: ConfigMap
metadata:
....

使用ConfigMap

一、環境變量方式注入到Pod

[root@k8s-master configmap]# vim pod-configmap-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-1
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    env:
    - name: NGINX_SERVER_PORT
      valueFrom:    #下面這一段表示變量NGINX_SERVER_PORT的值來自於configmap資源nginx-config的key(nginx_port)的值
        configMapKeyRef:
          name: nginx-config    #configmap資源名稱
          key: nginx_port      #configmap資源裏面的key名
    - name: NGINX_SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: server_name
[root@k8s-master configmap]# kubectl apply -f pod-configmap-1.yaml    #建立Pod
pod/pod-cm-1 created
[root@k8s-master configmap]# kubectl get pods     #查看pod
NAME       READY   STATUS    RESTARTS   AGE
pod-cm-1   1/1     Running   0          4s
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX    #鏈接pod資源pod-cm-1並執行命令printenv打印環境變量。過濾是否有上面定義的兩個環境變量
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=www.ilinux.cn

#測試,修改端口,能夠發現使用環境變量的注入pod中的端口不會根據配置的變動而改變
[root@k8s-master configmap]# kubectl edit cm/nginx-config   #編輯cm資源nginx-config將nginx_port值改成8080
......
apiVersion: v1
data:
  nginx_port: "8080"
......
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=www.ilinux.cn

二、存儲卷方式掛載ConfigMap

[root@k8s-master configmap]# vim pod-configmap-2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-2
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/config.d/
      readOnly: true
  volumes:    #建立一個存儲卷
  - name: nginxconf    #存儲卷名稱
    configMap:    #使用configMap類型
      name: nginx-config    #指定configmap資源名稱
[root@k8s-master configmap]# kubectl apply -f pod-configmap-2.yaml    #建立Pod
pod/pod-cm-2 created
[root@k8s-master configmap]# kubectl get pods     #查看pod
NAME       READY   STATUS    RESTARTS   AGE
pod-cm-1   1/1     Running   0          10m
pod-cm-2   1/1     Running   0          4s
[root@k8s-master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh    #鏈接pod資源pod-cm-2,並進入到掛載目錄查看。
/ # ls /etc/nginx/config.d/
nginx_port   server_name
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # cat nginx_port 
8080
/etc/nginx/config.d # cat server_name 
www.ilinux.cn 


#測試,修改端口,能夠發現使用volume的方式掛載configmap到容器中,支持動態更新。
[root@k8s-master configmap]# kubectl edit cm/nginx-config    #編輯cm資源nginx-config將nginx_port值改成8088
apiVersion: v1
data:
  nginx_port: "8088"
[root@k8s-master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh
/etc/nginx/config.d # cat nginx_port 
8088
/etc/nginx/config.d #

示例

這裏使用上面建立的configmap資源nginx-www示例。

1)編輯資源清單文件

[root@k8s-master configmap]# vim pod-configmap-3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-3
  namespace: default
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.12
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxwww
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: nginxwww
    configMap:
      name: nginx-www

2)建立Pod

[root@k8s-master configmap]# kubectl apply -f pod-configmap-3.yaml 
pod/pod-cm-3 created
[root@k8s-master configmap]# kubectl get pods -o wide   #查看pod
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
pod-cm-3   1/1     Running   0          5s    10.244.1.92   k8s-node1   <none>           <none>

3)進入到Pod中查看配置文件,並建立對應的數據目錄進行測試

[root@k8s-master configmap]# kubectl exec -it pod-cm-3 -- /bin/sh    #進入到pod中
# ls /etc/nginx/conf.d
www.conf
# cat /etc/nginx/conf.d/www.conf    #查看生成的www.conf配置文件
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}

# nginx -T    #查看當前nginx加載的配置文件
......
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}
# mkdir -p /data/web/html    #建立數據目錄
# echo "<h1>ConfigMap Pod Test</h1>" >> /data/web/html/index.html    #建立測試文件

#這裏拿kubernetes集羣節點測試
[root@k8s-master ~]# vim /etc/hosts    #編輯hosts文件將上面的pod和對應的域名進行解析
10.244.1.92    www.ilinux.cn
[root@k8s-master ~]# curl www.ilinux.cn    #訪問測試
<h1>ConfigMap Pod Test</h1>

Secret資源

介紹

Secret對象存儲數據的方式及使用方式相似於ConfigMap對象,以鍵值方式存儲數據,在Pod資源中經過環境變量或存儲捲進行數據訪問,解決了密碼、token、密鑰等敏感數據的配置問題,而不須要將這些敏感數據暴露到鏡像或者pod spec中。另外,Secret對象的數據存儲和打印格式爲Base64編碼的字符串,所以用戶在建立Secret對象時也要提供此種編碼格式的數據。在容器中以環境變量或存儲卷的方式訪問時,它們會被自動解碼爲明文格式。須要注意的是,若是是在Master節點上,Secret對象以非加密的格式存儲在etcd中,因此須要對etcd的管理和權限進行嚴格控制。

Secret的四種類型

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

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

  • kubernetes.io/dockerconfigjson:用來存儲Docker鏡像倉庫的認證信息,類型標識符爲docker-regiestry

  • kubernetes.io/tls:用於爲SSL通訊模式存儲證書和私鑰文件,命令式建立時類型標識爲tls

建立Secret

一、命令直接建立 --from-literal:

「kubectl create secret generic」命令使用「--from-literal」選項可在命令行直接給出鍵值來建立ConfigMap對象,重複使用此選項能夠傳遞多個鍵值對。格式以下:

kubectl create secret generic NAME --from-literal=key1=value1 --from-literal=key2=value2
[root@k8s-master ~]# kubectl create secret generic mysql-auth --from-literal=username=root --from-literal=password=MyP@sswd     #建立secret資源mysql-auth,並指定兩個鍵值
secret/mysql-auth created
[root@k8s-master ~]# kubectl get secret     #查看secret資源
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d
mysql-auth            Opaque                                2      17s
[root@k8s-master ~]# kubectl describe secret/mysql-auth    #查看secret資源mysql-auth的詳細信息
Name:         mysql-auth
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  8 bytes
username:  4 bytes

二、命令行基於文件建立 --from-file:

「kubectl create secret generic」命令使用「--from-file」選項便可基於文件內容來建立ConfigMap對象,一樣能夠重複屢次使用。格式以下:

kubectl create secret generic my-secret --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
[root@k8s-master ~]# mkdir secret && cd secret
[root@k8s-master secret]# echo -n admin > ./username
[root@k8s-master secret]# echo -n 123456 > ./password
[root@k8s-master secret]# 
[root@k8s-master secret]# kubectl create secret generic mysecret --from-file=username=./username --from-file=password=./password
secret/mysecret created
[root@k8s-master secret]# kubectl get secret 
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d
mysecret              Opaque                                2      6s
mysql-auth            Opaque                                2      5m23s
[root@k8s-master secret]# kubectl get secret/mysecret -o yaml
apiVersion: v1
data:
  password: MTIzNDU2    #這裏能夠看到secret存儲的值都是base64編碼格式
  username: YWRtaW4=
kind: Secret
metadata
......

三、使用資源清單建立:

Secret資源是標準的Kubernetes API對象,除了標準的apiVersionkindmetadata字段,它可用的其餘字段以下:

data    <map[string]string>    #"key:value"格式的數據,一般是敏感信息,數據格式須要以Base64格式編碼的字符串,所以須要事先完成編碼
stringData    <map[string]string>    #以明文格式(非Base64編碼)定義的「key:value"數據;無須事先對數據進行Base64編碼,而是在建立爲Secret對象時自動進行編碼並保存於data字段中。
type    <string>    #僅是爲了便於編程方式處理Secret數據而提供的類型標識。
[root@k8s-master secret]# vim secret-demo.yaml
apiVersion: v1
kind: Secret
metadata:
  name: secret-demo
  namespace: default
stringData:
  username: redis
  password: redisP@ss
type: Opaque
[root@k8s-master secret]# kubectl apply -f secret-demo.yaml 
secret/secret-demo created
[root@k8s-master secret]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d1h
mysecret              Opaque                                2      28m
mysql-auth            Opaque                                2      33m
secret-demo           Opaque                                2      5s
[root@k8s-master secret]# kubectl get secret/secret-demo -o yaml
apiVersion: v1
data:
  password: cmVkaXNQQHNz
  username: cmVkaXM=
kind: Secret
metadata:
......

使用Secret

相似於Pod消費ConfigMap對象的方式,Secret對象能夠注入爲環境變量,也能夠存儲爲存儲卷形式掛載使用。由於Secret默認保存的是非明文格式,經過注入爲環境變量實爲不明智。

存儲卷方式示例:

這裏假設須要爲Nginx測試建立SSL虛擬主機

1)首先建立私鑰和自簽證書

[root@k8s-master secret]# openssl genrsa -out nginx.key 2048
[root@k8s-master secret]# openssl req -new -x509 -key nginx.key -out nginx.crt -subj /C=CN/ST=ShenZhen/L=ShenZhen/O=DevOps/CN=www.ilinux.cn

2)建立secret

[root@k8s-master secret]# kubectl create secret tls nginx-ssl --key=./nginx.key --cert=./nginx.crt 
secret/nginx-ssl created
[root@k8s-master secret]# kubectl get secret nginx-ssl
NAME        TYPE                DATA   AGE
nginx-ssl   kubernetes.io/tls   2      14s

3)編輯資源清單

[root@k8s-master secret]# vim pod-secret-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume-demo
  namespace: default
spec:
  containers:
  - name: web-server
    image: nginx:1.12
    volumeMounts:
    - name: nginxcert
      mountPath: /etc/nginx/ssl/
      readOnly: true
  volumes:
  - name: nginxcert
    secret:
      secretName: nginx-ssl

4)建立pod並驗證

[root@k8s-master secret]# kubectl apply -f pod-secret-demo.yaml 
pod/secret-volume-demo created
[root@k8s-master secret]# kubectl exec -it secret-volume-demo -- /bin/sh
# ls /etc/nginx/ssl
tls.crt  tls.key
相關文章
相關標籤/搜索