欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ConfigMap掛載與Subpath在Nginx容器中的應用小結

 更新時間:2024年03月06日 09:00:24   作者:華為云開發(fā)者聯盟  
configmap可以通過ENV環(huán)境變量和文件兩種方式掛載到容器中,修改configmap后容器中對應的ENV環(huán)境變量不會更新,將配置文件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環(huán)境變量和文件兩種方式掛載到容器中,修改configmap后容器中對應的ENV環(huán)境變量不會更新;修改configmap后容器中對應的file會自動更新,如果以subpath方式掛載文件,文件內容不會自動更新

將nginx.conf作為configmap掛載到容器中

1.創(chuàng)建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業(yè)務使用對應的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目錄會在底層存儲自動創(chuàng)建且權限為777,無需手動創(chuàng)建)

避免覆蓋效果演示

1.創(chuàng)建一個工作負載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.創(chuàng)建一個工作負載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.創(chuàng)建工作負載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   #該路徑必須在節(jié)點上已存在
        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實例調度至同一個節(jié)點

[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.在節(jié)點上查看掛載路徑,每個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容器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • django8.5?項目部署Nginx的操作步驟

    django8.5?項目部署Nginx的操作步驟

    nginx是一個開源的,支持高性能,高并發(fā)的www服務和代理服務軟件。它是一個俄羅斯人lgor sysoev開發(fā)的,作者將源代碼開源出來供全球使用,下面小編給大家?guī)砹薲jango8.5?項目部署Nginx的操作步驟,感興趣的朋友一起看看吧
    2022-01-01
  • Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)

    Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)

    這篇文章主要介紹了Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)的方法,文中關于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以及l(fā)imit_conn實現帶寬控制的例子,需要的朋友可以參考下
    2015-03-03
  • Nginx 啟用 OCSP Stapling的配置

    Nginx 啟用 OCSP Stapling的配置

    本篇文章主要介紹了Nginx 啟用 OCSP Stapling的配置,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Laravel的Nginx重寫規(guī)則實例代碼

    Laravel的Nginx重寫規(guī)則實例代碼

    這篇文章主要介紹了Laravel的Nginx重寫規(guī)則實例代碼,需要的朋友可以參考下
    2017-09-09
  • nginx?負載均衡輪詢方式配置詳解

    nginx?負載均衡輪詢方式配置詳解

    負載均衡(load-balance)就是將負載分攤到多個操作單元上執(zhí)行,從而提高服務的可用性和響應速度,帶給用戶更好的體驗,本文給大家介紹nginx?負載均衡輪詢方式配置,感興趣的朋友一起看看吧
    2022-03-03
  • 通俗易懂講解nginx-rtmp-module

    通俗易懂講解nginx-rtmp-module

    nginx-rtmp-module?是一個用于 Nginx 的第三方模塊,它擴展了 Nginx 服務器的功能,使其能夠處理實時流媒體數據,本文就來詳細的介紹一下nginx-rtmp-module的使用,感興趣的可以了解一下
    2025-02-02

最新評論