21機器是vip,1八、19兩臺機器主備。nginx
keepalived和nginx的經驗少,後續繼續分享。api
[root[@test](https://my.oschina.net/azibug) keepalived]# more keepalived.conf ! Configuration File for keepalived global_defs { router_id test1 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx_pid.sh" interval 5 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 110 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 1.13.22.21 } track_script { chk_nginx } } [root[@test](https://my.oschina.net/azibug) keepalived]#
與18基本同樣,除了:bash
[root[@test2](https://my.oschina.net/u/1253032) keepalived]# more keepalived.conf ! Configuration File for keepalived global_defs { router_id test2 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx_pid.sh" interval 5 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 1.13.22.21 } }
[root[@test](https://my.oschina.net/azibug) nginx]# more nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; #gzip on; upstream upstream_nginx_zuul { server 1.13.22.18:8181; server 1.13.22.19:8181; } server { listen 80; location / { proxy_set_header Host $host:$server_port; proxy_pass http://upstream_nginx_zuul; } } include /etc/nginx/conf.d/*.conf; } [root@test1 nginx]#
和18配置徹底同樣。app
[root@test nginx]# more nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; #gzip on; upstream upstream_nginx_zuul { server 1.13.22.18:8181; server 1.13.22.19:8181; } server { listen 80; location / { proxy_set_header Host $host:$server_port; proxy_pass http://upstream_nginx_zuul; } } include /etc/nginx/conf.d/*.conf; }
兩臺機器同樣。負載均衡
[root@test2 keepalived]# more check_nginx_pid.sh #!/bin/bash LOG_DIR="/etc/keepalived" echo $(date "+%Y-%m-%d %H:%M:%S") "check nginx status" >> $LOG_DIR/log_nginx.log A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /usr/sbin/nginx #重啓nginx echo $(date "+%Y-%m-%d %H:%M:%S") "restarting nginx..." >> $LOG_DIR/restart.log sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重啓失敗,則停掉keepalived服務,進行VIP轉移 #killall keepalived echo $(date "+%Y-%m-%d %H:%M:%S") "killing keepalived..." >> $LOG_DIR/restart.log fi fi