2019独角兽企业重金招聘Python工程师标准>>>

k8s-configMap

生产中,在几乎所有的应用都会涉及到配置文件的变更如说在web的程序中,需要连接数据库,缓存甚至是队列等等。每一种应用都要定义其独立的各种配置,如果我们不能很好的管理这些配置文件,我们的工作就变得无比的繁琐。为此业内的一些大公司专门开发了自己的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了自己的一套方案,即ConfigMap。kubernetes通过ConfigMap来实现对容器中应用的配置管理。 

1.创建ConfigMap

创建ConfigMap的方式有两种,一种是通过yaml文件来创建,另一种是通过kubectl直接在命令行下创建。方式(1):命令行方式制定配置文件直接创建kubectl create configmap nginxconfig --from-file /server/yaml/nginx/nginx.conf #通过命令直接创建
kubectl get configmap nginxconfig -o yaml #查看文件方式(2):编写标准的yaml文件格式: kubectl create -f xxxxconfigmap.yamlcat nginx-configmap.yaml
#-----------------------------创建命名空间----------------------------
apiVersion: v1
kind: Namespace
metadata:name: mt-math                  #创建的命名空间名称为mt-math
---
#-----------------------------创建configmap文件-----------------------
#创建configmap有多种方式,可以通过命令如:
#kubectl create configmap nginxconfig --from-file /server/yaml/nginx/nginx.conf
#查看创建的yaml文件内容:kubectl get configmap nginxconfig -o yaml
#这里采用标准的yaml文件格式进行创建:更新时采用:kubectl  replace -f nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: nginxconfignamespace: mt-math
data:nginx.conf: |#########wyl530user nginx;worker_processes auto;error_log /etc/nginx/error.log;pid /run/nginx.pid;include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;}http {log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';server_tokens     off;access_log        /usr/share/nginx/html/access.log  main;sendfile            on;tcp_nopush          on;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 2048;include             /etc/nginx/mime.types;default_type        application/octet-stream;include /etc/nginx/conf.d/*.conf;server {listen       80 default_server;listen       [::]:80 default_server;server_name  _;root         /usr/share/nginx/html;include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}}

2.  创建pod yaml文件

cat deployment.yaml
#----------------------------创建pv---------------------------------
apiVersion: v1
kind: PersistentVolume
metadata:name: pv-nfs-pv01             #创建的pv名称可创建多个.namespace: mt-math            #属于的命名空间labels:pv: pv-nfs-01               #定义pv标签,后续通过pvc绑定特定的pv标签。通常如果不写标签则默认通过访问方式和storage大小进行批量绑定。(重要)
spec:capacity:storage: 1Gi                 #创建的pv-nfs-pv01大小为1GaccessModes:- ReadWriteManynfs:                           #创建的pv数据来源path: /NFS/pv01              #数据源目录server: 192.168.0.14         #数据源ip
#--------------------------------创建pvc--------------------------------
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: nfs-data-pvc              #创建的pvc名称namespace: mt-math              #属于的命名空间
spec:accessModes:- ReadWriteMany                       resources:requests:storage: 1Gi               #请求大小为1Gselector:                      #定义标签选择器,此时k8s会根据标签,storage,访问方式 进行匹配。三者同时满足才会绑定。如果不定义,则系统会根据storage的大小和访问方式进行匹配绑定.matchLabels:pv: pv-nfs-01              #定义请求标签为pv-nfs-pv01的pv且大小为1G
#--------------------------------创建pod------------------------------------
---
apiVersion: extensions/v1beta1   #接口版本
kind: Deployment                 #接口类型
metadata:                        #基础标签名称信息name: wyl-nginx                #创建的pod名称namespace: mt-math             #创建的pod属于mt-math命名空间
spec:                            #详细参数设置replicas: 3                    #副本数量selector:                      #标签选择器matchLabels:app: wyl-nginx             #正对标签为wyl-nginx的pod进行副本的创建strategy:rollingUpdate:               #由于replicas为3,则整个升级,pod个数在2-4个之间maxSurge: 1                #滚动升级时会先启动1个podmaxUnavailable: 1          #滚动升级时允许的最大Unavailable的pod个数template:                      #模板metadata:labels:app: wyl-nginx           #模板pod名称必填spec:                        #定义容器模板信息,该模板可以包含多个容器containers:- name:  nginximage: nginx:latestimagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 80volumeMounts:- name: nginx-nfsmountPath: /usr/share/nginx/html- name: nginx-pvcmountPath: /var/log/nginxsubPath: nginx.conf- name: nginx-etc       #挂载数据节点名称mountPath: /etc/nginx/nginx.conf #挂载此目录subPath: nginx.confvolumes:                    #设置挂载- name: nginx-nfs           #挂载的数据节点名称nfs:                      #挂载的服务类型server: 192.168.0.14    #服务Ippath: /NFS/wwwroot      #挂载数据目录- name: nginx-pvc           #挂载数据节点名称persistentVolumeClaim:    #服务类型claimName: nfs-data-pvc #数据源名称- name: nginx-etc           #挂载数据节点名称configMap:name: nginxconfig        #指定创建configMap的名称items:- key: nginx.conf       #key为文件名称path: nginx.conf      #文件路径内容
---
#-----------------------创建server-------------------------------------------
kind: Service
apiVersion: v1
metadata:name: wyl-nginx             namespace: mt-math             #属于的命名空间
spec:selector:app: wyl-nginx               #针对标签为wyl-nginx的标签进行负载type: NodePort                 #正对Node节点进行端口暴露ports:                       - protocol: TCP              #使用端口的协议port: 3017                 #供内网访问暴露的端口targetPort: 80             #目标pod的端口nodePort: 33333            #node节点暴露的端口
#简单说明-------------------------------------------------------------------
#1.创建了mt-math命名空间,删除命名空间默认删除该命名空间下的所有资源服务
#2.创建了pv,pv不属于任何命名空间
#3.创建了pvc,pvc属于mt-math命名空间
#4.创建了pod属于mt-math命名空间
#5.创建了server属于mt-math命名空间
#6.建议将pv单独写到一个文件中.否则在我们删除改命名空间下的所有服务后,利用该文件再次创建时会报pv已经创建.
#---------------------------------------------------------------------------

