Kerbernetes使用ConfigMap资源配置非铭感信息
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.配置应用程序常用方法
配置应用程序是很常见的应用程序,常用的配置途径是使用配置文件或命令行选项;但容器化应用是基于镜像文件启动,其配置方式有别于此两种途径。
配置容器化应用程序常用的方法有以下几种:
(1)将设置好的配置文件硬编码进镜像中;
(2)基于环境变量:
Docker环境变量:Dockerfile中定义。
Kubernetes环境变量:资源配置文件中定义。
(3)使用Kubernetes的ConfigMap和Secret实现集中式配置(配置中心)。
二.创建ConfigMap实战案例
1>.创建configmap对象
[[email protected] ~]# kubectl get namespace NAME STATUS AGE default Active 5d11h ingress-nginx Active 41h kube-node-lease Active 5d11h kube-public Active 5d11h kube-system Active 5d11h myservice Active 46h testing Active 2d15h testing2 Active 2d5h yinzhengjie-eshop Active 33h yinzhengjie-ns Active 36h yinzhengjie-volume Active 24h [[email protected] ~]# [[email protected] ~]# kubectl create namespace yinzhengjie-config namespace/yinzhengjie-config created [[email protected] ~]# [[email protected] ~]# kubectl get namespace NAME STATUS AGE default Active 5d11h ingress-nginx Active 41h kube-node-lease Active 5d11h kube-public Active 5d11h kube-system Active 5d11h myservice Active 46h testing Active 2d15h testing2 Active 2d5h yinzhengjie-config Active 2s yinzhengjie-eshop Active 33h yinzhengjie-ns Active 36h yinzhengjie-volume Active 24h [[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# kubectl get namespace NAME STATUS AGE default Active 5d11h ingress-nginx Active 41h kube-node-lease Active 5d11h kube-public Active 5d11h kube-system Active 5d11h myservice Active 46h testing Active 2d15h testing2 Active 2d5h yinzhengjie-config Active 50s yinzhengjie-eshop Active 33h yinzhengjie-ns Active 36h yinzhengjie-volume Active 24h [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# kubectl create configmap filebeat-cfg -n yinzhengjie-config --from-literal=redis_hostname="redis.default.service.cluster.local" --from-literal=log_level="Info" configmap/filebeat-cfg created [[email protected] ~]# [[email protected] ~]# kubectl get configmap -n yinzhengjie-config NAME DATA AGE filebeat-cfg 2 28s [[email protected] ~]#
[[email protected] ~]# kubectl get configmap -n yinzhengjie-config NAME DATA AGE filebeat-cfg 2 28s [[email protected] ~]# [[email protected] ~]# kubectl get configmap -n yinzhengjie-config -o yaml apiVersion: v1 items: - apiVersion: v1 data: log_level: Info redis_hostname: redis.default.service.cluster.local kind: ConfigMap metadata: creationTimestamp: "2020-02-09T23:39:57Z" name: filebeat-cfg namespace: yinzhengjie-config resourceVersion: "501375" selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c kind: List metadata: resourceVersion: "" selfLink: "" [[email protected] ~]#
2>.
3>.
4>.
5>.
三.