一般用configmap去管理一些配置文件,或者是大量的環境變量信息,configmap將配置和pod分開,有一個nginx,nginx.conf---》configmap.nginx。更易於配置文件的更改和管理
configmap加密,而secret不加密
ConfigMap的簡單命令
kubectl create configmap test --from-file=/root/yaml/nginx.yaml #將/root/yaml/nginx.yaml文件寫入configmap 名字為test(存儲配置文件) kubectl create configmap test02 --from-file=/root/yaml/ #將/root/yaml/目錄下的文件寫入configmap,configmap 名字為test02 kubectl create configmap test01 --from-literal=MYSQL_ROOT_PASSWORD=123456 #變量儲存到configmap,變量名為MYSQL_ROOT_PASSWORD,值為123456,可以跟更多變量,在尾部加--from-literal=key=value 即可 kubectl describe configmaps test #查詢上面的結果
apiVersion: v1 kind: ConfigMap metadata: nginx data: MYSQL_ROOT_PASSWORD: '123456' #定義一個變量,值類型為字符串,所以需要加引號 my.cnf: | [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql character-set-server = utf8 log-error = /var/log/mysqld.log # 中繼日志 relay-log=mysql-relay-bin replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% #文件名為 my.cnf 需要換行的話冒號后面需要加 |
簡單的使用configmap
apiVersion: v1 kind: ConfigMap metadata: name: test04 data: MYSQL_ROOT_PASSWORD: '123456' #定義一個變量,值類型為字符串,所以需要加引號 my.cnf: | [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql character-set-server = utf8 log-error = /var/log/mysqld.log # 中繼日志 relay-log=mysql-relay-bin replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% --- kind: Deployment apiVersion: apps/v1 metadata: name: mysql spec: selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.7 imagePullPolicy: IfNotPresent volumeMounts: - mountPath: /opt #掛載到那個目錄 name: test command: - sh - -c - sleep 100 volumes: - name: test #存儲卷的名稱 configMap: name: test04 #configmap名字 items: - key: my.cnf #configmap 內文件 path: my.cnf #configmap掛載到容器內的相對路徑
查看幫助:
kubectl explain deployment.spec.template.spec.volumes.configMap.items #查詢每個字段下面有哪些子項,並且有其說明這里,作為參考