3.创建资源服务

kubectl create -f /server/yaml/nginx/nginx-configmap.yaml
kubectl create -f /server/yaml/nginx/deployment.yaml
kubectl exec -it wyl-nginx-d6f68d775-l4rkb --namespace=mt-math -- cat /etc/nginx/nginx.conf |head -1

转载于:https://my.oschina.net/wangyunlong/blog/3054633

k8s-configmap 挂载使用相关推荐

  1. K8S configmap挂载文件

    新建configmap: apiVersion: v1 kind: ConfigMap metadata:name: front-confnamespace: tsp data:baseUrl.js: ...

  2. 【K8S实战系列-nignx-2】k8s中configmap挂载配置nginx.conf

    [K8S实战系统-nignx-2]k8s中configmap挂载配置nginx.conf 1. ConfigMap是什么 ConfigMap作用是存储不加密的数据到etcd中,让Pod以变量或数据卷V ...

  3. K8S configmap详解:从文件创建、从文件夹创建及以volume、env环境变量的方式在pod中使用

    K8S configmap详解:从文件创建.从文件夹创建及以volume.env环境变量的方式在pod中使用 ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在 ...

  4. .NET Core 使用 K8S ConfigMap的正确姿势

    背景 ASP.NET Core默认的配置文件定义在 appsetings.json和 appsettings.{Environment}.json文件中.这里面有一个问题就是,在使用容器部署时,每次修 ...

  5. 使用 ConfigMap 挂载配置文件

    使用 ConfigMap 挂载配置文件 Intro 有一些敏感信息比如数据库连接字符串之类的出于安全考虑,这些敏感信息保存在了 AzureKeyVault 中,最近应用上了 k8s 部署,所以想把 A ...

  6. k8s ConfigMap使用示例:以volume或变量形式挂载到pod中

    以Volume形式挂载cm到pod redis.properties [root@m-1 redis]# cat redis.properties redis.host=127.0.0.1 redis ...

  7. k8s mysql 挂载_如何在 Kubernetes 环境中搭建 MySQL(四):使用 StorageClass 挂接 RBD...

    MySQL + Kubernetes 1. 简介 在系列文章的第三篇中,讲到了如何使用 PV 和 PVC 挂载 RBD 上建立好的块存储镜像,但这还是不足以满足 cloud native 环境下的需求 ...

  8. K8S日志挂载到本机

    1 简介 K8S部署后台服务后,在Pods内部生成运行日志,若不配置日志挂载, 当,Pods被删除后,日志则随Pods被删除, 为解决该问题,配置日志挂载,即把Pods中的日志通过NFS服务挂载到本机 ...

  9. k8s NFS挂载出现 mount.nfs: access denied by server while mounting 192.168.153.128:/nfs/data

    ### pod 一直处于pending状态 root@ubuntu:/opt/module/k8s# kubectl get pods -n nfs-clientNAME READY STATUS R ...

  10. 【技术解决方案】企业如何从SpringBoot应用平滑迁移到云原生K8s平台

    文章目录 在K8S上部署Spring Cloud Alibaba 在Kubernetes上部署Spring Cloud Kubernetes 在Kubernetes上部署Spring Boot应用 方 ...

最新文章

  1. qchart折现图_Qt Charts 动态实时折线图绘制
  2. STP/RSTP/MSTP协议简介
  3. php pdo参数化,php – 如何正确地使用PDO对象的参数化SELECT查询
  4. 4键电子手表说明书_数字S1系统7寸门口主机操作说明书
  5. java 数组大数乘法_java – 在数组中查找3个数字的最大乘积
  6. Android UI系列-----ScrollView和HorizontalScrollView
  7. c语言利用循环结构解决密码转换,C语言课件第六章循环结构.ppt
  8. Ubuntu 16.04-codeblocks 汉化
  9. AntDesign 表格设置背景色
  10. quilt 工具增加 patch 方法
  11. 缓存和数据库刷新的顺序 及阿里OCS介绍
  12. 【python量化】用时间卷积神经网络(TCN)进行股价预测
  13. 717 1比特与2比特字符
  14. python数据挖掘母亲和孩子身高预测_孩子身高预测公式 靠谱指数高达80分哦!
  15. 百词斩秋招java,成都百词斩2018web前端秋招笔试题
  16. c语言编程水果忍者,少儿创意编程scratch初级游戏之一水果忍者
  17. 武汉大学信管专业期末复习系列——《计算机网络》(谢希仁版)(网络层)
  18. springmvc对json数据的处理
  19. 小程序生成二维码分享朋友圈的功能
  20. java基础入门-04-【集合学生管理系统】

热门文章

  1. Java数据类型,Hibernate数据类型,标准sql数据类型之间的对应表
  2. Latex 中cite的使用
  3. ogg启动报错libnnz11.so: cannot open shared object file
  4. JAVA Metrics 度量工具使用介绍
  5. nodejs template
  6. Active Directory系列之五:Active Directory的主要还原
  7. 介绍一下ajax后最好的返回结果的方式#34;Json#34;
  8. Tomcat相关总结
  9. 全球最大的NFC 交易平台OpenSea严重漏洞可使黑客窃取钱包密币
  10. 微软“照片”应用Raw 格式图像编码器漏洞 (CVE-2021-24091)的技术分析