在k8s中将nginx.conf文件内容创建为ConfigMap挂载到pod容器中

将nginx.conf文件内容创建为ConfigMap

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log error;
pid    /var/run/nginx.pid;
worker_rlimit_nofile 65536;

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

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format log_json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"upstream_addr": "$upstream_addr",'
                        '"upstream_status": "$upstream_status",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';

    access_log  /var/log/nginx/access.log  log_json;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    
    proxy_connect_timeout 90;
    proxy_read_timeout 300;
    proxy_send_timeout 300;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers   4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary on;
    gzip_proxied any;
    gzip_disable "MSIE [1-6].";
    
    server {
        listen       80;
        server_name  localhost;
        add_header Cache-Control no-cache;
        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.html?s=$1 last;
                break;
            }
        }
        access_log  /var/log/nginx/default_access.log log_json;
    }

}

创建数据卷挂载到容器

在这个例子中,我们使用 ConfigMap nginx.conf 中的 KEY nginx.conf中的内容挂载到容器的 /etc/nginx/nginx.conf 路径,以此替换了容器镜像中该路径原来的文件内容; (在挂载时指定数据卷内子路径)

也可以将 ConfigMap 作为一个数据卷(在挂载时不指定数据卷内子路径)挂载到容器,此时 ConfigMap 将映射成一个文件夹,每一个 KEY 是文件夹下的文件名,KEY 对应的 VALUE 是文件当中的内容。

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/15011765.html