章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • ansible系列文章

  • Kubernetes笔记

    • 安装篇-kubeadm
    • k8s入门
    • k8s安装篇二进制
    • k8s面试题
    • kubernetes(k8s)yaml文件详解
    • k8s报错小结
    • Kubernetes 安装配置ingress controller
    • cka考试真题
    • ingress配置证书
    • cka考试作业
    • k8s部署java项目
    • jenkins脚本式流水线部署k8s项目实例一
    • helm v3安装并创建例子
    • 使用helm将本地部署文件上传到harbor chart上
    • helm公共仓库创建
    • helm适应minio作为私有仓库
    • helm release使用说明
    • kubernetes核心概念
    • kubectl使用技巧
    • kubernetes卷的几种类型
    • kubernetes安全框架
    • 云原生-什么是HPA和PDB、VPA
    • k8s部署php项目示例
    • 配置kubeconfig 文件访问 Kubernetes 集群
    • configmap配置的几种方式
      • k8s配置go服务
      • k8s部署java项目
      • kubernetes部署prometheus监控
      • kubernetes部署elk日志系统
      • kubernetes环境devops流水线
      • kubernetes高阶技能必备的工具
      • deployment中使用configmap、secret的方式
      • 业务pod 飘移pending排查分析
    • elk

    • jenkins

    • GitLabCI_CD

    • 专题
    • Kubernetes笔记
    章工运维
    2025-02-18
    目录

    configmap配置的几种方式

    # deployment的配置

    • 针对单个文件
        #只展示部分内容
        spec:
          containers:
          - name: php
            image: docker-images/pic:v1
            ports:
            - containerPort: 80
            volumeMounts:
            - name: database-config
              mountPath: /var/www/html/config/database.php
              subPath: database.php
            resources:
              requests:
                memory: "128Mi"
                cpu: "100m"
              limits:
                memory: "256Mi"
                cpu: "200m"
          volumes:
          - name: database-config
            configMap:
              name: app-config-files
              items:
              - key: database.php
                path: database.php
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    • 针对目录的配置

          spec:
            containers:
            - name: php
              image: docker-images/pic:v1
              ports:
              - containerPort: 80
              volumeMounts:
              - name: app-config
                mountPath: /var/www/html/config
                readOnly: true
              resources:
                requests:
                  memory: "128Mi"
                  cpu: "100m"
                limits:
                  memory: "256Mi"
                  cpu: "200m"
            volumes:
            - name: app-config
              configMap:
                name: app-config-files
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
    • 配置敏感数据

    apiVersion: v1
    kind: Secret
    metadata:
      name: app-secret-config
    type: Opaque
    data:
      database.php: |
        <?php
        return [
            'mysql' => [
                'host' => 'mysql-service',
                'port' => '3306',
                'database' => 'myapp',
                'username' => base64_encode('username'),
                'password' => base64_encode('password'),
                'charset' => 'utf8mb4',
            ],
        ];
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    # 创建configmap的几种方式

    # 1. 从单个文件创建 ConfigMap

    假设你有一个名为 config.properties 的文件,其内容如下:

    key1=value1
    key2=value2
    
    1
    2

    你可以使用以下命令快速创建一个 ConfigMap,将该文件的内容作为一个键值对:

    kubectl create configmap my-config --from-file=config.properties
    
    1

    执行后,my-config 将包含一个名为 config.properties 的键,文件内容将作为值:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-config
    data:
      config.properties: |
        key1=value1
        key2=value2
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 2. 从目录创建 ConfigMap

    假设你有一个目录 /path/to/config/,该目录中包含多个配置文件,下面是该目录的文件列表:

    /path/to/config/
        ├── config1.properties
        ├── config2.properties
    
    1
    2
    3

    config1.properties 内容:

    key1=value1
    key2=value2
    
    1
    2

    config2.properties 内容:

    key3=value3
    key4=value4
    
    1
    2

    使用以下命令将整个目录的内容创建为 ConfigMap:

    kubectl create configmap my-config --from-file=/path/to/config/
    
    1

    执行后,my-config 将包含以下内容:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-config
    data:
      config1.properties: |
        key1=value1
        key2=value2
      config2.properties: |
        key3=value3
        key4=value4
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    # 3. 从多个文件创建 ConfigMap

    假设你有两个独立的配置文件 config1.properties 和 config2.properties,分别位于 /path/to/config1/ 和 /path/to/config2/:

    /path/to/config1/config1.properties 内容:

    key1=value1
    key2=value2
    
    1
    2

    /path/to/config2/config2.properties 内容:

    key3=value3
    key4=value4
    
    1
    2

    你可以通过以下命令将它们分别添加到 ConfigMap 中:

    kubectl create configmap my-config --from-file=/path/to/config1/config1.properties --from-file=/path/to/config2/config2.properties
    
    1

    执行后,my-config 将包含:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-config
    data:
      config1.properties: |
        key1=value1
        key2=value2
      config2.properties: |
        key3=value3
        key4=value4
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    # 总结

    • 从单个文件创建:将一个文件的内容作为 ConfigMap 键值对。
    • 从目录创建:将目录中的所有文件(以文件名作为键,文件内容作为值)创建为 ConfigMap。
    • 从多个文件创建:将多个文件分别作为独立的键值对添加到 ConfigMap 中。

    这些是快速根据不同场景创建 ConfigMap 的方式。

    微信 支付宝
    上次更新: 2025/02/18, 09:32:44

    ← 配置kubeconfig 文件访问 Kubernetes 集群 k8s配置go服务→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式