nginx configmap

root@ubuntu:~/nginx_ingress# cat nginx-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  nginx.conf: |
    user nginx;
    worker_processes  3;
    error_log  /var/log/nginx/error.log;
    events {
      worker_connections  10240;
    }
    http {
      log_format  main
              'remote_addr:$remote_addr	'
              'time_local:$time_local	'
              'method:$request_method	'
              'uri:$request_uri	'
              'host:$host	'
              'status:$status	'
              'bytes_sent:$body_bytes_sent	'
              'referer:$http_referer	'
              'useragent:$http_user_agent	'
              'forwardedfor:$http_x_forwarded_for	'
              'request_time:$request_time';
      access_log        /var/log/nginx/access.log main;
      server {
          listen       80;
          server_name  _;
          location / {
              root   html;
              index  index.html index.htm;
          }
      }
      include /etc/nginx/virtualhost/virtualhost.conf;
    }
  virtualhost.conf: |
    upstream app {
      server localhost:8080;
      keepalive 1024;
    }
    server {
      listen 80 default_server;
      root /usr/local/app;
      access_log /var/log/nginx/app.access_log main;
      error_log /var/log/nginx/app.error_log;
      location / {
        proxy_pass http://app/;
        proxy_http_version 1.1;
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-config
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-config
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx # mount nginx-conf volumn to /etc/nginx
          readOnly: true
          name: nginx-conf
        - mountPath: /var/log/nginx
          name: log
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf # place ConfigMap `nginx-conf` on /etc/nginx
          items:
            - key: nginx.conf
              path: nginx.conf
            - key: virtualhost.conf
              path: virtualhost/virtualhost.conf # dig directory
      - name: log
        emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx-config
root@ubuntu:~/nginx_ingress# kubectl create -f  nginx-configmap.yaml 
Error from server (AlreadyExists): error when creating "nginx-configmap.yaml": configmaps "nginx-conf" already exists
error validating "nginx-configmap.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false
root@ubuntu:~/nginx_ingress# 
root@ubuntu:~/nginx_ingress# cat nginx-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf-test
data:
  nginx.conf: |
    user nginx;
    worker_processes  3;
    error_log  /var/log/nginx/error.log;
    events {
      worker_connections  10240;
    }
    http {
      log_format  main
              'remote_addr:$remote_addr	'
              'time_local:$time_local	'
              'method:$request_method	'
              'uri:$request_uri	'
              'host:$host	'
              'status:$status	'
              'bytes_sent:$body_bytes_sent	'
              'referer:$http_referer	'
              'useragent:$http_user_agent	'
              'forwardedfor:$http_x_forwarded_for	'
              'request_time:$request_time';
      access_log        /var/log/nginx/access.log main;
      server {
          listen       80;
          server_name  _;
          location / {
              root   html;
              index  index.html index.htm;
          }
      }
      include /etc/nginx/virtualhost/virtualhost.conf;
    }
  virtualhost.conf: |
    upstream app {
      server localhost:8080;
      keepalive 1024;
    }
    server {
      listen 80 default_server;
      root /usr/local/app;
      access_log /var/log/nginx/app.access_log main;
      error_log /var/log/nginx/app.error_log;
      location / {
        proxy_pass http://app/;
        proxy_http_version 1.1;
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-config
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-config
  template:
    metadata:
      labels:
        app: nginx-config
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx # mount nginx-conf volumn to /etc/nginx
          readOnly: true
          name: nginx-conf
        - mountPath: /var/log/nginx
          name: log
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf-test # place ConfigMap `nginx-conf` on /etc/nginx
          items:
            - key: nginx.conf
              path: nginx.conf
            - key: virtualhost.conf
              path: virtualhost/virtualhost.conf # dig directory
      - name: log
        emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-config-test
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx-config
root@ubuntu:~/nginx_ingress# kubectl create -f  nginx-configmap.yaml 
configmap/nginx-conf-test created
deployment.apps/nginx-config created
service/nginx-config-test created
root@ubuntu:~/nginx_ingress# kubectl get configmaps nginx-conf-test
NAME              DATA   AGE
nginx-conf-test   2      13s
root@ubuntu:~/nginx_ingress# kubectl describe  configmaps nginx-conf-test
Name:         nginx-conf-test
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx.conf:
----
user nginx;
worker_processes  3;
error_log  /var/log/nginx/error.log;
events {
  worker_connections  10240;
}
http {
  log_format  main
          'remote_addr:$remote_addr	'
          'time_local:$time_local	'
          'method:$request_method	'
          'uri:$request_uri	'
          'host:$host	'
          'status:$status	'
          'bytes_sent:$body_bytes_sent	'
          'referer:$http_referer	'
          'useragent:$http_user_agent	'
          'forwardedfor:$http_x_forwarded_for	'
          'request_time:$request_time';
  access_log  /var/log/nginx/access.log main;
  server {
      listen       80;
      server_name  _;
      location / {
          root   html;
          index  index.html index.htm;
      }
  }
  include /etc/nginx/virtualhost/virtualhost.conf;
}

virtualhost.conf:
----
upstream app {
  server localhost:8080;
  keepalive 1024;
}
server {
  listen 80 default_server;
  root /usr/local/app;
  access_log /var/log/nginx/app.access_log main;
  error_log /var/log/nginx/app.error_log;
  location / {
    proxy_pass http://app/;
    proxy_http_version 1.1;
  }
}

Events:  <none>
root@ubuntu:~/nginx_ingress# kubectl exec -it nginx-config-7775cff659-8pf2v  -- sh
# ps -elf | grep nginx
sh: 1: ps: not found
# top
sh: 2: top: not found
# ls /etc/nginx/virtualhost/virtualhost.conf
/etc/nginx/virtualhost/virtualhost.conf
# ^[[A: not foundB
# : 4: 
# 
# cat /etc/nginx/virtualhost/virtualhost.conf
upstream app {
  server localhost:8080;
  keepalive 1024;
}
server {
  listen 80 default_server;
  root /usr/local/app;
  access_log /var/log/nginx/app.access_log main;
  error_log /var/log/nginx/app.error_log;
  location / {
    proxy_pass http://app/;
    proxy_http_version 1.1;
  }
}
# ls /var/log/nginx/
access.log  app.access_log  app.error_log  error.log
# ls /etc/nginx
nginx.conf  virtualhost
# ^[[A^[[B
sh: 10: : not found
# cat /etc      ^H^H
cat: /etc: Is a directory
cat: ''$'': No such file or directory
# cat 、^H
cat: ''$'343200201': No such file or directory
# cat /etc      ^H
root@ubuntu:~/nginx_ingress# kubectl exec -it nginx-config-7775cff659-8pf2v  -- /bin/bash
root@nginx-config-7775cff659-8pf2v:/# ls /etc/nginx 
nginx.conf  virtualhost
root@nginx-config-7775cff659-8pf2v:/# cat /etc/nginx/nginx.conf    
user nginx;
worker_processes  3;
error_log  /var/log/nginx/error.log;
events {
  worker_connections  10240;
}
http {
  log_format  main
          'remote_addr:$remote_addr	'
          'time_local:$time_local	'
          'method:$request_method	'
          'uri:$request_uri	'
          'host:$host	'
          'status:$status	'
          'bytes_sent:$body_bytes_sent	'
          'referer:$http_referer	'
          'useragent:$http_user_agent	'
          'forwardedfor:$http_x_forwarded_for	'
          'request_time:$request_time';
  access_log    /var/log/nginx/access.log main;
  server {
      listen       80;
      server_name  _;
      location / {
          root   html;
          index  index.html index.htm;
      }
  }
  include /etc/nginx/virtualhost/virtualhost.conf;
}
root@nginx-config-7775cff659-8pf2v:/# cat /etc/nginx/virtualhost/virtualhost.conf 
upstream app {
  server localhost:8080;
  keepalive 1024;
}
server {
  listen 80 default_server;
  root /usr/local/app;
  access_log /var/log/nginx/app.access_log main;
  error_log /var/log/nginx/app.error_log;
  location / {
    proxy_pass http://app/;
    proxy_http_version 1.1;
  }
}
root@nginx-config-7775cff659-8pf2v:/# 

但是修改配置文件后的内容,容器里不能自动更新,必须对该容器进行删除(不能进行软重启),重新运行,会导致业务中断,短暂数据丢失的可能发生。

更改worker_processes 

root@ubuntu:~/nginx_ingress# kubectl describe  cm nginx-conf-test 
Name:         nginx-conf-test
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx.conf:
----
user nginx;
worker_processes  4;
error_log  /var/log/nginx/error.log;
events {
  worker_connections  10240;
}
root@ubuntu:~/nginx_ingress# kubectl exec -it nginx-config-7775cff659-8pf2v  -- /bin/bash
root@nginx-config-7775cff659-8pf2v:/# cat /etc/nginx/nginx.conf                                     
user nginx;
worker_processes  3;
error_log  /var/log/nginx/error.log;
events {
  worker_connections  10240;
}
http {
  log_format  main
          'remote_addr:$remote_addr	'
          'time_local:$time_local	'
          'method:$request_method	'
          'uri:$request_uri	'
          'host:$host	'
          'status:$status	'
          'bytes_sent:$body_bytes_sent	'
          'referer:$http_referer	'
          'useragent:$http_user_agent	'
          'forwardedfor:$http_x_forwarded_for	'
          'request_time:$request_time';
  access_log    /var/log/nginx/access.log main;
  server {
      listen       80;
      server_name  _;
      location / {
          root   html;
          index  index.html index.htm;
      }
  }
  include /etc/nginx/virtualhost/virtualhost.conf;
}

k8s中configmap挂载配置nginx.conf

原文地址:https://www.cnblogs.com/dream397/p/15242057.html