werew 发表于 2014-9-3 08:58:35

keepalived双机实现高可用和负载均衡

安装keepalived

1
2
3
# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/ && make && make install
#ln-s/usr/local/sbin/keepalived /sbin/
# chkconfigkeepalivedon






修改网卡配置

1
2
3
4
5
6
7
8
# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.2
NETMASK=255.255.255.255
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=192.168.1.2
ONBOOT=yes






修改内核文件配置

1
2
3
4
5
6
# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
# sysctl -p






修改keepalived配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script chk_mysql_port {
                script "/var/shell/mysql_check.sh"
                interval 2
                weight 2
}
global_defs {
   notification_email {
      445970860@qq.com
   }

   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER    (BACKUP备用)
    interface eth0
    virtual_router_id 51
    priority 100   (backup须优先级低于master)
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    track_script {
                chk_mysql_port
      }
    virtual_ipaddress {
      192.168.1.2
    }

}
virtual_server 192.168.1.2 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.1 80 {
      weight 3
      TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 192.168.1.3 80 {
      weight 3
      TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
}






编写脚本(监控mysql,当mysql出现问题不能重新启动时停止nginx服务,keepalived服务会将出现问题的web服务器从集群中剔除出去)

1
2
3
4
5
6
7
8
9
10
# vim mysql_check.sh
#!/bin/bash
MYSQL_SLAVE=`ps -C mysqld | grep -v PID | wc -l`
if [ $MYSQL_SLAVE -eq 0 ];then
      /etc/init.d/mysqld restart
      sleep 3
      if [ `ps -C mysqld | grep -v PID | wc -l` -eq 0 ];then
                pkill -9 nginx
      fi
fi



页: [1]
查看完整版本: keepalived双机实现高可用和负载均衡