nginx + keepalived hot standby

For the medium and small enterprises, if not the money to buy expensive four / seven load balancing switches, then Nginx is a good seven load balancing options, and can achieve double the load balancer Nginx via Nginx + Keepalived mutual support, any one machine fails, the other can take over the virtual IP in the past. The following is the third update, the following detailed description of its installation steps below:

First, install nginx, has been installed on a step, see the article

Second, install keepalived

keepalived Download: http: //www.keepalived.org/software/
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-12.2. / configure –prefix = / usr / local / keepalived
make
make install
cp / usr / local / keepalived / sbin / keepalived / usr / sbin /
cp / usr / local / keepalived / etc / sysconfig / keepalived / etc / sysconfig /
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir / etc / keepalived
cd / etc / keepalived /

The main configuration file

vim keepalived.conf

 

  1. global_defs {        ####全局定义
  2.    notification_email {        #####指定keepalived在发生事件时(比如切换),需要发送的email对象,可以多个,每行一个
  3.     123@qq.com
  4.    }
  5.    notification_email_from 123@qq.com
  6.    smtp_server 127.0.0.1          #####指定发送email的smtp服务器
  7.    smtp_connect_timeout 30
  8.    router_id LVS_DEVEL          #######运行keepalived的机器的一个标识
  9. }
  10. vrrp_sync_group VG1 {
  11.   group {
  12.     VI_1
  13.     VI_2
  14.    }
  15. }
  16. vrrp_script chk_nginx {
  17.         script    “/etc/chk_nginx.sh”     ###监控脚本
  18.         interval 2      ###监控时间
  19.         #weight -2
  20. }
  21. vrrp_instance VI_1 {
  22.     state MASTER
  23.     interface eth0
  24. #    track_interface {
  25. #       eth0
  26. #       eth1
  27. #   }
  28.     virtual_router_id 51      #VRID标记 master和backup必须一致
  29.     mcast_src_ip 192.168.2.113
  30.     #发送多播包的地址,如果不设置默认使用绑定的网卡的primary ip
  31.     priority 150
  32.     advert_int 1
  33.     authentication {
  34.         auth_type PASS
  35.         auth_pass 1111
  36.     }
  37.    track_script {
  38.         chk_nginx         ### 就是上面vrrp_script后面的实例名称   执行监控的服务
  39.     }
  40.     virtual_ipaddress {
  41.        192.168.2.116
  42.         }
  43. }
  44. vrrp_instance VI_2 {
  45.     state BACKUP
  46.     interface eth0
  47.     tual_router_id 52
  48.     mcast_src_ip 192.168.100.113
  49.     priority 149
  50.     advert_int 1
  51.     authentication {
  52.         auth_type PASS
  53.         auth_pass 1111
  54.     }
  55. #    track_interface {
  56. #       eth0
  57. #       eth1
  58. #    }
  59.     virtual_ipaddress {
  60.        192.168.100.117
  61.         }
  62. }

 

另一主的配置文件

 

 

  1. global_defs { ####全局定义
  2. notification_email { #####Keepalived specified when an event occurs (such as switching), email subject you want to send, can more than one, one per line
  3. 123.com
  4. }
  5. notification_email_from 123@qq.com
  6. smtp_server 127.0.0.1 #####指定发送email的smtp服务器
  7. smtp_connect_timeout 30
  8. router_id LVS_DEVEL #######运行keepalived的机器的一个标识
  9. }
  10. vrrp_sync_group VG1 {
  11. group {
  12. VI_1
  13. VI_2
  14. }
  15. }
  16. vrrp_script chk_nginx {
  17. script “/etc/chk_nginx.sh” ###监控脚本
  18. interval 2 ###监控时间
  19. #weight -2
  20. }
  21. vrrp_instance VI_1 {
  22. state BACKUP
  23. interface eth0
  24. # track_interface {
  25. # eth0
  26. # eth1
  27. # }
  28. virtual_router_id 51 #VRID标记 master和backup必须一致
  29. mcast_src_ip 192.168.2.112
  30. #发送多播包的地址,如果不设置默认使用绑定的网卡的primary ip
  31. priority 149
  32. advert_int 1
  33. authentication {
  34. auth_type PASS
  35. auth_pass 1111
  36. }
  37. virtual_ipaddress {
  38. 192.168.2.116
  39. }
  40. }
  41. vrrp_instance VI_2 {
  42. state MASTER
  43. interface eth0
  44. tual_router_id 52
  45. mcast_src_ip 192.168.100.112
  46. priority 150
  47. advert_int 1
  48. authentication {
  49. auth_type PASS
  50. auth_pass 1111
  51. }
  52. track_script {
  53. chk_nginx ### The above is an example of the name behind vrrp_script execution monitoring service
  54. }
  55. # track_interface {
  56. # eth0
  57. # eth1
  58. }
  59. virtual_ipaddress {
  60. 192.168.100.117
  61. }
  62. }

### At the time of writing the above configuration must pay attention to a space where we must add (a lot of small details must pay attention)

Here is their definition monitor nginx script chk_nginx.sh

 

 

  1. vim  chk_nginx.sh
  2. #!/bin/bash
  3. url=“http://192.168.100.1/index.html”
  4. status=$(/usr/bin/curl -s –head “$url”|awk ‘/HTTP/ {print $2}’)
  5. if[ “$status” != “200” ]; then
  6. /etc/init.d/keepalived stop
  7. fi

Leave a Reply

Your email address will not be published. Required fields are marked *