3 月 012014
 

 



keepalived 安装和使用

安装:

mkdir -p    /root/install

cd /root/install
tar -zxvf keepalived-1.2.9.tar.gz
cd keepalived-1.2.9
./configure –prefix=/usr/local/keepalive
make && make install
使用 主备 检测 并 不抢占
    nopreempt               #默认是抢占模式,这里设置不抢占,初始配置文件没有这行#
 
 
主的配置文件
 ! Configuration File for keepalived
global_defs {
router_id topcmm.net
}
vrrp_script check_run {
             script “/root/1.sh”
#监控脚本
       interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 88
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass topcmm.net
}
    #     track_script {
#       check_run
#   }                              
#  监控脚本,如果监控失败,会把主的切掉,自动会换到备的。但是keepalived的进程没有
        virtual_ipaddress {
192.168.100.11
}
}
virtual_server  192.168.100.11 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
persistence_timeout 50
protocol TCP    real_server 192.168.0.86 80 {
weight 1
        notify_down /root/mysql.sh

#检测脚本 如果后端检测失败,会执行上面的命令
        HTTP_GET {
url {
path /1
digest db0aa8bee7afe5a6e363ddcd890e9743
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}}

备机
  ! Configuration File for keepalived
global_defs {
router_id topcmm.net
}
vrrp_script check_run {
script “/root/1.sh”
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass topcmm.net
}

    #  track_script {
#      check_run
# }

virtual_ipaddress {
192.168.100.11
}
}
virtual_server  192.168.100.11 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
persistence_timeout 50
protocol TCP    real_server 192.168.0.88 80 {
weight 1
              notify_down /root/mysql.sh
        HTTP_GET {
url {
path /1
digest 9e01624ab7743bcdbd8cb31681392cd5
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

}

实现切换有2种方式
1:一种是通keepalived的检测,这种方式的好处是:可以自己写监控脚本,出问题的时候keepalived的进程不会挂掉.同时我们设置不抢占,除非keepalived被人为的重启,否则的话永远都不会主的会自动切回。
2:一种是通过keepalived的后端检测,这种方式的好处是可以使用keepalive定义的一些后端检测。而且出问题可以调用脚本,我参考的文章是这种方式,不过他采用的是出问题杀进程的方法,所以出问题的keepalived会被杀掉,重启的时候会自动切换回去。
通过考虑,还是觉得第一种方式比较适合。后面的配置可以忽略不写。但是监控脚本一定要确保没问题。
优化之后的配置
主的
  ! Configuration File for keepalived
global_defs {
router_id topcmm.net
}
vrrp_script check_run {
# script “/root/1.sh”
script “echo ‘n’ | telnet 127.0.0.1 3307 | grep Connected”
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 88
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass shanliao.com
}
track_script {
check_run
}
virtual_ipaddress {
192.168.0.11
}
}
备的
   ! Configuration File for keepalived
global_defs {
router_id topcmm.net
}
vrrp_script check_run {
# script “/root/1.sh”
script “echo ‘n’ | telnet 127.0.0.1 3307 | grep Connected”
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass shanliao.com
}

track_script {
check_run
}
virtual_ipaddress {
192.168.0.11
}
}

 

 Posted by at 下午 3:50

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny