设为首页 收藏本站
查看: 729|回复: 0

[经验分享] Kubernetes外挂配置管理

[复制链接]
发表于 2018-1-4 11:54:11 | 显示全部楼层 |阅读模式
  其他容器编排调度工具会大谈特谈“轻应用”、“十二要素应用”,这样就势必会对企业级复杂应用做很大的改动。Kubernetes是为了解决“如何合理使用容器支撑企业级复杂应用”这个问题而诞生的,所以它的设计理念是要支持绝大多数应用的原生形态。例如,很多应用程序的配置需要通过配置文件,命令行参数和环境变量的组合配置来完成(“十二要素应用”等均要求去配置)。这些配置应该从image内容中解耦,以此来保持容器化应用程序的可移植性。ConfigMap API资源提供了将配置数据注入容器的方式,同时保证该机制对容器来说是透明的。ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。
  ConfigMap API资源存储键/值对配置数据,这些数据可以在pods里使用。ConfigMap跟Secrets类似,但是ConfigMap可以更方便的处理不包含敏感信息的字符串。示例如下:
  

kind: ConfigMap  
apiVersion: v1
  
metadata:
  creationTimestamp:
2016-02-18T19:14:38Z  name: example
-config  namespace: default
  
data:
  example.property.
1: hello  example.property.
2: world  example.property.
file: |-  property.
1=value-1  property.
2=value-2  property.
3=value-3  

  通过示例代码可以看到:ConfigMap可以包含细粒度的配置项,如:example.property.1;也可以包含粗粒度的配置文件,如:example.property.file。

1、创建ConfigMap

1.1 从文件夹创建
  

[iyunv@k8s-master propertirs]# cat /home/yaml/propertirs/game.properties  
enemies
=aliens  
lives
=3  
enemies.cheat
=true  
enemies.cheat.level
=noGoodRotten  
secret.code.passphrase
=UUDDLRLRBABAS  
secret.code.allowed
=true  
secret.code.lives
=30  
[iyunv@k8s
-master propertirs]# cat /home/yaml/propertirs/ui.properties  
color.good
=purple  
color.bad
=yellow  
allow.textmode
=true  
how.
nice.to.look=fairlyNice  
[iyunv@k8s
-master propertirs]# kubectl create configmap game-config --from-file=/home/yaml/propertirs/  
configmap
"game-config" created  
[iyunv@k8s
-master propertirs]# kubectl describe configmaps game-config  
#该方法只能得到ConfigMap的Key和size
  
Name:        game
-config  
Namespace:    default
  
Labels:
<none>  
Annotations:
<none>  

  
Data
  

====  
ui.properties:
83 bytes  
game.properties:
158 bytes  
#若想得到详细信息,可通过以下命令:
  
[iyunv@k8s
-master propertirs]# kubectl get configmaps game-config -o yaml  
apiVersion: v1
  
data:
  game.properties:
|  enemies
=aliens  lives
=3  enemies.cheat
=true  enemies.cheat.level
=noGoodRotten  secret.code.passphrase
=UUDDLRLRBABAS  secret.code.allowed
=true  secret.code.lives
=30  ui.properties:
|  color.good
=purple  color.bad
=yellow  allow.textmode
=true  how.
nice.to.look=fairlyNice  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:22:34Z  name: game
-config  namespace: default
  resourceVersion:
"3002770"  selfLink:
/api/v1/namespaces/default/configmaps/game-config  uid: a04f90f0
-0de5-11e7-b3d5-fa163ebba51b  


1.2 从文件创建
  

[iyunv@k8s-master propertirs]# kubectl create configmap game-config-2 --from-file=/home/yaml/propertirs/game.properties --from-file=/home/yaml/propertirs/ui.properties  
configmap
"game-config-2" created  
[iyunv@k8s
-master propertirs]# kubectl get configmaps game-config-2 -o yaml  
apiVersion: v1
  
data:
  game.properties:
|  enemies
=aliens  lives
=3  enemies.cheat
=true  enemies.cheat.level
=noGoodRotten  secret.code.passphrase
=UUDDLRLRBABAS  secret.code.allowed
=true  secret.code.lives
=30  ui.properties:
|  color.good
=purple  color.bad
=yellow  allow.textmode
=true  how.
nice.to.look=fairlyNice  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:30:15Z  name: game
-config-2  namespace: default
  resourceVersion:
"3003415"  selfLink:
/api/v1/namespaces/default/configmaps/game-config-2  uid: b2e4dfab
-0de6-11e7-b3d5-fa163ebba51b  


1.3  指定data中的key
  

[iyunv@k8s-master propertirs]# kubectl create configmap game-config-3 --from-file=game-special-key=/home/yaml/propertirs/game.properties  
configmap
"game-config-3" created  
[iyunv@k8s
-master propertirs]# kubectl get configmaps game-config-3 -o yaml  
apiVersion: v1
  
data:
  game
-special-key: |  enemies
=aliens  lives
=3  enemies.cheat
=true  enemies.cheat.level
=noGoodRotten  secret.code.passphrase
=UUDDLRLRBABAS  secret.code.allowed
=true  secret.code.lives
=30  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:33:23Z  name: game
-config-3  namespace: default
  resourceVersion:
"3003678"  selfLink:
/api/v1/namespaces/default/configmaps/game-config-3  uid: 2345dad3
-0de7-11e7-b3d5-fa163ebba51b  


1.4  指定具体的值
  

[iyunv@k8s-master propertirs]# kubectl create configmap game-config-4 --from-literal=special.user=zhenyu --from-literal=special.passwd=yaodidiao  
configmap
"game-config-4" created  
[iyunv@k8s
-master propertirs]# kubectl get configmaps game-config-4 -o yaml  
apiVersion: v1
  
