ConfigMap挂载与Subpath在Nginx容器中的应用小结

 更新时间:2024年03月06日 09:00:24   作者:华为云开发者联盟  
configmap可以通过ENV环境变量和文件两种方式挂载到容器中,修改configmap后容器中对应的ENV环境变量不会更新,将配置文件nginx.conf以configmap文件的方式挂载到容器中,本文介绍ConfigMap挂载与Subpath在Nginx容器中的应用小结,感兴趣的朋友一起看看吧

背景

nginx.conf通过configmap文件形式挂载到容器内,可以更加方便的修改nginx.conf配置

方案简介

将配置文件nginx.conf以configmap文件的方式挂载到容器中。为了更通用,可以将使用主nginx.conf include 指定xx.conf方式,主nginx.conf作为一个cm,具体xx.conf对应一个cm

configmap可以通过ENV环境变量和文件两种方式挂载到容器中,修改configmap后容器中对应的ENV环境变量不会更新;修改configmap后容器中对应的file会自动更新,如果以subpath方式挂载文件,文件内容不会自动更新

将nginx.conf作为configmap挂载到容器中

1.创建configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: default
data:
  nginx.conf: |+
    user  nginx;
    worker_processes  8;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  /var/log/nginx/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        #gzip  on;
        include /etc/nginx/conf.d/*.conf;
    }
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-server-config
  namespace: default
data:
  server1.conf: |+
    server {
            listen       80;
            server_name  server1.com;
            location / {
                root   /usr/share/nginx/html/;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
  server2.conf: |+
    server {
            listen       81;
            server_name  server2.com;
            location / {
                root   /usr/share/nginx/html/;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }

2.部署nginx业务使用对应的cm

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v1
  name: test-reload
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: test-reload
  template:
    metadata:
       labels:
        app: test-reload
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: container-1
        volumeMounts:
        - mountPath: /etc/nginx/conf.d
          name: vol-168233491311961268
        - mountPath: /etc/nginx/nginx.conf
          name: vol-168249948123126427
          readOnly: true
          subPath: nginx.conf
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: default-secret
      restartPolicy: Always
      volumes:
      - configMap:
          defaultMode: 420
          name: nginx-server-config
        name: vol-168233491311961268
      - configMap:
          defaultMode: 420
          name: nginx-config
        name: vol-168249948123126427

subpath拓展

subpath的作用如下:

  • 避免覆盖。如果挂载路径是一个已存在的目录,则目录下的内容不会被覆盖。直接将configMap/Secret挂载在容器的路径,会覆盖掉容器路径下原有的文件,使用subpath选定configMap/Secret的指定的key-value挂载在容器中,则不会覆盖掉原目录下的其他文件
  • 文件隔离。pod中含有多个容器公用一个日志volume,不同容器日志路径挂载的到不同的子目录,而不是根路径(Subpath目录会在底层存储自动创建且权限为777,无需手动创建)

避免覆盖效果演示

1.创建一个工作负载nginx,并用普通方式挂载configmap配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  test-subpath.conf: |+
    test subpath;
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      volumes:
      - configMap:
          defaultMode: 420
          name: config
        name: vol-168249948123126427
      containers:
      - image: centos:latest
        name: centos
        command:
        - /bin/bash
        args:
        - -c
        - while true;do sleep 1 &&  echo hello;done
        volumeMounts:
        - mountPath: /tmp
          name: vol-168249948123126427

2.使用docker inspect ${容器id}命令查看容器挂载信息,挂载目标为tmp目录,tmp目录下原有内容被覆盖

cke_137.png

[root@test-746c64649c-pzztn /]# ls -l /tmp/
total 0
lrwxrwxrwx 1 root root 24 Feb 27 03:02 test-subpath.conf -> ..data/test-subpath.conf

3.创建一个工作负载nginx,并用subpath方式挂载configmap配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: config
data:
  test-subpath.conf: |+
    test subpath;
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      volumes:
      - configMap:
          defaultMode: 420
          name: config
        name: vol-168249948123126427
      containers:
      - image: centos:latest
        name: centos
        command:
        - /bin/bash
        args:
        - -c
        - while true;do sleep 1 &&  echo hello;done
        volumeMounts:
        - mountPath: /tmp/test-subpath.conf
          name: vol-168249948123126427
          subPath: test-subpath.conf

4.使用docker inspect ${容器Id}命令查看容器挂载信息,挂载目标为test-subpath.conf文件,所以tmp目录下原来的文件不会被覆盖

[root@test-7b64fd6bb-56lpp /]# ls -l /tmp/
total 12
-rwx------ 1 root root 701 Dec  4  2020 ks-script-esd4my7v
-rwx------ 1 root root 671 Dec  4  2020 ks-script-eusq_sc5
-rw-r--r-- 1 root root  14 Feb 27 03:07 test-subpath.conf

文件隔离演示

1.创建工作负载test,使用hostPath卷类型持久化日志文件

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test
  name: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      volumes:
      - hostPath:
          path: /tmp/log   #该路径必须在节点上已存在
        name: vol-168249948123126427
      containers:
      - image: centos:latest
        name: centos
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        command:
        - /bin/bash
        args:
        - -c
        - while true;do echo $(POD_NAME) >> /tmp/log/app.log && sleep 900 ;done
        volumeMounts:
        - mountPath: /tmp/log
          name: vol-168249948123126427
          subPathExpr: $(POD_NAME)

2.两个Pod实例调度至同一个节点

[root@test ~]# kubectl get pod -owide -l app=test
NAME                    READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
test-69dfc665cd-2nhg5   1/1     Running   0          95s   172.16.4.59   172.16.2.172   <none>           <none>
test-69dfc665cd-z7rsj   1/1     Running   0          77s   172.16.4.25   172.16.2.172   <none>           <none>

3.进入容器内查看日志文件

[root@test ~]# kubectl exec -it test-69dfc665cd-2nhg5 bash
[root@test-69dfc665cd-2nhg5 /]# cat /tmp/log/app.log 
test-69dfc665cd-2nhg5
[root@test-69dfc665cd-2nhg5 /]# exit
exit
[root@test ~]# kubectl exec -it test-69dfc665cd-z7rsj bash
[root@test-69dfc665cd-z7rsj /]# cat /tmp/log/app.log 
test-69dfc665cd-z7rsj

4.在节点上查看挂载路径,每个Pod的日志文件用目录进行隔离,目录名为Pod名称

[root@172 log]# pwd
/tmp/log
[root@172 log]# ll
total 0
drwxr-xr-x 2 root root 60 Feb 27 15:08 test-69dfc665cd-2nhg5
drwxr-xr-x 2 root root 60 Feb 27 15:09 test-69dfc665cd-z7rsj
[root@172 log]# cat test-69dfc665cd-2nhg5/app.log 
test-69dfc665cd-2nhg5
[root@172 log]# cat test-69dfc665cd-z7rsj/app.log 
test-69dfc665cd-z7rsj

到此这篇关于ConfigMap挂载与Subpath在Nginx容器中的应用的文章就介绍到这了,更多相关Subpath Nginx容器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • django8.5 项目部署Nginx的操作步骤

    django8.5 项目部署Nginx的操作步骤

    nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件。它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用,下面小编给大家带来了django8.5 项目部署Nginx的操作步骤,感兴趣的朋友一起看看吧
    2022-01-01
  • Nginx配置srcache_nginx模块搭配Redis建立缓存系统

    Nginx配置srcache_nginx模块搭配Redis建立缓存系统

    这篇文章主要介绍了Nginx配置srcache_nginx模块搭配Redis建立缓存系统的方法,文中关于Nginx模块和Redis数据库的安装就不再说明了,这里只关注配置搭建阶段,需要的朋友可以参考下
    2016-01-01
  • 简单快速搭建Nginx文件服务器

    简单快速搭建Nginx文件服务器

    这篇文章主要为大家介绍了简单快速搭建Nginx文件服务器方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • 简介Nginx服务器的Websockets配置方法

    简介Nginx服务器的Websockets配置方法

    这篇文章主要介绍了简介Nginx服务器的Websockets配置方法,是使用Nginx服务器的网管的必备知识XD~需要的朋友可以参考下
    2015-06-06
  • Nginx访问php文件直接下载的解决方法

    Nginx访问php文件直接下载的解决方法

    本文主要给大家介绍了如何解决Nginx访问php文件直接下载,这种情况通常是因为nginx没有将PHP文件交给PHP解释器处理,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Nginx带宽控制(限速模块使用)

    Nginx带宽控制(限速模块使用)

    这篇文章主要介绍了Nginx带宽控制(限速模块使用),本文讲解了使用limit_rate和limit_rate_aft以及limit_conn实现带宽控制的例子,需要的朋友可以参考下
    2015-03-03
  • Nginx 启用 OCSP Stapling的配置

    Nginx 启用 OCSP Stapling的配置

    本篇文章主要介绍了Nginx 启用 OCSP Stapling的配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Laravel的Nginx重写规则实例代码

    Laravel的Nginx重写规则实例代码

    这篇文章主要介绍了Laravel的Nginx重写规则实例代码,需要的朋友可以参考下
    2017-09-09
  • nginx 负载均衡轮询方式配置详解

    nginx 负载均衡轮询方式配置详解

    负载均衡(load-balance)就是将负载分摊到多个操作单元上执行,从而提高服务的可用性和响应速度,带给用户更好的体验,本文给大家介绍nginx 负载均衡轮询方式配置,感兴趣的朋友一起看看吧
    2022-03-03
  • 通俗易懂讲解nginx-rtmp-module

    通俗易懂讲解nginx-rtmp-module

    nginx-rtmp-module 是一个用于 Nginx 的第三方模块,它扩展了 Nginx 服务器的功能,使其能够处理实时流媒体数据,本文就来详细的介绍一下nginx-rtmp-module的使用,感兴趣的可以了解一下
    2025-02-02

最新评论