当前位置: 首页 > ds >正文

编排之神-Kubernetes存储专题--ConfigMap演练

8.Kubernetes中的存储

8.1 ConfigMap

8.1.1 ConfigMap的功能
  • configMap用于保存配置数据,以键值对形式存储。
  • configMap 资源提供了向 Pod 注入配置数据的方法。
  • 镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。
  • etcd限制了文件大小不能超过1M
8.1.2 ConfigMap的使用场景
  • 填充环境变量的值
  • 设置容器内的命令行参数
  • 填充卷的配置文件
8.1.3 ConfigMap创建方式
8.1.3.1 字面值创建
[root@k8s-master mnt]# kubectl create cm ceshi --from-literal name1=dhj
[root@k8s-master mnt]# kubectl describe cm ceshi

在这里插入图片描述

8.1.3.2 通过文件创建
[root@k8s-master ~]# vim /etc/resolv.conf			# 此文件是本裸金属主机DNS地址指向
[root@k8s-master ~]# kubectl create cm lee2-config --from-file /etc/resolv.conf
[root@k8s-master ~]# kubectl describe cm lee2-config
image-20250816111309751
8.1.3.3 通过目录创建
[root@k8s-master mnt]# mkdir ceshi/
[root@k8s-master mnt]# cp /etc/resolv.conf ceshi/
[root@k8s-master mnt]# kubectl create cm test  --from-file ceshi/
[root@k8s-master mnt]# kubectl describe cm test
image-20250816111727610
8.1.3.4 通过yaml文件创建
[root@k8s-master mnt]# kubectl create cm testyaml --from-literal db_host=172.25.254.100 --from-literal db_port=3306 --dry-run=client -o yaml > testyaml.yml
[root@k8s-master mnt]# vim testyaml.yml
apiVersion: v1
data:db_host: 172.25.254.100db_port: "3306"
kind: ConfigMap
metadata:creationTimestamp: nullname: testyaml[root@k8s-master mnt]# kubectl apply -f testyaml.yml
[root@k8s-master mnt]# kubectl describe cm testyaml
image-20250816115841573
8.1.4 ConfigMap的使用方式
  • 通过环境变量的方式直接传递给pod
  • 通过pod的命令行运行方式
  • 作为volume的方式挂载到pod内
