参考:https://www.cnblogs.com/breezey/p/6582082.html

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

我们知道,在几乎所有的应用开发中,都会涉及到配置文件的变更,比如说在web的程序中,需要连接数据库,缓存甚至是队列等等。而我们的一个应用程序从写第一行代码开始,要经历开发环境、测试环境、预发布环境只到最终的线上环境。而每一个环境都要定义其独立的各种配置。如果我们不能很好的管理这些配置文件,你的运维工作将顿时变的无比的繁琐。为此业内的一些大公司专门开发了自己的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了自己的一套方案,即ConfigMap。kubernetes通过ConfigMap来实现对容器中应用的配置管理。

1.创建configmap

通过yaml创建

我们先来看第一种,在yaml文件中,配置文件以key-value键值对的形式保存,当然也可以直接放一个完整的配置文件,在下面的示例中,cache_hst、cache_port、cache_prefix即是key-value键值对,而app.properties和my.cnf都是配置文件:

[root@k8s-master k8s-objs]# pwd
/root/k8s-objs
[root@k8s-master k8s-objs]# cat configmap-test1.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-cfg
  namespace: default
data:
  cache_host: mysql-k8s
  cache_port: "33006"
  cache_prefix: k8s
  my.cnf: |
    [mysqld]
    log-bin = mysql-bin
  app.properties: |
    property.1 = value-1
    property.2 = value-2
    property.3 = value-3
[root@k8s-master k8s-objs]# 
[root@k8s-master k8s-objs]# kubectl create -f configmap-test1.yaml 
configmap/test-cfg created


[root@k8s-master k8s-objs]# kubectl get configmaps
NAME       DATA   AGE
test-cfg   5      85s
[root@k8s-master k8s-objs]# kubectl describe pod test-cfg 
Error from server (NotFound): pods "test-cfg" not found
[root@k8s-master k8s-objs]# kubectl describe configmap test-cfg    
Name:         test-cfg
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
app.properties:
----
property.1 = value-1
property.2 = value-2
property.3 = value-3

cache_host:
----
mysql-k8s
cache_port:
----
33006
cache_prefix:
----
k8s
my.cnf:
----
[mysqld]
log-bin = mysql-bin

Events:  <none>
[root@k8s-master k8s-objs]# 

通过命令创建configmap

kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'

##直接将文件创建为configmap

##kubectl create configmap test-config --from-file=./configs ##将该目录下的所有配置文件创建为configmap
##kubectl create configmap test-config2 --from-file=./configs/db.conf --from-file=./configs/cache.conf

 

[root@k8s-master k8s-objs]# kubectl create configmap test-config3 --from-literal=db.host=10.5.10.116 --from-literal=db.port='3306'
configmap/test-config3 created
[root@k8s-master k8s-objs]# kubectl get configmaps              
NAME           DATA   AGE
test-cfg       5      6m1s
test-config3   2      9s
[root@k8s-master k8s-objs]# kubectl describe configmap test-cfg3
Error from server (NotFound): configmaps "test-cfg3" not found
[root@k8s-master k8s-objs]# kubectl describe configmap test-config3
Name:         test-config3
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db.host:
----
10.5.10.116
db.port:
----
3306
Events:  <none>
[root@k8s-master k8s-objs]# 

 查看属性

[root@k8s-master k8s-objs]# kubectl get configmaps -o yaml         
apiVersion: v1
items:
- apiVersion: v1
  data:
    app.properties: |
      property.1 = value-1
      property.2 = value-2
      property.3 = value-3
    cache_host: mysql-k8s
    cache_port: "33006"
    cache_prefix: k8s
    my.cnf: |
      [mysqld]
      log-bin = mysql-bin
  kind: ConfigMap
  metadata:
    creationTimestamp: "2019-04-11T08:25:13Z"
    name: test-cfg
    namespace: default
    resourceVersion: "976815"
    selfLink: /api/v1/namespaces/default/configmaps/test-cfg
    uid: 544cc424-5c33-11e9-a5c3-000c291ae345
- apiVersion: v1
  data:
    db.host: 10.5.10.116
    db.port: "3306"
  kind: ConfigMap
  metadata:
    creationTimestamp: "2019-04-11T08:31:05Z"
    name: test-config3
    namespace: default
    resourceVersion: "977288"
    selfLink: /api/v1/namespaces/default/configmaps/test-config3
    uid: 261ab5e2-5c34-11e9-a5c3-000c291ae345
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