k8s 部署nginx 实现集群统一配置,自动更新nginx.conf配置文件 总结
创始人
2025-05-31 06:34:08
0

k8s 部署nginx 实现集群统一配置,自动更新nginx.conf配置文件 总结

大纲

  • 1 nginx镜像选择
  • 2 创建configmap保存nginx配置文件
  • 3 使用inotify监控配置文件变化
  • 4 Dockerfile创建
  • 5 调整镜像原地址使用阿里云
  • 6 创建deploy部署文件部署nginx
  • 7 测试使用nginx配置文件同步&nginx自动重启

直接使用https://hub.docker.com/_/nginx nginx镜像有几个问题

  • 1 集群环境下需要手动的配置多个nginx.conf文件
  • 2 集群环境下配置文件修改后需要 kubectl exec -it 到多个pod重启nginx

使用k8s configmap统一配置集群下所有nginx的配置,并使用inotify监听配置文件变化后自动重启

nginx镜像选择

nginx镜像地址 https://hub.docker.com/_/nginx 使用 nginx:1.23.3 作为基础镜像

此镜像的配置文件为 /etc/nginx/nginx.conf 可以看到配置文件会include /etc/nginx/conf.d 文件夹下的配置
在这里插入图片描述

只需把此文件夹与configmap挂载就可以使用自己的配置信息了

创建configmap

创建一个configmap 用来保存nginx的配置文件

apiVersion: v1
kind: ConfigMap
metadata:name: nginx-config
data:nginx.conf: |server {listen 8080;charset utf-8;server_name  localhost; location / {root   /usr/share/nginx/html;index  index.html index.htm;}}

使用inotify监控配置文件变化

可以使用inotify 实现对配置文件夹的监控,当文件夹内有.conf文件创建,修改,删除后重新启动nginx

可以创建一个脚本,此脚本监控 /etc/nginx/conf.d 下文件的变化

#!/bin/bash
configfile='.conf$'#监听文件夹修改,删除事件
inotifywait -e modify,delete -mr --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f %e'  /etc/nginx/conf.d | while read day time folder file event;
do#判断变化的文件是否是.conf结尾的文件 注意正则判断需要使用[[]]if [[ $file =~ $configfile ]]; thennginx -t# $?返回上一个命令的结束状态 0表示正常if [ $? == 0 ]; thennginx -s reloadfifi   	
done	

再准备一个启动start.sh脚本用于启动nginx以及inotify监控

echo "start nginx"
# 启动nginx
nginx
# 启动监控 需要保持一个前台运行的程序 否则容器运行后就退出
./auto_reload.sh

inotify的使用可以参考 《linux-inotify工具监控文件状态变化总结》

Dockerfile创建

Dockerfile 内容如下,可以调整linux镜像源使用阿里云的镜像源

FROM nginx:1.23.3
VOLUME ["/data/service/logs","/docker/tmp","/data/service/store"] 
WORKDIR "/data/service"
LABEL base.name="nginx-auto-reload" 
LABEL base.desc="nginx-auto-reload"
#修改操作系统源地址 使用阿里云 可以不修改,但是由于网络原因会比较满
#注意 nginx:1.23.3 镜像使用的是debian 11.x (bullseye)
#需要使用对应的阿里云 镜像源 https://developer.aliyun.com/mirror/debian?spm=a2c6h.13651102.0.0.3e221b11W40Fzd
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >/etc/apt/sources.list
RUN echo "deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >>/etc/apt/sources.list
RUN echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >>/etc/apt/sources.list
RUN echo "deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main" >>/etc/apt/sources.list
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >>/etc/apt/sources.list
RUN echo "deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >>/etc/apt/sources.list
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >>/etc/apt/sources.list
RUN echo "deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >>/etc/apt/sources.list
RUN apt-get update
RUN apt-get install inotify-tools -y
ADD auto_reload.sh auto_reload.sh
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY ["auto_reload.sh","start.sh","./"]
RUN chmod 711 auto_reload.sh && chmod 711 start.sh 
CMD ["./start.sh"]

需要使用对应的阿里云 镜像源 https://developer.aliyun.com/mirror/debian?spm=a2c6h.13651102.0.0.3e221b11W40Fzd

在这里插入图片描述

