kubernets/kubernetes-MD/kubernetes资源对象ConfigMap.md

396 lines
9.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>Kubernetes资源对象ConfigMap</center></h1>
------
## 一ConfigMap
用来存储配置文件的kubernetes资源对象所有的配置内容都存储在etcd中ConfigMap与 Secret 类似
#### 1.ConfigMap与 Secret 的区别
ConfigMap 保存的是不需要加密的、应用所需的配置信息
ConfigMap 的用法几乎与 Secret 完全相同:可以使用 kubectl create configmap 从文件或者目录创建 ConfigMap也可以直接编写 ConfigMap 对象的 YAML 文件
#### 2.创建ConfigMap
方式1通过直接在命令行中指定configmap参数创建即--from-literal
方式2通过指定文件创建即将一个配置文件创建为一个ConfigMap--from-file=<文件>
方式3通过指定目录创建即将一个目录下的所有配置文件创建为一个ConfigMap--from-file=<目录>
方式4事先写好标准的configmap的yaml文件然后kubectl create -f 创建
通过命令行参数--from-literal创建
创建命令
```shell
[root@master yaml]# kubectl create configmap test-config1 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'
configmap/test-config1 created
```
结果如下面的data内容所示
```shell
[root@master yaml]# kubectl get configmap test-config1 -o yaml
apiVersion: v1
data:
db.host: 10.5.10.116
db.port: "3306"
kind: ConfigMap
metadata:
creationTimestamp: "2019-02-14T08:22:34Z"
name: test-config1
namespace: default
resourceVersion: "7587"
selfLink: /api/v1/namespaces/default/configmaps/test-config1
uid: adfff64c-3031-11e9-abbe-000c290a5b8b
```
通过指定文件创建:
编辑配置文件app.properties内容如下
```shell
[root@master yaml]# cat app.properties
property.1 = value-1
property.2 = value-2
property.3 = value-3
property.4 = value-4
[mysqld]
!include /home/wing/mysql/etc/mysqld.cnf
port = 3306
socket = /home/wing/mysql/tmp/mysql.sock
pid-file = /wing/mysql/mysql/var/mysql.pid
basedir = /home/mysql/mysql
datadir = /wing/mysql/mysql/var
```
创建(可以有多个--from-file
```shell
[root@master yaml]# kubectl create configmap test-config2 --from-file=./app.properties
```
结果如下面data内容所示
```shell
[root@master yaml]# kubectl get configmap test-config2 -o yaml
apiVersion: v1
data:
app.properties: |
property.1 = value-1
property.2 = value-2
property.3 = value-3
property.4 = value-4
[mysqld]
!include /home/wing/mysql/etc/mysqld.cnf
port = 3306
socket = /home/wing/mysql/tmp/mysql.sock
pid-file = /wing/mysql/mysql/var/mysql.pid
basedir = /home/mysql/mysql
datadir = /wing/mysql/mysql/var
kind: ConfigMap
metadata:
creationTimestamp: "2019-02-14T08:29:33Z"
name: test-config2
namespace: default
resourceVersion: "8176"
selfLink: /api/v1/namespaces/default/configmaps/test-config2
uid: a8237769-3032-11e9-abbe-000c290a5b8b
```
通过指定文件创建时configmap会创建一个key/value对key是文件名value是文件内容。如不想configmap中的key为默认的文件名可以在创建时指定key名字
```shell
[root@master yaml]# kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
```
指定目录创建:
configs 目录下的config-1和config-2内容如下所示
```shell
[root@master yaml]# tail configs/config-1
aaa
bbb
c=d
[root@master yaml]# tail configs/config-2
eee
fff
h=k
```
创建
```shell
[root@master yaml]# kubectl create configmap test-config3 --from-file=./configs
```
结果下面data内容所示
```shell
[root@master yaml]# kubectl get configmap test-config3 -o yaml
apiVersion: v1
data:
config-1: |
aaa
bbb
c=d
config-2: |
eee
fff
h=k
kind: ConfigMap
metadata:
creationTimestamp: "2019-02-14T08:37:05Z"
name: test-config3
namespace: default
resourceVersion: "8808"
selfLink: /api/v1/namespaces/default/configmaps/test-config3
uid: b55ffbeb-3033-11e9-abbe-000c290a5b8b
```
指定目录创建时configmap内容中的各个文件会创建一个key/value对key是文件名value是文件内容忽略子目录
通过事先写好configmap的标准yaml文件创建
yaml文件内容如下 注意其中一个key的value有多行内容时的写法
```shell
[root@master yaml]# cat configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config4
namespace: default
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
```
创建
```shell
[root@master yaml]# kubectl apply -f configmap.yaml
configmap/test-config4 created
```
结果如下面data内容所示
```shell
[root@master yaml]# kubectl get configmap test-config4 -o yaml
apiVersion: v1
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"cache_host":"memcached-gcxt","cache_port":"11211","cache_prefix":"gcxt","my.cnf":"[mysqld]\nlog-bin = mysql-bin\nhaha = hehe\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config4","namespace":"default"}}
creationTimestamp: "2019-02-14T08:46:57Z"
name: test-config4
namespace: default
resourceVersion: "9639"
selfLink: /api/v1/namespaces/default/configmaps/test-config4
uid: 163fbe1e-3035-11e9-abbe-000c290a5b8b
```
查看configmap的详细信息
```shell
[root@master yaml]# kubectl describe configmap
```
#### 3.使用ConfigMap
通过环境变量的方式直接传递pod
通过在pod的命令行下运行的方式
使用volume的方式挂载入到pod内
示例ConfigMap文件
```shell
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
```
通过环境变量使用:
使用valueFrom、configMapKeyRef、name、key指定要用的key
```shell
[root@master yaml]# cat testpod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: daocloud.io/library/nginx
env:
- name: SPECIAL_LEVEL_KEY //这里是容器里设置的新变量的名字
valueFrom:
configMapKeyRef:
name: special-config //这里是来源于哪个configMap
key: special.how //configMap里的key
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
```
测试
```shell
[root@master yaml]# kubectl exec -it dapi-test-pod /bin/bash
root@dapi-test-pod:/# echo $SPECIAL_TYPE_KEY
charm
```
通过envFrom、configMapRef、name使得configmap中的所有key/value对都自动变成环境变量
```shell
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: daocloud.io/library/nginx
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
```
这样容器里的变量名称直接使用configMap里的key名
```shell
[root@master yaml]# kubectl exec -it dapi-test-pod /bin/bash
root@dapi-test-pod:/# env
HOSTNAME=dapi-test-pod
NJS_VERSION=1.15.8.0.2.7-1~stretch
NGINX_VERSION=1.15.8-1~stretch
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
PWD=/
special.how=very
HOME=/root
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
TERM=xterm
SHLVL=1
KUBERNETES_SERVICE_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
special.type=charm
KUBERNETES_SERVICE_HOST=10.96.0.1
```
作为volume挂载使用
```shell
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-configmap
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-configmap
image: daocloud.io/library/nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: config-volume3
mountPath: /tmp/config3
volumes:
- name: config-volume3
configMap:
name: test-config-3
```
进入容器中/tmp/config4查看
```shell
[root@master yaml]# kubectl exec -it nginx-configmap-7447bf77d6-svj2t /bin/bash
root@nginx-configmap-7447bf77d6-svj2t:/# ls /tmp/config4/
cache_host cache_port cache_prefix my.cnf
root@nginx-configmap-7447bf77d6-svj2t:/# cat /tmp/config4/cache_host
memcached-gcxt
可以看到在config4文件夹下以每一个key为文件名value为值创建了多个文件。
```
假如不想以key名作为配置文件名可以引入items 字段在其中逐个指定要用相对路径path替换的key
```shell
volumes:
- name: config-volume4
configMap:
name: test-config4
items:
- key: my.cnf //原来的key名
path: mysql-key
- key: cache_host //原来的key名
path: cache-host
```
注意:
删除configmap后原pod不受影响然后再删除pod后重启的pod的events会报找不到cofigmap的volume
pod起来后再通过kubectl edit configmap …修改configmap过一会pod内部的配置也会刷新
在容器内部修改挂进去的配置文件后过一会内容会再次被刷新为原始configmap内容