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
/ #
相关推荐
皮锤打乌龟4 小时前
(干货)Jenkins使用kubernetes插件连接k8s的认证方式
运维·kubernetes·jenkins
南猿北者5 小时前
docker Network(网络)
网络·docker·容器
ggaofeng8 小时前
通过命令学习k8s
云原生·容器·kubernetes
death bell8 小时前
Docker基础概念
运维·docker·容器
想学习java初学者11 小时前
Docker Compose部署Kafka(非Zookeeper)
docker·容器·kafka
qq_道可道11 小时前
K8S升级到1.24后,切换运行时导致 dind 构建镜像慢根因定位与解决
云原生·容器·kubernetes
SONGW201812 小时前
k8s拓扑域 :topologyKey
kubernetes
weixin_4381973813 小时前
K8S实现反向代理,负载均衡
linux·运维·服务器·nginx·kubernetes
郝同学的测开笔记14 小时前
云原生探索系列(十二):Go 语言接口详解
后端·云原生·go
mit6.82415 小时前
[Docker#5] 镜像仓库 | 命令 | 实验:搭建Nginx | 创建私有仓库
linux·后端·docker·云原生