Suppose we have a scenario in which all servers share the same configuration file, we can't maintain each server manually. At this time, we can use the configuration management function of zookeeper.
Environment: python + nginx + zookeeper
Objective: when the configuration file in zookeeper changes, nginx automatically pulls the latest configuration file and applies it to the local area. Restart the server in the middle:
One. Build a zookeeper cluster:
For details, please refer to: http://zookeeper.apache.org/doc/r3.4.13/zookeeper started.html
Make sure the zookeeper cluster is healthy:
Two. Install nginx
yum install nginx -y
Start nginx: systemctl start nginx
Three. Write the python zookeeper client:
Objective: to connect the zookeeper cluster regularly and detect the change of configuration file:
from kazoo.client import KazooClient import time zk=KazooClient(hosts='192.168.85.137:2181') zk.start() Version=None while True: @zk.DataWatch("/nginx") def watch_node(data, stat): global Version if Version == None: Version=stat if Version != stat: Version = stat nginx_file=str(data,encoding='utf-8') print("Configuration changed!!!!!") f=open('nginx.conf','w',encoding='utf-8') f.write(nginx_file) f.flush() f.close() import os Path=os.path.dirname(os.path.abspath(__file__)) os.system('cp -f %s/nginx.conf /etc/nginx/nginx.conf && systemctl restart nginx.service'%Path) #Replace the nginx configuration file, and then restart the service. Note that this is just a general framework. The production environment must not be restarted directly. You can write some judgments. At least you should make sure that the configuration file is free of errors before restarting nginx time.sleep(3)
Test: modify nginx configuration in zookeeper to see if the client can pull the configuration file:
from kazoo.client import KazooClient import time zk=KazooClient(hosts='192.168.85.137:2181') zk.start() nginx_config=""" user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 5555 default_server; index index.php index.html; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } """ zk.set('/nginx',bytes(nginx_config,encoding='utf-8'))
success!!!