Nginx +keepalived

时间:2022-12-14 13:22:17

Nginx +keepalived

 

配置高可用的Nginx

准备环境:

节点node17,node18

 

lftp 172.16.0.1:/pub/Sources/6.x86_64/nginx

 

[root@node17 ~]# rpm -ivh nginx-1.6.2-1.el6.ngx.x86_64.rpm

 

[root@node17 ~]# scp -rp nginx-1.6.2-1.el6.ngx.x86_64.rpm node18:/root

 

[root@node18 ~]# rpm -ivh nginx-1.6.2-1.el6.ngx.x86_64.rpm

 

为两个 节点上都配置默认的网页,来以示区别

 

[root@node17 html]# vim index.html

<h1>node15.stu21.com</h1>

 

[root@node18 html]# vim index.html

<h1>node2.stu21.com </h1>

 

接下来启动两个 节点上的nginx 服务:

 

[root@node17 html]# service nginx start;ssh node18 'service nginx start';

 

测试:

 

Nginx +keepalived

 

Nginx +keepalived

 

 

 

 

 

 

 


题:我们一般进行主从切换测试时都是关闭keepalived或关闭网卡接口,有没有一种方法能实现在不关闭keepalived下或网卡接口来实现维护
呢?以及监测nginx的状态??
方法肯定是有的,在keepalived新版本中,支持脚本vrrp_srcipt,具体如何使用大家可以man keepalived.conf查看。下面我们来演示一下具体怎么实现。

 

    vrrp_srcipt chk_schedown { 

   script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"  

   interval 1 #监控间隔  

   weight -5 #减小优先级  

   fall 2 #监控失败次数  

   rise 1 #监控成功次数  

}

    vrrp_script chk_nginx {

        script "killall -0 nginx"

        interval 1

        weight -5

        fall 2

        rise 1

}

 

(2).执行脚本

    track_script {

   chk_schedown #执行chk_schedown脚本

     chk_nginx #执行chk_nginx脚本

}

 

 

node17:我就继续沿用apache+lvs+keepalived的节点node17上keepalived相关配置,稍作修改

[root@node17 ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

    root@node17.stu21.com

    root@node18.stu21.com #配置管理员邮箱

}

notification_email_from kaadmin@stu21.com #配置发件人

smtp_server 127.0.0.1 #配置邮件服务器

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_schedown { #定义vrrp执行脚本

script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" #查看是否有down文件,有就进入维护模式

interval 1 #监控间隔时间

weight -5 #降低优先级

fall 2 #失败次数

rise 1 #成功数次

}

    vrrp_script chk_nginx {

        script "killall -0 nginx"

        interval 1

        weight -5

        fall 2

        rise 1

}

 

vrrp_instance VI_1 {

state MASTER #配置模式

#state BACKUP #测试配置模式

interface eth0

virtual_router_id 51

priority 100 #配置优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.21.100 #配置虚拟IP地址

}

track_script { #执行脚本

chk_schedown

    chk_nginx #执行chk_nginx脚本

}

#增加以下三行

notify_master "/etc/keepalived/notify.sh -n master -a 172.16.21.100"

notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.21.100"

notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.21.100"

}

 

vrrp_instance VI_2 {

state BACKUP #配置模式 #修改为BACKUP

#state MASTER #测试配置模式

interface eth0

virtual_router_id 52

 

priority 99 #配置优先级 #修改优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.21.101 #配置虚拟IP地址

}

track_script {

chk_schedown

chk_nginx #执行chk_nginx脚本

}

#增加以下三行

notify_master "/etc/keepalived/notify.sh -n master -a 172.16.21.101"

notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.21.101"

notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.21.101"

}

 

 

#virtual_server 172.16.21.100 80 {

# delay_loop 6

# lb_algo rr

# lb_kind DR

# nat_mask 255.255.255.0

# #persistence_timeout 50

# protocol TCP

#

# real_server 172.16.21.15 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

#     status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# real_server 172.16.21.16 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# sorry_server 127.0.0.1 80 #增加一行sorry_server

#}

#

#

#virtual_server 172.16.21.101 80 {

# delay_loop 6

# lb_algo rr

# lb_kind DR

# nat_mask 255.255.255.0

# #persistence_timeout 50

# protocol TCP

#