data:
  special.
passwd: yaodidiao  special.user: zhenyu
  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:36:12Z  name: game
-config-4  namespace: default
  resourceVersion:
"3003915"  selfLink:
/api/v1/namespaces/default/configmaps/game-config-4  uid: 8802f6d2
-0de7-11e7-b3d5-fa163ebba51b  


2、使用ConfigMap

2.1 环境变量或参数
  创建一个Pod,并将一个已经创建好的ConfigMap作为环境变量,注入到Pod中。
  

[iyunv@k8s-master propertirs]# kubectl get configmaps game-config-4 -o yaml  
apiVersion: v1
  
data:
  special.
passwd: yaodidiao  special.user: zhenyu
  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:36:12Z  name: game
-config-4  namespace: default
  resourceVersion:
"3003915"  selfLink:
/api/v1/namespaces/default/configmaps/game-config-4  uid: 8802f6d2
-0de7-11e7-b3d5-fa163ebba51b  
[iyunv@k8s
-master propertirs]# cat testEnv.yaml  
apiVersion: v1
  
kind: Pod
  
metadata:
  labels:
  name: testenv
  role: master
  name: testenv
  
spec:
  containers:
- name: testenv  image: busybox
  imagePullPolicy: IfNotPresent
env:- name: SPECIAL_USER  valueFrom:
  configMapKeyRef:
  name: game
-config-4  key: special.user
  command:
- sleep  - "360000"
  
[iyunv@k8s-master propertirs]# kubectl create -f testEnv.yaml
  
pod "testenv" created
  
[iyunv@k8s-master propertirs]# kubectl exec -ti testenv sh
  
/ # echo $SPECIAL_USER
  
zhenyu
  
/ #
  


2.2 挂载文件数据卷
  

[iyunv@k8s-master propertirs]# kubectl get configmaps game-config -o yaml  
apiVersion: v1
  
data:
  game.properties:
|  enemies
=aliens  lives
=3  enemies.cheat
=true  enemies.cheat.level
=noGoodRotten  secret.code.passphrase
=UUDDLRLRBABAS  secret.code.allowed
=true  secret.code.lives
=30  ui.properties:
|  color.good
=purple  color.bad
=yellow  allow.textmode
=true  how.
nice.to.look=fairlyNice  
kind: ConfigMap
  
metadata:
  creationTimestamp:
2017-03-21T03:22:34Z  name: game
-config  namespace: default
  resourceVersion:
"3002770"  selfLink:
/api/v1/namespaces/default/configmaps/game-config  uid: a04f90f0
-0de5-11e7-b3d5-fa163ebba51b  
[iyunv@k8s
-master propertirs]# cat testVolume.yaml  
apiVersion: v1
  
kind: Pod
  
metadata:
  labels:
  name: testvolume
  role: master
  name: testvolume
  
spec:
  containers:
- name: testvolume  image: busybox
  imagePullPolicy: IfNotPresent
  volumeMounts:
- name: config-volume  mountPath:
/etc/config  command:
- sleep  - "360000"
  volumes:
  - name: config-volume
  configMap:
  name: game-config
  
[iyunv@k8s-master propertirs]# kubectl create -f testVolume.yaml
  
pod "testvolume" created
  
[iyunv@k8s-master propertirs]# kubectl exec -ti testvolume sh
  
/ # cd /etc/config/
  
/etc/config # ls
  
game.properties  ui.properties
  
/etc/config # cat game.properties
  
enemies=aliens
  
lives=3
  
enemies.cheat=true
  
enemies.cheat.level=noGoodRotten
  
secret.code.passphrase=UUDDLRLRBABAS
  
secret.code.allowed=true
  
secret.code.lives=30
  
/etc/config # cat ui.properties
  
color.good=purple
  
color.bad=yellow
  
allow.textmode=true
  
how.nice.to.look=fairlyNice
  
/etc/config #
  


2.3 挂载信息数据卷
  

[iyunv@k8s-master propertirs]# cat testVolume.yaml  
apiVersion: v1
  
kind: Pod
  
metadata:
  labels:
  name: testvolume
  role: master
  name: testvolume
  
spec:
  containers:
- name: testvolume  image: busybox
  imagePullPolicy: IfNotPresent
  volumeMounts:
- name: config-volume  mountPath:
/etc/config  command:
- sleep  - "360000"
  volumes:
  - name: config-volume
  configMap:
  name: game-config-4
  
[iyunv@k8s-master propertirs]# kubectl get  configmaps game-config-4 -o yaml
  
apiVersion: v1
  
data:
  special.passwd: yaodidiao
  special.user: zhenyuyaodidiao
  
kind: ConfigMap
  
metadata:
  creationTimestamp: 2017-03-21T06:29:29Z
  name: game-config-4
  namespace: default
  resourceVersion: "3018779"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: bd086dca-0dff-11e7-b3d5-fa163ebba51b
  
[iyunv@k8s-master propertirs]# kubectl create -f testVolume.yaml
  
pod "testvolume" created
  
[iyunv@k8s-master propertirs]# kubectl exec -ti testvolume sh
  
/ # cd /etc/config/
  
/etc/config # ls
  
special.passwd  special.user
  
/etc/config # cat special.user
  
zhenyuyaodidiao/etc/config #
  
/etc/config # exit
  


2.4 热更新
  当ConfigMap以数据卷的形式挂载进Pod的时,这时更新ConfigMap(或删掉重建ConfigMap),Pod内挂载的配置信息会热更新。这时可以增加一些监测配置文件变更的脚本,然后reload对应服务。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-431478-1-1.html 上篇帖子: Kubernetes集群搭建过程中遇到的问题 下篇帖子: [系统集成] 基于Kubernetes 部署 jenkins 并动态分配资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表