8.1.4.1 使用ConfigMap填充环境变量
[root@k8s-master mnt]# kubectl run testpod --image busyboxplus --dry-run=client -o yaml > test.yml[root@k8s-master ~]# vim cmcfg1.yml
[root@k8s-master mnt]# kubectl apply -f cmcfg1.yml[root@k8s-master ~]# kubectl create cm cm-ceshi --from-file cmcfg1.yml --dry-run=client[root@k8s-master ~]# kubectl describe cm cm-ceshi
# 将cm中的内容映射为指定变量
[root@k8s-master ~]# vim testyaml1.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testyamlname: testyaml
spec:containers:- image: busyboxplus:latestname: testyamlcommand:- /bin/sh- -c- envenv:- name: key1valueFrom:configMapKeyRef:name: testyamlkey: db_host- name: key2valueFrom:configMapKeyRef:name: testyamlkey: db_portrestartPolicy: Never[root@k8s-master ~]# kubectl apply -f testyaml1.yml[root@k8s-master mnt]# kubectl create cm testpod --from-file testyaml1.yml[root@k8s-master mnt]# kubectl logs pod/testyaml | grep key
key1=172.25.254.100
key2=3306
image-20250816145425112
# 把cm中的值直接映射为变量
[root@k8s-master ~]# vim testpod2.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- envenvFrom:- configMapRef:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master mnt]# kubectl logs pod/testpod | grep db
db_port=3306
db_host=172.25.254.100
image-20250816145449347
# 在pod命令行中使用变量
[root@k8s-master ~]# vim testpod3.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- echo ${db_host} ${db_port}		# 变量调用envFrom:- configMapRef:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master ~]# kubectl logs pods/testpod
172.25.254.100 3306 
image-20250816145552253
8.1.4.2 通过数据卷使用ConfigMap
[root@k8s-master ~]# vim testpod4.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- cat /config/db_hostvolumeMounts:					# 调用卷策略- name: config-volume			# 卷名称mountPath: /configvolumes:							# 声明卷的配置- name: config-volume				# 卷名称configMap:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master ~]# kubectl logs testpod
172.25.254.100
8.1.4.3 利用ConfigMap填充pod的配置文件
# 建立配置文件模板
[root@k8s-master ~]# vim nginx.conf
server {listen 8000;server_name _;root /usr/share/nginx/html;index index.html;
}# 利用模板生成cm
root@k8s-master ~]# kubectl create cm nginx-conf --from-file nginx.conf
configmap/nginx-conf created
[root@k8s-master ~]# kubectl describe cm nginx-conf
Name:         nginx-conf
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
nginx.conf:
----
server {listen 8000;server_name _;root /usr/share/nginx/html;index index.html;
}BinaryData
====Events:  <none># 建立nginx控制器文件
[root@k8s-master ~]# kubectl create deployment nginx --image nginx:latest --replicas 1 --dry-run=client -o yaml > nginx.yml# 设定nginx.yml中的卷
[root@k8s-master ~]# vim nginx.yml
[root@k8s-master ~]# cat nginx.
cat: nginx.: 没有那个文件或目录
[root@k8s-master ~]# cat nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: nginxname: nginx
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginx:latestname: nginxvolumeMounts:- name: config-volumemountPath: /etc/nginx/conf.dvolumes:- name: config-volumeconfigMap:name: nginx-conf# 测试
[root@k8s-master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE        NOMINATED NODE   READINESS GATES
nginx-8487c65cfc-cz5hd   1/1     Running   0          3m7s   10.244.2.38   k8s-node2   <none>           <none>
[root@k8s-master ~]# curl 10.244.2.38:8000
8.1.4.4 通过热更新cm修改配置
[root@k8s-master ~]# kubectl edit cm nginx-conf
apiVersion: v1
data:nginx.conf: |server {listen 8080;						# 端口改为8080server_name _;root /usr/share/nginx/html;index index.html;}
kind: ConfigMap
metadata:creationTimestamp: "2024-09-07T02:49:20Z"name: nginx-confnamespace: defaultresourceVersion: "153055"uid: 20bee584-2dab-4bd5-9bcb-78318404fa7a# 查看配置文件
[root@k8s-master ~]# kubectl exec pods/nginx-8487c65cfc-cz5hd -- cat /etc/nginx/conf.d/nginx.conf
server {listen 8080;server_name _;root /usr/share/nginx/html;index index.html;
}

配置文件修改后不会生效,需要删除pod后控制器会重建pod,这时就生效了

[root@k8s-master ~]# kubectl delete pods nginx-8487c65cfc-cz5hd
pod "nginx-8487c65cfc-cz5hd" deleted[root@k8s-master ~]# curl  10.244.2.41:8080
http://www.xdnf.cn/news/18251.html

相关文章:

  • 网络编程3(网络层,数据链路层)
  • linux下timerfd和posix timer为什么存在较大的抖动?
  • 从零开始:SpringBoot与KingbaseES的完美融合实践
  • JavaScript性能优化实战(三):DOM操作性能优化
  • Ansible 管理变量和事实
  • 【撸靶笔记】第二关:GET -Error based -Intiger based
  • 【LeetCode】单链表经典算法:移除元素,反转链表,约瑟夫环问题,找中间节点,分割链表
  • 计算机网络 TCP三次握手、四次挥手超详细流程【报文交换、状态变化】
  • nn.Module模块介绍
  • USB 2.0声卡
  • 考研复习-操作系统-第一章-计算机系统概述
  • k8s-单主机Master集群部署+单个pod部署lnmp论坛服务(小白的“升级打怪”成长之路)
  • 什么是GD库?PHP中7大类64个GD库函数用法详解
  • 【撸靶笔记】第五关:GET - Double Injection - Single Quotes - String
  • Qt——主窗口 mainWindow
  • GaussDB常用术语缩写及释义
  • 【Golang】:错误处理
  • AI Search进化论:从RAG到DeepSearch的智能体演变全过程
  • 第12章《学以致用》—PowerShell 自学闭环与实战笔记
  • 第七十七章:多模态推理与生成——开启AI“从无到有”的时代!
  • 计算机程序编程软件开发设计之node..js语言开发的基于Vue框架的选课管理系统的设计与实现、基于express框架的在线选课系统的设计与实现
  • Jenkins - CICD 注入环境变量避免明文密码暴露
  • Python中f - 字符串(f-string)
  • Hadoop入门
  • 前端基础知识版本控制系列 - 05( Git 中 HEAD、工作树和索引之间的区别)
  • 图论水题4
  • 写作路上的迷茫与突破
  • java_spring boot 中使用 log4j2 及 自定义layout设置示例
  • NestJS 手动集成TypeORM
  • 关于第一次接触Linux TCP/IP网络相关项目