Kubernetes实战篇之配置与存储

配置管理

ConfigMap

创建ConfigMap

使用 kubectl create configmap -h 查看示例,构建 configmap 对象

bash 复制代码
$ kubectl create cm -h
##################### OUTPUT #############################
...

Examples:
  # Create a new config map named my-config based on folder bar
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new config map named my-config with specified keys instead of file basenames on disk
  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

  # Create a new config map named my-config with key1=config1 and key2=config2
  kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

  # Create a new config map named my-config from the key=value pairs in the file
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new config map named my-config from an env file
  kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
...
文件夹方式创建
bash 复制代码
# cm/db.properties
username=root
password=admin

# cm/redis.properties
host=127.0.0.1
port=6379

# 创建cm
$ kubectl create configmap db-config --from-file=cm

# 查看cm列表
$ kubectl get cm
NAME               DATA   AGE
db-config          2      4m17s
kube-root-ca.crt   1      12d

# 查看db-config cm信息
$ kubectl describe cm db-config
...
Data
====
db.properties:
----
username=root
password=admin


redis.properties:
----
host=127.0.0.1
port=6379
...

文件方式创建
bash 复制代码
# application.yaml
spring:
  application:
    name: test-app
server:
  port: 8080
 
# 创建cm
$ kubectl create configmap spring-config --from-file=application.yaml

# 查看cm列表
$ kubectl get cm
NAME               DATA   AGE
db-config          2      10m
kube-root-ca.crt   1      12d
spring-config      1      6s

# 查看 spring-config cm 
$ kubectl describe cm spring-config
...
Data
====
application.yaml:
----
spring:
  application:
    name: test-app
server:
  port: 8080
...

使用ConfigMap

需求:将cm挂载到容器

资源清单

yaml 复制代码
# test-cm.yaml
apiVersion: apps/v1 # deployment api版本
kind: Deployment # 资源类型为 deployment
metadata: # 元信息
  labels: # 标签
    app: test-cm
  name: test-cm # deployment名称
  namespace: default # 所在的命名空间
spec:
  replicas: 1 # 期望副本数
  revisionHistoryLimit: 10 # 进行滚动更新后保留的历史版本数
  selector: # 选择器,用于找到匹配的RS
    matchLabels: # 按照标签匹配
      app: test-cm
  strategy:
    type: RollingUpdate # 更新类型,滚动更新
  template: # pod模板
    metadata: # pod的元信息
      labels: # 标签
        app: test-cm
    spec: # pod的期望信息
      containers: # pod容器
      - image: alpine # 镜像
        command: ["/bin/sh", "-c", "env;sleep 3600"]
        imagePullPolicy: IfNotPresent # 拉取策略
        name: test-cm # 容器名称
        volumeMounts:
        - name: application-config # 加载的数据卷的名字,需跟 volumes.name 对应上
          mountPath: "/usr/local/mysql/conf"  # 想要将数据卷中的文件加载到哪个目录下
          readOnly: true  # 是否只读
      volumes:
        - name: application-config  # 数据卷名字
          configMap:
            name: spring-config
            items:  # 对 configMap中的key进行映射,不指定则将所有key转换为一个同名文件
            - key: "application.yaml" # configMap 中的key
              path: "application.yaml"  # 将该key的值转换为文件
      restartPolicy: Always # 重启策略
      terminationGracePeriodSeconds: 30 # 删除操作最大宽限时长
bash 复制代码
# 创建挂载了数据卷的deploy
$ kubectl create -f test-cm.yaml

# 查看文件挂载情况
$ kubectl exec -it test-cm-7c945f774c-lzjh2 -- sh
/ # cd /usr/local/mysql/conf/
/usr/local/mysql/conf # more application.yaml
################# OUTPUT ########################
spring:
  application:
    name: test-app
server:
  port: 8080

注意:用 configMap 挂载会将容器内 整个目录全都覆盖掉 ,需要用 subPath 解决


Secret

configMap 类似,采用的是 Base64 加密,不安全。


持久化存储

Volumes

将节点上的文件或目录挂载到 Pod 上,此时该目录会变成持久化存储目录,即使 Pod 被删除后重启,也可以重新加载到该目录,该目录下的文件不会丢失

HostPath

yaml 复制代码
apiVersion: v1
kind: Pod
metadata: 
  name: test-volume-pd
spec:
  containers: 
  - image: nginx
    name: nginx-volume
    volumeMounts:
    - mountPath: /test-pd	# 挂载到容器的哪个目录
      name: test-volume	# 挂载哪个volume
  volumes:
  - name: test-volume
   hostPath:	# 与主机共享目录,加载主机中的指定目录到容器中
     path: /data		# 节点中的目录
     type: DirectoryOrCreate	# 检查类型,在挂载前对挂载目录做什么检查操作,有多种选项,默认为空字符串,不做任何检查
相关推荐
阿里云云原生13 分钟前
告别手工配置:利用 Migration Skill 实现 Nginx Ingress 注解的智能分析与自动转换
云原生
2601_9622184728 分钟前
万象生鲜系统冷链物联网接入技术实现生鲜企业温控管理数字化
大数据·运维·微服务·云原生·架构
阿里云云原生28 分钟前
Event-Driven AI 架构解析:从 Confluent Intelligence 三大支柱到阿里云实践
云原生
阿里云云原生1 小时前
从手工回测到全自动化:AgentLoop 在 Agent Engineering 中的落地与实践总结
云原生·agent
报错小能手2 小时前
Kubernetes入门实战课 1
云原生·容器·kubernetes
CJY6214 小时前
Kubernetes 存储管理
云原生
缘友一世4 小时前
GLM-5.3-NVFP4 部署实战系列[二]Docker 镜像选择与补丁镜像构建:纯 Python overlay,不重编一行 CUDA
python·docker·容器·vllm
harmony&4 小时前
Kubernetes 实战:认证授权、集群 Dashboard 与动态卷供应全解析
云原生·容器·kubernetes
java_logo5 小时前
Docker 部署 MinIO:轻松搭建 S3 兼容对象存储平台
运维·docker·容器·对象存储·minio·轩辕镜像·minio部署
大大大大晴天️5 小时前
大数据上 K8s 的三种运行范式:离线计算、实时流处理与分析服务
大数据·kubernetes