在容器化应用中,每个环境都要独立的打一个镜像再给镜像一个特有的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
2)基于目录,将目录下的所有文件加载到configMap中
3)基于单个file
这里使用第3种:基于单个file的方法,其它方法也很简单,可自行百度,google
格式:
命令 ??????动作 ??模块 ?????????自定义名称 ??加载的文件
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 ??AGEnginx-config ??1 ?????5s
验证configMap中的nginx-config配置是否和先前vi的nginx.conf一样
[root@server yaml]# kubectl get configmaps/nginx-config -o yamlapiVersion: v1data: ?nginx.conf: |+ ??????????????????????????????????????????????##这一段就是内容,nginx.conf是该文件的键 ???server { ??????listen ??????8081; ??????server_name ?_; ??????root ????????/html; ??????location / { ??????} ???}kind: ConfigMapmetadata: ?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.yamlapiVersion: extensions/v1beta1kind: Deploymentmetadata: ?name: my-nginxspec: ?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: ???????????- key: nginx.conf ???????????????????????--使用nginx-config配置的nginx.conf键里的内容 ?????????????path: nginx.conf---apiVersion: v1kind: Servicemetadata: ?name: nginx-svcspec: ?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 createdservice/nginx-svc created[root@server yaml]# [root@server yaml]# [root@server yaml]# kubectl get pod NAME ???????????????????????READY ??STATUS ???RESTARTS ??AGEmy-nginx-55fd7587b7-7fscq ??1/1 ????Running ??0 ?????????9s[root@server yaml]# [root@server yaml]# kubectl get svc |grep nginxNAME ????????TYPE ???????CLUSTER-IP ?????EXTERNAL-IP ??PORT(S) ?????????AGEnginx-svc ???NodePort ???10.68.230.130 ??<none> ???????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 0lrwxrwxrwx 1 root root 17 Jan 31 09:31 nginx.conf -> ..data/nginx.conf ???????--可以看到这个配置文件是一个相对路径不是实体文件
上面已经验证了应用确实使用了configMap的配置
这样我们就可以把不同的环境配置文件都在k8s里建一份,在部署的时候指定对应环境的配置文件即可。
这就解决了不同环境,在部署应用时要重新打包镜像的问题。
kubernetes的配置中心configmap
原文地址:http://blog.51cto.com/passed/2348256