欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Kubernetes中Nginx配置热加载的全过程

程序员文章站 2022-03-02 22:53:20
目录前言使用方法总结前言nginx本身是支持热更新的,通过nginx -s reload指令,实际通过向进程发送hub信号实现不停服重新加载配置,然而在docker或者kubernetes中,每次都需...

前言

nginx本身是支持热更新的,通过nginx -s reload指令,实际通过向进程发送hub信号实现不停服重新加载配置,然而在docker或者kubernetes中,每次都需要进容器执行nginx -s reload指令,单docker容器还好说,可以在外面通过exec指定容器执行该指令进行热加载,kubernetes的话,就比较难受了

今天介绍一下kubernetes中nginx热加载配置的处理方法——reloader

reloader地址:https://github.com/stakater/reloader

reloader主要就是用来监测configmap或secret的变化,然后对相关deploymentconfig的deployment、daemonset执行滚动升级

reloader需要kubernetes1.9以上的版本才支持

使用方法

首先是安装部署reloader

# 直接通过官方yaml文件部署
kubectl apply -f https://raw.githubusercontent.com/stakater/reloader/master/deployments/kubernetes/reloader.yaml

默认情况下reloader是部署在default命名空间,但是它是监控所有命名空间的configmaps和secrets

当然,如果不想监控某个configmap或secret,可以通过--resources-to-ignore=configmaps/secrets来忽略某个资源

Kubernetes中Nginx配置热加载的全过程

部署成功后,就可以直接使用了,我提前部署了nginx和configmap

Kubernetes中Nginx配置热加载的全过程

这是目前的配置,看一下nginx目前的配置

Kubernetes中Nginx配置热加载的全过程

接着,我修改nginx的deployment,添加reloader,监听nginx-config这个configmap,执行reload

