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实现主备模式高可用

相关内容

热门资讯

台风“红霞”成为今年以来登陆我... 根据中央气象台、广东省气象台消息,今年第12号台风“红霞”的中心已于今天(26日)3时50分前后在广...
业界热议AI安全护栏升级路径 ● 本报记者 郑萃颖 近年来,人工智能(AI)技术快速发展,相关安全问题也引发各界关注。接受中国证券...
RISC-V:当开源架构遇见智... 2026年,RISC-V正站在新的历史节点。智能体的崛起、数据中心算力范式的重构,RVA23、RIS...
网络电视不小心按成蓝屏了 1、重新检查并连接好电视机的信号源输入即可。2、检测电视机的信号源输入,如线路、接口松动,则重新连接...
问问海尔的冰箱款式怎么样 最佳回答 海尔冰箱的款式还是很多的,你可以去专卖店看一看,也可以去京东上或者是天猫上看一看,京东和天...
美的中央空调面板按了没反应 美的中央空调面板按了没反应可能有多种原因,以下是一些常见的解决方法:1. 检查电源:确保空调面板已接...
爆料称一款小阔直板新机定档明年... 继华为陆续推出两款阔折叠手机产品之后,网间曾曝光过关于 @OPPO 将推出自家首款阔直板机的消息。不...
中央空调按了开关没有反应是怎么... 原因可能是因为中央空调在使用前忘记通电或者连接空调的电路老化,接触不良,可以尝试将电源通电或者更换新...
美的数控热水器开关按了没反应 先看看是否电源出现问题而导致美帝数控热水器没有通电,按了开关才会没反应。如果家里的电源有电,那么就可...
华为领衔“超节点”,这些跨界公... 文丨惠凯 编辑丨承承 算力竞争已进入“超节点”时代。在此背景下,液冷需求激增,芯片企业加速IPO,制...