Keepalived实现Nginx与LVS高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived实现Nginx与LVS高可用相关的知识,希望对你有一定的参考价值。
在192.168.1.10和192.168.1.20分别安装:
#yum –y install keepalived
192.168.1.10的配置文件内容:
#vim keepalived.conf
! Configuration File for keepalived global_defs { notification_email { } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "pidof nginx " interval 1 weight -3 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master " notify_backup "/etc/keepalived/notify.sh backup " notify_fault "/etc/keepalived/notify.sh fault " } |
192.168.1.20的配置文件内容:
#vim keepalived.conf
! Configuration File for keepalived global_defs { notification_email { } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "pidof nginx " interval 1 weight -1 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master " notify_backup "/etc/keepalived/notify.sh backup " notify_fault "/etc/keepalived/notify.sh fault " } |
主备服务器上的脚本内容:
#vim /etc/keepalived/notify.sh notify.sh
#!/bin/bash case "$1" in master) /etc/init.d/nginx start exit 0 ;; backup) /etc/init.d/nginx stop exit 0 ;; fault) /etc/init.d/nginx stop exit 0 ;; *) echo ‘Usage: `basename $0` {master|backup|fault}‘ exit 1 ;; esac |
主备服务器上LVS配置内容:
virtual_server 192.168.1.100 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 192.168.1.30 80 { weight 1 HTTP_GET { url { path /demo.php status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.1.40 80 { weight 1 HTTP_GET { url { path /demo.php status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
在nginx1和nginx2上分别运行一下命令:
ip addr add 192.168.1.100/32 label lo:0 brd 192.168.1.100 dev lo
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
以上是关于Keepalived实现Nginx与LVS高可用的主要内容,如果未能解决你的问题,请参考以下文章
企业级Nginx负载均衡与keepalived高可用实战keepalived篇