当前位置:   article > 正文

开发实践丨nginx.conf以configmap文件形式挂载到nginx容器中以及subpath使用场景

开发实践丨nginx.conf以configmap文件形式挂载到nginx容器中以及subpath使用场景

本文分享自华为云社区《nginx.conf以configmap文件形式挂载到nginx容器中以及subpath使用场景》,作者:可以交个朋友。

背景

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

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: nginx-config
  5. namespace: default
  6. data:
  7. nginx.conf: |+
  8. user nginx;
  9. worker_processes 8;
  10. error_log /var/log/nginx/error.log warn;
  11. pid /var/run/nginx.pid;
  12. events {
  13. worker_connections 1024;
  14. }
  15. http {
  16. include /etc/nginx/mime.types;
  17. default_type application/octet-stream;
  18. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  19. '$status $body_bytes_sent "$http_referer" '
  20. '"$http_user_agent" "$http_x_forwarded_for"';
  21. access_log /var/log/nginx/access.log main;
  22. sendfile on;
  23. keepalive_timeout 65;
  24. #gzip on;
  25. include /etc/nginx/conf.d/*.conf;
  26. }
  27. ---
  28. apiVersion: v1
  29. kind: ConfigMap
  30. metadata:
  31. name: nginx-server-config
  32. namespace: default
  33. data:
  34. server1.conf: |+
  35. server {
  36. listen 80;
  37. server_name server1.com;
  38. location / {
  39. root /usr/share/nginx/html/;
  40. index index.html index.htm;
  41. }
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. root html;
  45. }
  46. }
  47. server2.conf: |+
  48. server {
  49. listen 81;
  50. server_name server2.com;
  51. location / {
  52. root /usr/share/nginx/html/;
  53. index index.html index.htm;
  54. }
  55. error_page 500 502 503 504 /50x.html;
  56. location = /50x.html {
  57. root html;
  58. }
  59. }

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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. version: v1
  6. name: test-reload
  7. namespace: default
  8. spec:
  9. progressDeadlineSeconds: 600
  10. replicas: 1
  11. revisionHistoryLimit: 10
  12. selector:
  13. matchLabels:
  14. app: test-reload
  15. template:
  16. metadata:
  17. labels:
  18. app: test-reload
  19. spec:
  20. containers:
  21. - image: nginx:latest
  22. imagePullPolicy: Always
  23. name: container-1
  24. volumeMounts:
  25. - mountPath: /etc/nginx/conf.d
  26. name: vol-168233491311961268
  27. - mountPath: /etc/nginx/nginx.conf
  28. name: vol-168249948123126427
  29. readOnly: true
  30. subPath: nginx.conf
  31. dnsPolicy: ClusterFirst
  32. imagePullSecrets:
  33. - name: default-secret
  34. restartPolicy: Always
  35. volumes:
  36. - configMap:
  37. defaultMode: 420
  38. name: nginx-server-config
  39. name: vol-168233491311961268
  40. - configMap:
  41. defaultMode: 420
  42. name: nginx-config
  43. name: vol-168249948123126427

subpath拓展

subpath的作用如下:

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

避免覆盖效果演示

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

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: config
  5. data:
  6. test-subpath.conf: |+
  7. test subpath;
  8. ---
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. metadata:
  12. labels:
  13. app: test
  14. name: test
  15. spec:
  16. replicas: 1
  17. selector:
  18. matchLabels:
  19. app: test
  20. template:
  21. metadata:
  22. labels:
  23. app: test
  24. spec:
  25. volumes:
  26. - configMap:
  27. defaultMode: 420
  28. name: config
  29. name: vol-168249948123126427
  30. containers:
  31. - image: centos:latest
  32. name: centos
  33. command:
  34. - /bin/bash
  35. args:
  36. - -c
  37. - while true;do sleep 1 && echo hello;done
  38. volumeMounts:
  39. - mountPath: /tmp
  40. name: vol-168249948123126427

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

cke_137.png

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

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

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: config
  5. data:
  6. test-subpath.conf: |+
  7. test subpath;
  8. ---
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. metadata:
  12. labels:
  13. app: test
  14. name: test
  15. spec:
  16. replicas: 1
  17. selector:
  18. matchLabels:
  19. app: test
  20. template:
  21. metadata:
  22. labels:
  23. app: test
  24. spec:
  25. volumes:
  26. - configMap:
  27. defaultMode: 420
  28. name: config
  29. name: vol-168249948123126427
  30. containers:
  31. - image: centos:latest
  32. name: centos
  33. command:
  34. - /bin/bash
  35. args:
  36. - -c
  37. - while true;do sleep 1 && echo hello;done
  38. volumeMounts:
  39. - mountPath: /tmp/test-subpath.conf
  40. name: vol-168249948123126427
  41. subPath: test-subpath.conf

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

cke_138.png

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

文件隔离演示

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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: test
  6. name: test
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: test
  12. template:
  13. metadata:
  14. labels:
  15. app: test
  16. spec:
  17. volumes:
  18. - hostPath:
  19. path: /tmp/log #该路径必须在节点上已存在
  20. name: vol-168249948123126427
  21. containers:
  22. - image: centos:latest
  23. name: centos
  24. env:
  25. - name: POD_NAME
  26. valueFrom:
  27. fieldRef:
  28. fieldPath: metadata.name
  29. command:
  30. - /bin/bash
  31. args:
  32. - -c
  33. - while true;do echo $(POD_NAME) >> /tmp/log/app.log && sleep 900 ;done
  34. volumeMounts:
  35. - mountPath: /tmp/log
  36. name: vol-168249948123126427
  37. subPathExpr: $(POD_NAME)

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

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

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

  1. [root@test ~]# kubectl exec -it test-69dfc665cd-2nhg5 bash
  2. [root@test-69dfc665cd-2nhg5 /]# cat /tmp/log/app.log
  3. test-69dfc665cd-2nhg5
  4. [root@test-69dfc665cd-2nhg5 /]# exit
  5. exit
  6. [root@test ~]# kubectl exec -it test-69dfc665cd-z7rsj bash
  7. [root@test-69dfc665cd-z7rsj /]# cat /tmp/log/app.log
  8. test-69dfc665cd-z7rsj

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

  1. [root@172 log]# pwd
  2. /tmp/log
  3. [root@172 log]# ll
  4. total 0
  5. drwxr-xr-x 2 root root 60 Feb 27 15:08 test-69dfc665cd-2nhg5
  6. drwxr-xr-x 2 root root 60 Feb 27 15:09 test-69dfc665cd-z7rsj
  7. [root@172 log]# cat test-69dfc665cd-2nhg5/app.log
  8. test-69dfc665cd-2nhg5
  9. [root@172 log]# cat test-69dfc665cd-z7rsj/app.log
  10. test-69dfc665cd-z7rsj

点击关注,第一时间了解华为云新鲜技术~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/195736
推荐阅读
相关标签
  

闽ICP备14008679号