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
- global_defs { ####全局定义
- notification_email { #####指定keepalived在发生事件时(比如切换),需要发送的email对象,可以多个,每行一个
- 123@qq.com
- }
- notification_email_from 123@qq.com
- smtp_server 127.0.0.1 #####指定发送email的smtp服务器
- smtp_connect_timeout 30
- router_id LVS_DEVEL #######运行keepalived的机器的一个标识
- }
- vrrp_sync_group VG1 {
- group {
- VI_1
- VI_2
- }
- }
- vrrp_script chk_nginx {
- script “/etc/chk_nginx.sh” ###监控脚本
- interval 2 ###监控时间
- #weight -2
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- # track_interface {
- # eth0
- # eth1
- # }
- virtual_router_id 51 #VRID标记 master和backup必须一致
- mcast_src_ip 192.168.2.113
- #发送多播包的地址,如果不设置默认使用绑定的网卡的primary ip
- priority 150
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_nginx ### 就是上面vrrp_script后面的实例名称 执行监控的服务
- }
- virtual_ipaddress {
- 192.168.2.116
- }
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eth0
- tual_router_id 52
- mcast_src_ip 192.168.100.113
- priority 149
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- # track_interface {
- # eth0
- # eth1
- # }
- virtual_ipaddress {
- 192.168.100.117
- }
- }
另一主的配置文件
- global_defs { ####全局定义
- notification_email { #####Keepalived specified when an event occurs (such as switching), email subject you want to send, can more than one, one per line
- 123.com
- }
- notification_email_from 123@qq.com
- smtp_server 127.0.0.1 #####指定发送email的smtp服务器
- smtp_connect_timeout 30
- router_id LVS_DEVEL #######运行keepalived的机器的一个标识
- }
- vrrp_sync_group VG1 {
- group {
- VI_1
- VI_2
- }
- }
- vrrp_script chk_nginx {
- script “/etc/chk_nginx.sh” ###监控脚本
- interval 2 ###监控时间
- #weight -2
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- # track_interface {
- # eth0
- # eth1
- # }
- virtual_router_id 51 #VRID标记 master和backup必须一致
- mcast_src_ip 192.168.2.112
- #发送多播包的地址,如果不设置默认使用绑定的网卡的primary ip
- priority 149
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.2.116
- }
- }
- vrrp_instance VI_2 {
- state MASTER
- interface eth0
- tual_router_id 52
- mcast_src_ip 192.168.100.112
- priority 150
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- chk_nginx ### The above is an example of the name behind vrrp_script execution monitoring service
- }
- # track_interface {
- # eth0
- # eth1
- }
- virtual_ipaddress {
- 192.168.100.117
- }
- }
### 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
- vim chk_nginx.sh
- #!/bin/bash
- url=“http://192.168.100.1/index.html”
- status=$(/usr/bin/curl -s –head “$url”|awk ‘/HTTP/ {print $2}’)
- if[ “$status” != “200” ]; then
- /etc/init.d/keepalived stop
- fi
Leave a Reply