用 inotifywatch 监测挂载到 Pod 中到 configmap 的变化,触发 nginx 配置文件的热加载。
$ yum install -y epel-release && yum search inotify-tools && yum install -y inotify-tools
$ inotifywatch -e modify,create,delete,move -r -t 15 /mnt/webserver/conf 2>&1
Establishing watches...
-r
:同时监控子目录
-t 15
: 15 秒后退出,并打印结果的意思。
watch 期间,在 /mnt/webserver/conf/ 中随意创建一个文件,观测到期后,打印下面的结果:
Finished establishing watches, now collecting statistics.
total create filename
1 1 /mnt/webserver/conf/
如果没有任何变化,打印下面的结果:
$ inotifywatch -e modify,create,delete,move -r -t 1 /mnt/webserver/conf 2>&1
Establishing watches...
Finished establishing watches, now collecting statistics.
No events occurred.
所以要判断目录的内容是否发生了变化,可以通过观察结果中是否包含 filename
字符,如下:
while true; do
echo "watch start"
if [[ "$(inotifywatch -e modify,create,delete,move -r -t 5 /mnt/webserver/conf 2>&1)" =~ filename ]]; then
echo "config changed"
fi;
done
entrypoint 可以用下面的文件:
./watch.sh &
$NGINX_BIN -g "daemon off;"
watch.sh 监测文件变化,并触发更新:
while true; do
if [[ "$(inotifywatch -e modify,create,delete,move -r -t 10 /mnt/webserver/conf 2>&1)" =~ filename ]]; then
echo "Try to verify updated nginx config..."
$NGINX_BIN -t
if [ $? -ne 0 ]; then
echo "ERROR: New configuration is invalid!!"
else
echo "Reloading nginx with new config..."
$NGINX_BIN -s reload
fi
fi
done