创建镜像后推送到阿里云私库,用于后续的使用

docker build -t nginx-auto-reload .
docker tag nginx-auto-reload registry.cn-hangzhou.aliyuncs.com/jimliu/nginx-auto-reload
docker push registry.cn-hangzhou.aliyuncs.com/jimliu/nginx-auto-reload

在这里插入图片描述

创建deploy部署文件部署nginx

部署deploy.yaml 内容如下

apiVersion: apps/v1
kind: Deployment
metadata: name: nginx-deployment
spec:replicas: 1selector:matchLabels:app: nginx-auto-reloadtemplate:metadata:labels:app: nginx-auto-reloadspec:# 容器配置       imagePullSecrets:- name: myaliyunsecrethostname: nginx-hostsubdomain: nginx-inner-domaincontainers:- image: registry.cn-hangzhou.aliyuncs.com/jimliu/nginx-auto-reload:latestname: nginx-containers# 挂载文件夹volumeMounts:- mountPath: "/etc/nginx/conf.d/"name: config-volumevolumes:- name: config-volumeconfigMap:name: nginx-config---
# 外部访问的接口 
apiVersion: v1
kind: Service
metadata:  name: nginx-auto-reload-service  
spec:ports:- protocol: TCPport: 18080targetPort: 8080nodePort: 18080name: http8080#暴露两个接口用于测试 nginx重启- protocol: TCPport: 18081targetPort: 8081nodePort: 18081  name: http8081selector:  app: nginx-auto-reload

部署nginx并测试

创建configmap

在这里插入图片描述

部署nginx

kubectl apply -f n-deployment.yaml 

在这里插入图片描述

此步 nginx部署完成 service创建成功

测试nginx

在这里插入图片描述

8080端口访问成功

在这里插入图片描述

8081端口还无法访问

在这里插入图片描述

修改configmap中nginx配置文件 开放8081端口

在这里插入图片描述

等待configmap同步更新nginx pod中的配置文件

在这里插入图片描述
在这里插入图片描述

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
JAVA多线程知识整理 Java多线程基础 线程的创建和启动 继承Thread类来创建并启动 自定义Thread类的子类&#...
【洛谷 P1090】[NOIP... [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G ...
国民技术LPUART介绍 低功耗通用异步接收器(LPUART) 简介 低功耗通用异步收发器...
城乡供水一体化平台-助力乡村振... 城乡供水一体化管理系统建设方案 城乡供水一体化管理系统是运用云计算、大数据等信息化手段࿰...
程序的循环结构和random库...   第三个参数就是步长     引入文件时记得指明字符格式,否则读入不了 ...
中国版ChatGPT在哪些方面... 目录 一、中国巨大的市场需求 二、中国企业加速创新 三、中国的人工智能发展 四、企业愿景的推进 五、...
报名开启 | 共赴一场 Flu... 2023 年 1 月 25 日,Flutter Forward 大会在肯尼亚首都内罗毕...
汇编00-MASM 和 Vis... Qt源码解析 索引 汇编逆向--- MASM 和 Visual Studio入门 前提知识ÿ...
【简陋Web应用3】实现人脸比... 文章目录🍉 前情提要🌷 效果演示🥝 实现过程1. u...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
windows安装JDK步骤 一、 下载JDK安装包 下载地址:https://www.oracle.com/jav...
分治法实现合并排序(归并排序)... 🎊【数据结构与算法】专题正在持续更新中,各种数据结构的创建原理与运用✨...
在linux上安装配置node... 目录前言1,关于nodejs2,配置环境变量3,总结 前言...
Linux学习之端口、网络协议... 端口:设备与外界通讯交流的出口 网络协议:   网络协议是指计算机通信网...
Linux内核进程管理并发同步... 并发同步并发 是指在某一时间段内能够处理多个任务的能力,而 并行 是指同一时间能够处理...
opencv学习-HOG LO... 目录1. HOG(Histogram of Oriented Gradients,方向梯度直方图)1...
EEG微状态的功能意义 导读大脑的瞬时全局功能状态反映在其电场结构上。聚类分析方法一致地提取了四种头表面脑电场结构ÿ...
【Unity 手写PBR】Bu... 写在前面 前期积累: GAMES101作业7提高-实现微表面模型你需要了解的知识 【技...