如何理解kubernetes的配置中心configmap
admin
2023-04-08 15:43:16
0次

在企业中,一般都有2-4种环境,开发,测试,交付,生产这四种。这几种环境的配置也有所变化,我们在部署的时候通常不能一个配置文件部署四个环境。不通用。特别是容器化环境。

在容器化应用中,每个环境都要独立的打一个镜像再给镜像一个特有的tag,这很麻烦,这就要用到k8s原生的配置中心configMap就是用解决这个问题的。下面看示例。

使用configMap部署应用。
这里使用nginx来做示例,简单粗暴。
默认的nginx数据目录是在:/usr/share/nginx/html  目录,下面我写一个配置指定端口和数据目录

1、先将配置文件加载到configMap中
编写nginx配置

[root@server yaml]# vim nginx.conf 
server {
   listen       8081;         ##端口为8081
   server_name  _;
   root         /html;         ##改数据目录为/html

   location / {
   }

}

将上面写的nginx.conf加载到configMap中
创建configMap有三个方法
1)单个key
命令格式:
例:kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
查看:kubectl get configmaps special-config -o yaml

2)基于目录,将目录下的所有文件加载到configMap中
命令格式:
例:kubectl create configmap  tomcat-conf  --from-file=configmap/conf
查看:kubectl get configmaps tomcat-conf -o yaml

3)基于单个file
命令格式:
例:kubectl create configmap game-conf  --from-file=configmap/conf/game.properties
查看:kubectl get configmaps tomcat-conf -o yaml

注意事项:
使用configmap的pod必须要和configmap配置在同一个namespaces,否则识别不了。在创建configmap时在后面加上 -n namespaces名称
例:kubectl create configmap  tomcat-conf  --from-file=configmap/conf  -n tomcat

这里使用第3种:基于单个file的方法,其它方法也很简单。
格式:
命令       动作   模块          自定义名称   加载的文件
kubectl create configmap nginx-config  --from-file=./nginx.conf

[root@server yaml]# kubectl create configmap nginx-config --from-file=./nginx.conf 
configmap/nginx-config created                            ##提示创建成功
[root@server yaml]# 
[root@server yaml]# kubectl get configmaps       ##查看创建的nginx-config配置是否成功
NAME           DATA   AGE
nginx-config   1      5s

验证configMap中的nginx-config配置是否和先前vi的nginx.conf一样

[root@server yaml]# kubectl get configmaps/nginx-config -o yaml
apiVersion: v1
data:
  nginx.conf: |+                      ##这一段就是内容,nginx.conf是该文件的键
    server {
       listen       8081;
       server_name  _;
       root         /html;

       location / {
       }

    }

kind: ConfigMap
metadata:
  creationTimestamp: "2019-01-31T09:20:08Z"
  name: nginx-config
  namespace: default
  resourceVersion: "416047"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-config
  uid: 67199b66-2539-11e9-821c-000c2963b9a7
[root@server yaml]#

可以看到data:下面的nginx.conf里面的内容和我们上面vi的内容一致
接下来在部署应用的时候使用该配置

编写部署应用的yaml文件

[root@server yaml]# vim  nginx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.9
        ports:
        - containerPort: 8081
        volumeMounts:                         --就是这一段使用configMap配置
        - mountPath: /etc/nginx/conf.d  --将配置文件挂载到哪里
          name: config
        - mountPath: /html                     --指定数据目录
          name: data
      volumes:
        - name: data                              --指定数据目录创建
          emptyDir: {}
        - name: config                           --指定config使用configMap
          configMap:
            name: nginx-config                --指定使用configMap中的nginx-config配置
            items:                                     --注:也可不指定items,那默认是nginx-config里的所有值都挂载
            - key: nginx.conf                    --使用nginx-config配置的nginx.conf键里的内容
              path: nginx.conf

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: NodePort
  selector:
    app: my-nginx
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 28081

部署应用

[root@server yaml]# kubectl create -f nginx.yaml 
deployment.extensions/my-nginx created
service/nginx-svc created
[root@server yaml]# 
[root@server yaml]# 
[root@server yaml]# kubectl get pod 
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-55fd7587b7-7fscq   1/1     Running   0          9s
[root@server yaml]# 
[root@server yaml]# kubectl get svc |grep nginx
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
nginx-svc    NodePort    10.68.230.130           8081:28081/TCP   15s

部署成功,pod已经成功运行,服务发现已经做好了28081外部访问入口

2、验证nginx应用是否使用了configMap中的nginx-config
登陆部署的应用pod里去查看一下

[root@server yaml]# kubectl exec -it my-nginx-55fd7587b7-7fscq bash     --登陆容器
root@my-nginx-55fd7587b7-7fscq:/# 
root@my-nginx-55fd7587b7-7fscq:/# cat /etc/nginx/conf.d/nginx.conf        --查看配置确实是上面vi的内容
server {
   listen       8081;
   server_name  _;
   root         /html;

   location / {
   }

}

root@my-nginx-55fd7587b7-7fscq:/# 
root@my-nginx-55fd7587b7-7fscq:/# ls -d /html/               --数据目录也已经创建好了
/html/
root@my-nginx-55fd7587b7-7fscq:/#
root@my-nginx-55fd7587b7-7fscq:/# ls -l /etc/nginx/conf.d/
total 0
lrwxrwxrwx 1 root root 17 Jan 31 09:31 nginx.conf -> ..data/nginx.conf 
  --可以看到这个配置文件是一个相对路径不是实体文件

上面已经验证了应用确实使用了configMap的配置

这样我们就可以把不同的环境配置文件都在k8s里建一份,在部署的时候指定对应环境的配置文件即可。
这就解决了不同环境,在部署应用时要重新打包镜像的问题。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...