Kubernetes 之 ConfigMap

Kubernetes 之 ConfigMap

ConfigMap 定义

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pod 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。通过使用 ConfigMap 可以将你的配置数据和应用程序代码分开。

ConfigMap 的创建

  1. 直接在命令行中指定 Config 参数创建,通过--from-literal指定参数

    shell 复制代码
    kubectl create configmap mysql-config --from-literal=MYSQL_PORT=3306 --from-literal=MYSQL_ROOT_PASSWORD=passwd
    复制代码
    root@k8s-master1:~# kubectl get cm/mysql-config
    NAME           DATA   AGE
    mysql-config   2      10s
    root@k8s-master1:~# kubectl describe cm/mysql-config
    Name:         mysql-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    MYSQL_PORT:
    ----
    3306
    MYSQL_ROOT_PASSWORD:
    ----
    passwd
    
    BinaryData
    ====
  2. 通过指定文件创建,先创建一个nginx.conf文件,然后通过--from-file指令创建

    复制代码
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
            }
        }
    }
    shell 复制代码
    kubectl create configmap nginx-config --from-file nginx/nginx.conf
    复制代码
    root@k8s-master1:~# kubectl describe cm/nginx-config
    Name:         nginx-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    nginx.conf:
    ----
    worker_processes  1;\r
    \r
    events {\r
        worker_connections  1024;\r
    }\r
    \r
    http {\r
        include       /etc/nginx/mime.types;\r
        default_type  application/octet-stream;\r
    \r
        server {\r
            listen       80;\r
            server_name  localhost;\r
    \r
            location / {\r
                root   /usr/share/nginx/html;\r
                index  index.html index.htm;\r
            }\r
        }\r
    }
    
    BinaryData
    ====
    
    Events:  <none>
  3. 通过指定文件夹创建,先创建一个nginx文件夹,再创建一个index.html文件,然后把index.html文件和nginx.conf文件移动到nginx文件夹下,然后通过--from-file指令创建

    html 复制代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Nginx Test</title>
    </head>
    <body>
    Hello World
    </body>
    </html>
    shell 复制代码
    kubectl create configmap nginx-config --from-file nginx/
    复制代码
    root@k8s-master1:~# kubectl describe cm/nginx-config
    Name:         nginx-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    index.html:
    ----
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Nginx Test</title>
    </head>
    <body>
    Hello World
    </body>
    </html>
    nginx.conf:
    ----
    worker_processes  1;\r
    \r
    events {\r
        worker_connections  1024;\r
    }\r
    \r
    http {\r
        include       /etc/nginx/mime.types;\r
        default_type  application/octet-stream;\r
    \r
        server {\r
            listen       80;\r
            server_name  localhost;\r
    \r
            location / {\r
                root   /usr/share/nginx/html;\r
                index  index.html index.htm;\r
            }\r
        }\r
    }
    
    BinaryData
    ====
  4. 通过 yaml 文件的方式创建

    shell 复制代码
    kubectl create -f configmap-mysql-nginx.yaml
    复制代码
    root@k8s-master1:~# kubectl describe cm/configmap-mysql-nginx
    Name:         configmap-mysql-nginx
    Namespace:    default
    Labels:       app=configmap-mysql-nginx
    Annotations:  <none>
    
    Data
    ====
    MYSQL_PORT:
    ----
    3306
    MYSQL_ROOT_PASSWORD:
    ----
    passwd
    index.html:
    ----
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Nginx Test</title>
    </head>
    <body>
      Hello World
    </body>
    </html>
    
    nginx.conf:
    ----
    worker_processes  1;
    
    events {
      worker_connections  1024;
    }
    
    http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
    
      server {
        listen       80;
        server_name  localhost;
    
        location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
        }
      }
    }
    
    
    BinaryData
    ====
    
    Events:  <none>

ConfigMap 使用

yaml 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: pod-configmap
spec:
  containers:
    - name: configmap-test
      image: busybox
      command: [ "/bin/sh", "-c", "sleep 3600" ]
      env:
      - name: MYSQL_PORT
        valueFrom:
          configMapKeyRef:
            name: configmap-mysql-nginx
            key: MYSQL_PORT
      - name: MYSQL_ROOT_PASSWORD
        valueFrom:
          configMapKeyRef:
            name: configmap-mysql-nginx
            key: MYSQL_ROOT_PASSWORD
      envFrom:
        - configMapRef:
            name: service-config
      volumeMounts:
        - name: volume-configmap
          mountPath: /etc/nginx
  volumes:
    - name: volume-configmap
      configMap:
        name: configmap-mysql-nginx
  restartPolicy: Never
复制代码
root@k8s-master1:~# kubectl exec -it pod-configmap -- /bin/sh
/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=pod-configmap
SHLVL=1
HOME=/root
MYSQL_ROOT_PASSWORD=passwd
SERVICE_NAME=configmap-test
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYSQL_PORT=3306
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
/ # ls -al /etc/nginx/
total 12
drwxrwxrwx    3 root     root          4096 Jun  1 02:16 .
drwxr-xr-x    1 root     root          4096 Jun  1 02:16 ..
drwxr-xr-x    2 root     root          4096 Jun  1 02:16 ..2024_06_01_02_16_52.4048032045
lrwxrwxrwx    1 root     root            32 Jun  1 02:16 ..data -> ..2024_06_01_02_16_52.4048032045
lrwxrwxrwx    1 root     root            17 Jun  1 02:16 MYSQL_PORT -> ..data/MYSQL_PORT
lrwxrwxrwx    1 root     root            26 Jun  1 02:16 MYSQL_ROOT_PASSWORD -> ..data/MYSQL_ROOT_PASSWORD
lrwxrwxrwx    1 root     root            17 Jun  1 02:16 index.html -> ..data/index.html
lrwxrwxrwx    1 root     root            17 Jun  1 02:16 nginx.conf -> ..data/nginx.conf
/ #
相关推荐
打工仔折腾 AI3 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战
天衍四九-3 小时前
Docker Compose企业实战系列(八·终章):多环境隔离实战,开发/测试/生产一键切换
运维·docker·容器
ysu_03145 小时前
Docker-Desktop-WSL2实战指南-Windows从安装配置到项目容器化
windows·python·docker·容器·composer·dockerfile·wsl2
陈陈CHENCHEN6 小时前
【Kubernetes】纯 IPv6 K8s 集群访问外部 IPv4 服务
云原生·容器·kubernetes
明王明王6 小时前
从零搭建一个 AI Infra 实验室⑫:Docker 为什么默认看不到 GPU
人工智能·docker·容器
天衍四九-6 小时前
Docker Compose企业实战系列(四):Redis生产级部署|密码加固、持久化、内存优化完整方案
redis·docker·容器
troy1286 小时前
如何在谷歌云(Google Cloud)上部署 Kubernetes 集群
云原生·容器·kubernetes
xiaoligangting6 小时前
太原岗亭款式齐全
云原生
guo_wen_qiang7 小时前
docker安装常见软件集合
运维·docker·容器
程序员清风17 小时前
Docker、Nginx 与 Kubernetes 部署 AI 后端服务
nginx·docker·kubernetes