Kubernetes基础:ConfigMap:增删改查

Kubernetes 文档

配置 Pod 使用 ConfigMap:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/
使用 ConfigMap 来配置 Redis:https://kubernetes.io/zh-cn/docs/tutorials/configuration/configure-redis-using-configmap/
ConfigMap:https://kubernetes.io/zh-cn/docs/concepts/configuration/configmap/
其他文档:https://geek.csdn.net/658569e1dafaf23eeaee3157.html?#devmenu4

为什么使用ConfigMap

在Kubernetes 引入ConfigMap时的说明种提到是为了进行“动态配置管理”,上述图示很好地进行了说明,比如名为my-config的配置内容,在开发、测试和生产有不同的配置,通过ConfigMap即可对配置进行动态管理,根据需要进行关联,在实际的使用种更加灵活。

创建ConfigMap的方式

常见的创建方式有如下4种:
使用–from-literal选项在命令行中直接创建
使用–from-file选项指定配置文件创建
使用–from-file选项指定目录进行创建
使用-f选项指定标准的ConfigMap的yaml文件进行创建

查询ConfigMap的方法

查询缺省的default命名空间的ConfigMap信息

执行命令:kubectl get configmap | kubectl get cm

查询指定命令空间的ConfigMap信息

执行命令:kubectl get configmap -n 命名空间名称

查询所有命令空间的ConfigMap信息

执行命令:kubectl get configmap -A | kubectl get cm -A

看不到confimap里的DATA信息,可能是内容太长了,-o yaml

kubectl get configmap -o yaml

创建方式1: 使用from-literal选项

执行命令:kubectl create configmap ConfigMap名称 --from-literal=key1=value1 --from-literal=key2=value2 …

[root@host131 config]# kubectl create configmap user-configmap-literal --from-literal=user.name=liumiao --from-literal=user.id=1001
configmap/user-configmap-literal created
[root@host131 config]#

查询创建的ConfigMap信息

[root@host131 config]# kubectl get configmap
NAME                     DATA   AGE
user-configmap-literal   2      94s
[root@host131 config]# kubectl describe configmap user-configmap-literal
Name:         user-configmap-literal
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
user.id:
----
1001
user.name:
----
liumiao
Events:  <none>
[root@host131 config]#

创建方式2: 使用from-file选项(指定文件名)

执行命令:kubectl create configmap ConfigMap名称 --from-file=ConfigMap文件名

保存设定的ConfigMap文件名称为:

[root@host131 config]# ls
user.properties
[root@host131 config]# cat user.properties 
user.name=liumiao
user.id=1001
[root@host131 config]#

创建ConfigMap文件

[root@host131 config]# kubectl create configmap user-configmap-file --from-file=user.properties
configmap/user-configmap-file created
[root@host131 config]#

创建方式3: 使用from-file选项(指定目录名)

执行命令:kubectl create configmap ConfigMap名称 --from-file=ConfigMap文件所在目录名

此种方式与方式2的区别在于如果指定目录下可能会有多个文件

[root@host131 config]# ls
user.properties
[root@host131 config]# cat user.properties 
user.name=liumiao
user.id=1001
[root@host131 config]#
[root@host131 config]# kubectl create configmap user-configmap-dir --from-file=.
configmap/user-configmap-dir created
[root@host131 config]#

创建方式4: 使用-f选项

执行命令:kubectl create -f ConfigMap的yaml文件

此种方式的区别在于包括类型、名称以及内容都需要在yaml文件中进行说明,事前准备如下配置文件

[root@host131 config]# cat prometheus.configmap.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-configmap
data:
  prometheus.yml: |
    global:
      scrape_interval:     10s 
      evaluation_interval: 10s
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
        - targets: ['localhost:9090']
[root@host131 config]#

创建ConfigMap

[root@host131 config]# kubectl create -f prometheus.configmap.yml 
configmap/prometheus-configmap created
[root@host131 config]# 

结果确认

[root@host131 config]# kubectl get cm
NAME                     DATA   AGE
prometheus-configmap     1      8s
user-configmap-dir       1      6m21s
user-configmap-file      1      10m
user-configmap-literal   2      19m
[root@host131 config]# 
[root@host131 config]# kubectl describe cm prometheus-configmap
Name:         prometheus-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
prometheus.yml:
----
global:
  scrape_interval:     10s 
  evaluation_interval: 10s
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

Events:  <none>
[root@host131 config]# 

删除操作

执行命令:kubectl delete configmap ConfigMap名称

执行示例

[root@host131 config]# kubectl get cm
NAME                     DATA   AGE
prometheus-configmap     1      5m40s
user-configmap-dir       1      11m
user-configmap-file      1      15m
user-configmap-literal   2      24m
[root@host131 config]# kubectl delete configmap user-configmap-literal
configmap "user-configmap-literal" deleted
[root@host131 config]# kubectl get cm
NAME                   DATA   AGE
prometheus-configmap   1      6m10s
user-configmap-dir     1      12m
user-configmap-file    1      16m
[root@host131 config]# 

修改操作

执行命令:kubectl edit configmap ConfigMap名称

[root@host131 config]# kubectl edit configmap user-configmap-file
configmap/user-configmap-file edited
[root@host131 config]#

修改内容如下所示

apiVersion: v1
data:
  user.properties: |
    user.name=liumiaocn
    user.id=1003
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-03T06:18:06Z"
  name: user-configmap-file
  namespace: default
  resourceVersion: "36993"
  selfLink: /api/v1/namespaces/default/configmaps/user-configmap-file
  uid: fc26a0ff-594b-4b4d-bb86-69ee598de97f
  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值