本文分享自华为云社区《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

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目录下原有内容被覆盖

[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容器中的应用的更多相关文章

  1. docker挂载war包到tomcat容器中的注意点和坑

    刚开始用docker,难免会遇到很多坑,这里分享一下: 一 挂载最好挂载目录 我刚开始挂载war包,结果发现容器里把挂载的war包当成目录了 二 本地路径必须是绝对路径,否则不管用 三 容器中使用vi ...

  2. 如何在nginx容器中使用ping、nslookup、ip、curl 等工具?

    Nginx镜像太精简了,启动一个容器进行测试时,常用的网络工具都没有,可以使用下面的命令进行安装.也可以直接起一个busybox容器进行测试. apt update #ping apt install ...

  3. 使用docker-compose运行nginx容器挂载时遇到的文件/目录问题

    单独使用docker run命令指定挂载文件路径运行nginx容器是可以的,但是用在docker-compose中就不行了 报错如下: 原因就是挂载出错,不能直接挂载文件,还有挂载的容器里的目录要正确 ...

  4. 在 Docker 容器中运行应用程序

    案例说明 运行 3 个容器,实现对网站的监控. 三个容器的说明: 容器 web: 创建自 nginx 映像,使用 80 端口,运行于后台,实现 web 服务. 容器 mailer: 该容器中运行一个 ...

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

    将nginx.conf文件内容创建为ConfigMap user nginx; worker_processes auto; error_log /var/log/nginx/error.log er ...

  6. docker中使用nginx容器代理其他容器

    Nginx is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, ...

  7. 在win10 docker启动的centos容器中安装nginx

    我是在win10机器上搭建了一个docker,在docker启动了centos容器,在centos中安装nginx. 安装配置docker 直接在官网下载docker for windows:http ...

  8. 挂载本地file到容器中

    -v /Us……/cts/fffen:/usr/local/src -v 标记 将本地主机的目录 到 目标容器的路径下 在容器中查看:ls  发现已经存在py文件 运行python fenci.py ...

  9. 创建新镜像-从已创建的容器中更新镜像并提交镜像(以Nginx为例)

    目标:现在我们主要是修改nginx的index.html,然后做一个新镜像 1.基于nginx:1.12运行一个容器 docker run -d -p 8080:80 --name nginx ngi ...

  10. 复制docker容器中的nginx某个文件到linux中

    前提:docker容器中的nginx要开启

随机推荐

  1. React中函数组件与类组件的两种使用

    React 创建组件的两种方式 函数组件:使用js函数创建的组件 约定1:函数名称必须以大写字母开头 约定2:函数组件必须要有返回值. 如果返回值为null.表示不渲染任何内容. return nul ...

  2. 01uni-app的创建运行在不同端上的配置 以及tarBar的配置

    uni-app的创建### 01==>创建uni-app的项目非常简单.不需要注意什么注意点哈!! 创建项目的时候 可以参考官网 https://uniapp.dcloud.io/quickst ...

  3. js分钟转化为小时并且以某个数字进行递增

    有些时候,我们需要将分钟转为小时: 并且还有以一个数字进行递增: 呈现出[3,6,9,12,15,18]这样的递增形式 // 因为是递增,所以是相加: // 在使用+号的时候: // 注意两边都是数字 ...

  4. windowsbat删除命令

    widnwosbat命令 DEL /F /A /Q \?%1 用于删除指定路径下的文件,参数含义如下: /F: Force delete,即强制删除: /A: 用于指定文件属性,A代表存档,D代表目录 ...

  5. 3.1 Windows驱动开发:内核远程堆分配与销毁

    在开始学习内核内存读写篇之前,我们先来实现一个简单的内存分配销毁堆的功能,在内核空间内用户依然可以动态的申请与销毁一段可控的堆空间,一般而言内核中提供了ZwAllocateVirtualMemory这 ...

  6. C/C++ Npcap包实现ARP欺骗

    npcap 是Nmap自带的一个数据包处理工具,Nmap底层就是使用这个包进行收发包的,该库,是可以进行二次开发的,不过使用C语言开发费劲,在进行渗透任务时,还是使用Python构建数据包高效,唯一的 ...

  7. Data Encryption Standard算法:历经考验的经典加密方案

    在当今数字化时代,数据安全是一个至关重要的问题.为了保护敏感数据的机密性和完整性,加密算法成为了数据保护的关键技术.其中,DES(Data Encryption Standard)算法作为一种经典的对 ...

  8. DBGrideh使用小结【EditorMode】当用户点击单元格的时候,自动进入编辑状态

    (1)控制一个单元格是否允许编辑(或者说文字选中),可以设置Grid的Options的dgEditing属性:如果该属性为False,那么用户只能选中该单元格而无法选中里面的内容,也无法进行编辑.(2 ...

  9. .NET 云原生架构师训练营(模块二 基础巩固 日志)--学习笔记

    2.2.2 核心模块--日志 ILogger 的使用 日志的 ID 日志的分类 日志的级别 LoggerProvider 日志的最佳实践 .NET Core 和 ASP.NET Core 中的日志记录 ...

  10. linux 搭建http文件服务器

    1.安装httpd服务 yum -y install httpd 2.修改需要访问的文件路径 vi /etc/httpd/conf/httpd.conf ##默认是/var/www/html目录下的文 ...