{
  "kind": "deployment",
  "apiversion": "extensions/v1beta1",
  "metadata": {
    "name": "nginx",
    "namespace": "default",
    "selflink": "/apis/extensions/v1beta1/namespaces/default/deployments/nginx",
    "uid": "7eee5fa8-7514-11ec-a916-0210d5e9ca3b",
    "resourceversion": "286141",
    "generation": 10,
    "creationtimestamp": "2022-01-14t08:32:23z",
    "labels": {
      "k8s-app": "nginx"
    },
    "annotations": {
      "deployment.kubernetes.io/revision": "9",
      "description": "nginx应用"
      # 主要是这行
      "reloader.stakater.com/reload": "nginx-config"
    }
  },
  "spec": {
    "replicas": 1,
    "selector": {
      "matchlabels": {
        "k8s-app": "nginx"
      }
    }
    ……

然后apply该deployment,之后我们去更新configmap,更新nginx配置文件

Kubernetes中Nginx配置热加载的全过程

更新完成,去掉proxy_redirect,然后去看nginx容器是否执行滚动更新

Kubernetes中Nginx配置热加载的全过程

可以看到,nginx执行了滚动更新,接着看下nginx配置文件是否更新

Kubernetes中Nginx配置热加载的全过程

这样很简单的通过reloader就可以实现nginx的配置热加载

除了这种方法,常见的方法还有使用sidecar,通过sidecar去做的话,需要自己写监听脚本,比较麻烦,但是有时候也相对灵活,这里也附一个sidecar的python脚本

#!/usr/bin/env python
# -*- encoding: utf8 -*-
"""
需求:nginx配置文件变化,自动更新配置文件,类似nginx -s reload
实现:
    1、用pyinotify实时监控nginx配置文件变化
    2、如果配置文件变化,给系统发送hup来reload nginx
"""
import os
import re
import pyinotify
import logging
from threading import timer

# param
log_path = "/root/python/log"
conf_paths = [
  "/etc/nginx",
]
delay = 5
sudo = false
reload_command = "nginx -s reload"
if sudo:
  reload_command = "sudo " + reload_command

# log
logger = logging.getlogger(__name__)
logger.setlevel(level = logging.info)
log_handler = logging.filehandler(log_path)
log_handler.setlevel(logging.info)
log_formatter = logging.formatter('%(asctime)s - %(levelname)s - %(message)s')
log_handler.setformatter(log_formatter)
logger.addhandler(log_handler)

# reloader
def reload_nginx():
  os.system(reload_command)
  logger.info("nginx is reloaded")

t = timer(delay, reload_nginx)

def trigger_reload_nginx(pathname, action):
  logger.info("nginx monitor is triggered because %s is %s" % (pathname, action))
  global t
  if t.is_alive():
    t.cancel()
    t = timer(delay, reload_nginx)
    t.start()
  else:
    t = timer(delay, reload_nginx)
    t.start()

events = pyinotify.in_modify | pyinotify.in_create | pyinotify.in_delete

watcher = pyinotify.watchmanager()
watcher.add_watch(conf_paths, events, rec=true, auto_add=true)

class eventhandler(pyinotify.processevent):
  def process_default(self, event):
    if event.name.endswith(".conf"):
      if event.mask == pyinotify.in_create:
        action = "created"
      if event.mask == pyinotify.in_modify:
        action = "modified"
      if event.mask == pyinotify.in_delete:
        action = "deleted"
      trigger_reload_nginx(event.pathname, action)

handler = eventhandler()
notifier = pyinotify.notifier(watcher, handler)

# start
logger.info("start monitoring")
notifier.loop()

如果喜欢用go的,这里也提供go脚本

package main

import (
    "log"
    "os"
    "path/filepath"
    "syscall"

    "github.com/fsnotify/fsnotify"
    proc "github.com/shirou/gopsutil/process"
)

const (
    nginxprocessname = "nginx"
    defaultnginxconfpath = "/etc/nginx"
    watchpathenvvarname = "watch_nginx_conf_path"
)

var stderrlogger = log.new(os.stderr, "error: ", log.lshortfile)
var stdoutlogger = log.new(os.stdout, "", log.lshortfile)

func getmasternginxpid() (int, error) {
    processes, processeserr := proc.processes()
    if processeserr != nil {
        return 0, processeserr
    }

    nginxprocesses := map[int32]int32{}

    for _, process := range processes {
        processname, processnameerr := process.name()
        if processnameerr != nil {
            return 0, processnameerr
        }

        if processname == nginxprocessname {
            ppid, ppiderr := process.ppid()

            if ppiderr != nil {
                return 0, ppiderr
            }

            nginxprocesses[process.pid] = ppid
        }
    }

    var masternginxpid int32

    for pid, ppid := range nginxprocesses {
        if ppid == 0 {
            masternginxpid = pid

            break
        }
    }

    stdoutlogger.println("found master nginx pid:", masternginxpid)

    return int(masternginxpid), nil
}

func signalnginxreload(pid int) error {
    stdoutlogger.printf("signaling master nginx process (pid: %d) -> sighup\n", pid)
    nginxprocess, nginxprocesserr := os.findprocess(pid)

    if nginxprocesserr != nil {
        return nginxprocesserr
    }

    return nginxprocess.signal(syscall.sighup)
}

func main() {
    watcher, watchererr := fsnotify.newwatcher()
    if watchererr != nil {
        stderrlogger.fatal(watchererr)
    }
    defer watcher.close()

    done := make(chan bool)
    go func() {
        for {
            select {
            case event, ok := <-watcher.events:
                if !ok {
                    return
                }

                if event.op&fsnotify.create == fsnotify.create {
                    if filepath.base(event.name) == "..data" {
                        stdoutlogger.println("config map updated")

                        nginxpid, nginxpiderr := getmasternginxpid()
                        if nginxpiderr != nil {
                            stderrlogger.printf("getting master nginx pid failed: %s", nginxpiderr.error())

                            continue
                        }

                        if err := signalnginxreload(nginxpid); err != nil {
                            stderrlogger.printf("signaling master nginx process failed: %s", err)
                        }
                    }
                }
            case err, ok := <-watcher.errors:
                if !ok {
                    return
                }
                stderrlogger.printf("received watcher.error: %s", err)
            }
        }
    }()

    pathtowatch, ok := os.lookupenv(watchpathenvvarname)
    if !ok {
        pathtowatch = defaultnginxconfpath
    }

    stdoutlogger.printf("adding path: `%s` to watch\n", pathtowatch)

    if err := watcher.add(pathtowatch); err != nil {
        stderrlogger.fatal(err)
    }
    <-done
}

ok,今天的内容就到这里

总结

到此这篇关于kubernetes中nginx配置热加载的文章就介绍到这了,更多相关kubernetes中nginx配置热加载内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!