ConfigMap

ConfigMap作用是存储不加密的数据到etcd中,让Pod以变量或数据卷Volume挂载到容器中

应用场景:配置文件

创建配置文件

首先我们需要创建一个配置文件 nginx.conf

user  nginx;
        worker_processes  auto;
        worker_cpu_affinity 00000001 00000010 00000100 00001000;

        error_log  /var/log/nginx/error.log warn;
        pid        /var/run/nginx.pid;


        worker_rlimit_nofile 65536;

        events {
                worker_connections  65535;
                accept_mutex on;
                multi_accept on;
        }


        http {
                include       mime.types;
                default_type  application/octet-stream;
                log_format access_json '{"@timestamp":"$time_iso8601",'
                '"host":"$server_addr",'
                '"clientip":"$remote_addr",'
                '"size":$body_bytes_sent,'
                '"responsetime":$request_time,'
                '"upstreamtime":"$upstream_response_time",'
                '"upstreamhost":"$upstream_addr",'
                '"http_host":"$host",'
                '"url":"$uri",'
                '"domain":"$host",'
                '"xff":"$http_x_forwarded_for",'
                '"referer":"$http_referer",'
                '"status":"$status"}';
                access_log  /var/log/nginx/access.log  access_json;

                client_max_body_size 50M;
                keepalive_timeout  300;
                fastcgi_buffers 8 128k;
                fastcgi_buffer_size  128k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
                proxy_connect_timeout 90;
                proxy_read_timeout 300;
                proxy_send_timeout 300;
                sendfile        on;

                server {
                        listen       80;
                        server_name  localhost;
                        add_header Cache-Control no-cache;

                        location / {
                                root   /usr/share/nginx/html;
                                        #proxy_read_timeout 220s
                                index  index.html index.htm;
                        }


                        error_page   500 502 503 504  /50x.html;
                        location = /50x.html {
                                root   html;
                        }
                }

                include /etc/nginx/conf.d/*.conf;

                }

命令创建ConfigMap

kubectl create configmap nginx1-config --from-file=nginx.conf

然后查看详细信息

kubectl describe cm nginx1-config

yaml方式创建ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
    name: nginx1-config
data:
  nginx.conf: |
        user  nginx;
        worker_processes  auto;
        worker_cpu_affinity 00000001 00000010 00000100 00001000;

        error_log  /var/log/nginx/error.log warn;
        pid        /var/run/nginx.pid;


        worker_rlimit_nofile 65536;

        events {
                worker_connections  65535;
                accept_mutex on;
                multi_accept on;
        }


        http {
                include       mime.types;
                default_type  application/octet-stream;
                log_format access_json '{"@timestamp":"$time_iso8601",'
                '"host":"$server_addr",'
                '"clientip":"$remote_addr",'
                '"size":$body_bytes_sent,'
                '"responsetime":$request_time,'
                '"upstreamtime":"$upstream_response_time",'
                '"upstreamhost":"$upstream_addr",'
                '"http_host":"$host",'
                '"url":"$uri",'
                '"domain":"$host",'
                '"xff":"$http_x_forwarded_for",'
                '"referer":"$http_referer",'
                '"status":"$status"}';
                access_log  /var/log/nginx/access.log  access_json;

                client_max_body_size 50M;
                keepalive_timeout  300;
                fastcgi_buffers 8 128k;
                fastcgi_buffer_size  128k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
                proxy_connect_timeout 90;
                proxy_read_timeout 300;
                proxy_send_timeout 300;
                sendfile        on;

                server {
                        listen       80;
                        server_name  localhost;
                        add_header Cache-Control no-cache;

                        location / {
                                root   /usr/share/nginx/html;
                                        #proxy_read_timeout 220s
                                index  index.html index.htm;
                        }


                        error_page   500 502 503 504  /50x.html;
                        location = /50x.html {
                                root   html;
                        }
                }

                include /etc/nginx/conf.d/*.conf;

                }

Volume数据卷形式挂载

vim cm.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web1-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
        - name: web1
          image: "nginx:latest"
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
          volumeMounts:
          - name: nginx1-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf

      volumes:
        - name: nginx1-config
          configMap:
            name: nginx1-config
            items:
            - key: nginx.conf
              path: nginx.conf

kubectl apply -f cm.yaml

kubectl get deploy -o wide

进入pod容器查看是否修改

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