In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Kubernetes Nginx configuration hot loading process is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Preface
Nginx itself supports hot updates. Through the nginx-s reload instruction, you can actually reload the configuration without stopping service by sending a HUB signal to the process. However, in Docker or Kubernetes, you need to enter the container to execute the nginx-s reload instruction every time. A single docker container can also say that you can execute this instruction outside the exec designated container for hot loading. Kubernetes, it is more difficult.
Today, we will introduce how to handle the hot loading configuration of Nginx in Kubernetes-- reloader.
Reloader address: https://github.com/stakater/Reloader
Reloader is mainly used to monitor the changes of ConfigMap or Secret, and then perform rolling upgrades to Deployment and DaemonSet of related DeploymentConfig.
Reloader needs a version above kubernetes1.9 to support it
Usage
The first step is to install and deploy reloader
# deploy kubectl apply-f https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml directly through the official yaml file
By default, reloader is deployed in the default namespace, but it is the configmaps and secrets that monitor all namespaces
Of course, if you don't want to monitor a configmap or secret, you can ignore a resource through-- resources-to-ignore=configMaps/secrets
After the deployment is successful, you can use it directly. I deployed nginx and configmap in advance.
This is the current configuration. Take a look at the current configuration of Nginx.
Next, I modify the Deployment of Nginx, add reloader, listen to the ConfigMap of nginx-config, and execute 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 App" # mainly this line "reloader.stakater.com/reload": "nginx-config"} "spec": {"replicas": 1, "selector": {"matchLabels": {"k8s-app": "nginx"}.
Then apply the Deployment, and then we update the ConfigMap, update the nginx configuration file
Update is complete, remove proxy_redirect, and then go to see if the nginx container performs a rolling update.
As you can see, nginx performs a rolling update, and then check to see if the nginx configuration file is updated
In this way, the configuration hot loading of Nginx can be easily realized through reloader.
In addition to this method, the common method is to use sidecar. If you do it through sidecar, you need to write your own listening script, which is troublesome, but sometimes it is relatively flexible. Here is also a python script for sidecar.
#! / usr/bin/env python#-*-encoding: utf8-* "" requirements: nginx configuration file changes, automatically update configuration files, similar to nginx-s reload implementation: 1, real-time monitoring of nginx configuration file changes with pyinotify 2, if configuration file changes Send HUP to the system to reload nginx "import osimport reimport pyinotifyimport loggingfrom threading import Timer# ParamLOG_PATH =" / root/python/log "CONF_PATHS = [" / etc/nginx " ] DELAY = 5SUDO = FalseRELOAD_COMMAND = "nginx-s reload" if SUDO: RELOAD_COMMAND = "sudo" + RELOAD_COMMAND# Loglogger = 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) # Reloaderdef 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_DELETEwatcher = 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) # Startlogger.info ("Start Monitoring") notifier.loop ()
If you like to use go, go scripts are also provided here.
Package mainimport ("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: = 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: =
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.