nginx+keepalived实现主备模式高可用
admin
2023-03-11 02:21:04
0

一、拓扑
nginx+keepalived实现主备模式高可用
二、keepalived配置
2.1 主配置
! Configuration File for keepalived

global_defs {
notification_email {acassen@firewall.loc
br/>acassen@firewall.loc
br/>sysadmin@firewall.loc
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_V1
}

vrrp_script chk_nginx {
script "/usr/local/keepalived/check_nginx.sh"
interval 2
weight 2
}

vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass wenlf@780419
}
track_script {
chk_nginx
}
virtual_ipaddress {
172.16.10.65 dev ens33
}
notify_master "/usr/local/keepalived/clean_arp.sh 172.16.10.65"
}
2.2 备配置
! Configuration File for keepalived

global_defs {
notification_email {acassen@firewall.loc
br/>acassen@firewall.loc
br/>sysadmin@firewall.loc
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_V1
}

vrrp_script chk_nginx {
script "/usr/local/keepalived/check_nginx.sh"
interval 2
weight 2
}

vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass wenlf@780419
}
track_script {
chk_nginx
}
virtual_ipaddress {
172.16.10.65 dev ens33
}
notify_master "/usr/local/keepalived/clean_arp.sh 172.16.10.65"
2.3 监控脚本

----------------------------------------clean_arp.sh------------------------------------
#!/bin/sh
VIP=$1
GATEWAY=172.16.10.254
/sbin/arping -I ens33 -c 5 -s $VIP $GATEWAY &>/dev/null

-----------------------------------------check_nginx.sh--------------------------------
#!/bin/sh
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
/etc/rc.d/init.d/nginx start
fi
sleep 2
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
/etc/rc.d/init.d/keepalived stop
fi
三、nginx配置
3.1 nginx.conf
user www www;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;

pid logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
worker_connections 1024;
use epoll;

}

http {

include LoadBalance/*.conf;
include mime.types;
default_type application/octet-stream;
#默认编码
#charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on;
autoindex on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip模块设置
gzip on; 
gzip_min_length 1k;    
gzip_buffers 4 16k;   
gzip_http_version 1.0;    
gzip_comp_level 2;    
gzip_types text/plain application/x-javascript text/css application/xml;    
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#include LoadBalance/*.conf;
include vhost/*.conf;

}
3.2 虚拟机
server
{
listen 80;
server_name 172.16.10.65;
#index index.html index.htm index.php;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://LoadBalance;
#proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
#auth_basic_user_file pwd;
}
}
3.3 负载均衡配置
upstream LoadBalance{
server 192.168.10.70:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.10.71:80 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;

}
四、验证

nginx+keepalived实现主备模式高可用

nginx+keepalived实现主备模式高可用

相关内容

热门资讯

玩家分享攻略“麦穗app推筒子... 网上科普关于“麦穗app推筒子有没有挂”话题很是火热,小编也是针对麦穗app推筒子作*弊开挂的方法以...
最新引进“喜扣打筒子.怎么开挂... 网上科普关于“喜扣打筒子有没有挂”话题很是火热,小编也是针对喜扣打筒子作*弊开挂的方法以及开挂对应的...
今日重大消息“天天福建十三张.... 您好:天天福建十三张这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这...
【第一资讯】“三鑫联盟.是不是... 网上科普关于“三鑫联盟有没有挂”话题很是火热,小编也是针对三鑫联盟作*弊开挂的方法以及开挂对应的知识...
玩家最新攻略“摇钱树拼三张.有... 您好:摇钱树拼三张这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款...
今日重大消息“新毛豆炸金花.有... 有 亲,根据资深记者爆料新毛豆炸金花是可以开挂的,确实有挂(咨询软件无需...
终于明白“老友内蒙古麻将.有挂... 网上科普关于“老友内蒙古麻将有没有挂”话题很是火热,小编也是针对老友内蒙古麻将作*弊开挂的方法以及开...
今日重磅消息“乐享牛牛.是不是... 您好:乐享牛牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
重磅消息“酉阳大贰.怎么开挂?... 网上科普关于“酉阳大贰有没有挂”话题很是火热,小编也是针对酉阳大贰作*弊开挂的方法以及开挂对应的知识...
玩家分享攻略“越乡游义乌麻将.... 有 亲,根据资深记者爆料越乡游义乌麻将是可以开挂的,确实有挂(咨询软件无...