高可用软件KEEPALIVED安装

系统:centos6.5

虚拟IP:172.16.10.10

机器A:172.16.10.11

机器B:172.16.10.12

为了方便这里采用yum安装,源码安装我没试过,这里主要讲实现keepalived高可用的一些基础的设置。两天机子操作基本一致,,有不同的地方会专门指出。

安装命令

yum install keepalived -y
安装完成之后,配置文件是/etc/keepalived/keepalived.conf,修改它
vim /etc/keepalived/keepalived.conf
输入内容如下
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NGINX_HA
}
#vrrp_script chk_port {
#        script "/etc/keepalived/check_keepalived.sh"
#        interval 1
#       # weight 2
#}

vrrp_instance VI_2 {
    state BACKUP   #表示本机的装填,必须大写,正常情况下,一个机器为MASTER,另一个为BACKUP。不过我一般两个都是BACKUP,这个不影响。
    interface eth1  #虚拟IP绑定的网卡名称。用ifconfig查看自己的网卡名称,改成自己的就可以。
    virtual_router_id 52   #虚拟路由ID,如果你局域网里面有多个软件要用到KEEPALIVED的话,需要把这个的值改成不一样的,,否则会出现相互干扰的状况。
    priority 100    #这个怎么翻译我忘了,这个值一般情况下,哪个数字大,哪个就会成为MASTER。master和slave这里的值需要不一样,并且差值的绝对值要小于其他模块weight的值
    nopreempt       #不抢占模式,假设master挂掉,虚拟IP会自动切换到slave上,等master恢复正常,就会变成新的slave。不会导致抢占发生VIP漂移的现象。如果不加此项原master在加入后会抢占成为master,发生虚拟IP漂移
    advert_int 1    #这个忘了,一般不动。
    authentication {    #验证相关的,两天机子保持一致即可,一般不改变
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.10/16   #虚拟IP地址和掩码
    }

贴一下我这里的两个配置吧

MASTER配置

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NGINX_HA
}
#vrrp_script chk_port {
#        script "/etc/keepalived/check_keepalived.sh"
#        interval 1
#       # weight 2
#}

vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 52
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.10/16
    }

SLAVE配置
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id NGINX_HA
}
#vrrp_script chk_port {
#        script "/etc/keepalived/check_keepalived.sh"
#        interval 1
#       # weight 2
#}

vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 52
    priority 80
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.10.10/16
    }


这里有一点要注意,防火墙要放开VRRP广播。。

vim /etc/sysconfig/iptables
在-A INPUT -p icmp -j ACCEPT加入
-A INPUT -p vrrp -j ACCEPT
保存退出,重启防火墙。

最后我们启动keepalived

service keepalived start
 


启动之后通过ip a命令可以查看虚拟IP是否成功

[root@jk-test php_client]# ip a
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:aa:b9:4d brd ff:ff:ff:ff:ff:ff
    inet 172.16.10.11/16 brd 172.16.255.255 scope global eth1
    inet 172.16.10.10/16 scope global secondary eth1
    inet6 fe80::250:56ff:feaa:b94d/64 scope link 
       valid_lft forever preferred_lft forever
[root@jk-test php_client]# 

我们可以看到,在11这台电脑上,多了一个IP 172.16.10.10

此时如果我们在11上停止keepalived,就会发现虚拟IP跑到了12上。

[root@jk-test php_client]# service keepalived stop
Stopping keepalived:                                       [  OK  ]
[root@jk-test php_client]# 


下来我们用ip a看一下12

[root@jk-test php_client]# ip a
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:aa:08:69 brd ff:ff:ff:ff:ff:ff
    inet 172.16.10.12/16 brd 172.16.255.255 scope global eth1
    inet 172.16.10.10/16 scope global secondary eth1
    inet6 fe80::250:56ff:feaa:869/64 scope link 
       valid_lft forever preferred_lft forever
[root@jk-test php_client]# 

此时我们发现虚拟IP已经跑到了12上,好了今天就到这里,以后有新东西我会在下面补充。

你可能感兴趣的:(高可用软件KEEPALIVED安装)