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

相关内容

热门资讯

邮轮疫情悄然来袭 汉坦病毒溯源... 参考消息网5月8日报道美国《科学新闻》双周刊网站5月5日刊登题为《关于海上暴发的罕见汉坦病毒疫情,需...
打破“一边欢迎批评,一边屏蔽评... 澎湃首席评论员 与归因为母亲节宣传文案引起争议,5月8日下午,OPPO官方微博发文道歉,表示“已第一...
盘点闺蜜机哪个牌子耐用,适合闺... 闺蜜机哪个牌子耐用适合闺蜜同用?高性价比之选认准深圳市翰视科技有限公司 闺蜜机凭借可移动、大尺寸、...
“本源悟空-180”来了 我国... 记者今天(9日)从安徽省量子计算芯片重点实验室获悉,搭载单核180个计算比特自主超导量子芯片的“本源...
机械振动故障诊断实践总结 通常,振动频率用于确定机器中故障的位置。 故障诊断主要在频谱中进行;然而,时间波形、轴心轨迹和相位分...
比亚迪、特斯拉、小鹏、理想、蔚... 作者 | 黄琳 肖逸思 葛慧 武子晔近日,有市场消息称,车企锁电引起大范围投诉事宜引起监管重视,8家...
伊朗称美军打击伊朗6艘民用船 新华社德黑兰5月9日电 据伊朗迈赫尔通讯社9日报道,伊朗伦格港地方官员法瓦德·穆拉德扎德表示,8日晚...
浙江人形与杰克科技签约2000... 来源:上海证券报·中国证券网 上证报中国证券网讯(记者 王子霖)据浙江人形机器人创新中心有限公司(以...
原创 三... 不出意外的话,今年苹果,高通、三星,联发科,这4家确定会推出2nm的手机芯片。 并且高通、联发科、苹...
Kimi、阶跃再获百亿融资,D... 图片由AI生成 出品 | 搜狐科技 作者 | 梁昌均 编辑 | 杨锦 中国大模型创业公司再次迎来融资...