# real_server 172.16.21.15 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# real_server 172.16.21.16 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# sorry_server 127.0.0.1 80 #增加一行sorry_server

#}

#

 

#virtual_server#10.10.10.2 1358 {

# delay_loop 6

# lb_algo rr

# lb_kind NAT

# persistence_timeout 50

# protocol TCP

 

# sorry_server 192.168.200.200 1358

 

# real_server 192.168.200.2 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

 

# real_server 192.168.200.3 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334c

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334c

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

#}

 

#virtual_server#10.10.10.3 1358 {

# delay_loop 3

# lb_algo rr

# lb_kind NAT

# nat_mask 255.255.255.0

# persistence_timeout 50

# protocol TCP

 

# real_server 192.168.200.4 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

 

# real_server 192.168.200.5 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

#}

 

node18:同样也是沿用apache+lvs+keepalived的节点node17上keepalived相关配置,稍作修改,着色说明需要加上

 

[root@node18 keepalived]# cat keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

    root@node17.stu21.com

    root@node18.stu21.com #配置管理员邮箱

}

notification_email_from kaadmin@stu21.com #配置发件人

smtp_server 127.0.0.1 #配置邮件服务器

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_schedown {

script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"

interval 1

weight -5

fall 2

rise 1

}

    vrrp_script chk_nginx {

        script "killall -0 nginx"

        interval 1

        weight -5

        fall 2

        rise 1

}

 

vrrp_instance VI_1 {

state BACKUP #配置模式 #修改为BACKUP

# state MASTER #测试配置模式

interface eth0

virtual_router_id 51

priority 99 #配置优先级 #修改优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.21.100 #配置虚拟IP地址

}

track_script {

chk_schedown

chk_nginx #执行chk_nginx脚本

}

#增加以下三行

notify_master "/etc/keepalived/notify.sh -n master -a 172.16.21.100"

notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.21.100"

notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.21.100"

}

 

vrrp_instance VI_2 {

#state BACKUP #配置模式 #修改为BACKUP

state MASTER #测试配置模式

interface eth0

virtual_router_id 52

 

priority 101 #配置优先级 #修改优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.21.101 #配置虚拟IP地址

}

track_script {

chk_schedown

chk_nginx #执行chk_nginx脚本

}

#增加以下三行

notify_master "/etc/keepalived/notify.sh -n master -a 172.16.21.101"

notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.21.101"

notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.21.101"

}

 

 

#virtual_server 172.16.21.100 80 {

# delay_loop 6

# lb_algo rr

# lb_kind DR

# nat_mask 255.255.255.0

# #persistence_timeout 50

# protocol TCP

#

# real_server 172.16.21.15 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

#     status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# real_server 172.16.21.16 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# sorry_server 127.0.0.1 80 #增加一行sorry_server

#}

#

#virtual_server 172.16.21.101 80 {

# delay_loop 6

# lb_algo rr

# lb_kind DR

# nat_mask 255.255.255.0

# #persistence_timeout 50

# protocol TCP

#

# real_server 172.16.21.15 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# real_server 172.16.21.16 80 { #配置realaserver

# weight 1

# HTTP_GET {#监控配置

# url {

# path /

# #digest ff20ad2481f97b1754ef3e12ecd3a9cc

# status_code 200

# }

#

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 1

# }

# }

# sorry_server 127.0.0.1 80 #增加一行sorry_server

#}

#

 

#virtual_server#10.10.10.2 1358 {

# delay_loop 6

# lb_algo rr

# lb_kind NAT

# persistence_timeout 50

# protocol TCP

 

# sorry_server 192.168.200.200 1358

 

# real_server 192.168.200.2 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

 

# real_server 192.168.200.3 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334c

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334c

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

#}

 

#virtual_server#10.10.10.3 1358 {

# delay_loop 3

# lb_algo rr

# lb_kind NAT

# nat_mask 255.255.255.0

# persistence_timeout 50

# protocol TCP

 

# real_server 192.168.200.4 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

 

# real_server 192.168.200.5 1358 {

# weight 1

# HTTP_GET {

# url {

# path /testurl/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl2/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# url {

# path /testurl3/test.jsp

# digest 640205b7b0fc66c1ea91c463fac6334d

# }

# connect_timeout 3

# nb_get_retry 3

# delay_before_retry 3

# }

# }

#}