首页 > 代码库 > keepalived high-availability

keepalived high-availability

#

#keepalived 官网

#keepalived 中文文档

#keepalived 下载

#

#逻辑图

技术分享


主机名keeoalived角色ip地址
Storage_1Master

192.168.174.137 

Vip:192.168.174.100

Storage_2Backup192.168.174.138

#均关闭防火墙和selinux


下载软件 编译安装

[root@Storage_2 ~]# wget 
[root@Storage_2 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@Storage_2 keepalived-1.3.5]# make && make install
#同理
[root@Storage_1 ~]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz


或者yum 安装 (系统镜像内有)

[root@Storage_1 ~]# yum install -y keepalived
[root@Storage_2 ~]# yum install -y keepalived


本文采用的是yum安装


nginx安装

#前面的文章已经安装过很多次了,这里就不再重复


#开启nginx

[root@Storage_1 ~]# /usr/local/nginx/sbin/nginx

技术分享



[root@Storage_2 ~]# /usr/local/nginx/sbin/nginx

技术分享



#修改keepalived配置文件(Master节点)

[root@Storage_1 ~]# vim /etc/keepalived/keepalived.conf
[root@Storage_1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     test@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.01
   smtp_connect_timeout 30
   router_id Stroage_1
}
vrrp_script chk_nginx {    #脚本
script "/opt/chk_nginx_status.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 150           #优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.174.100
    }
    track_script {
	chk_nginx
}
}


编写检查nginx状态的脚本

[root@Storage_1 ~]# vim /opt/chk_nginx_status.sh
[root@Storage_1 ~]# cat /opt/chk_nginx_status.sh 
#!/bin/bash
if [ `netstat -lntup | grep "nginx: master" |wc -l` -eq 0 ]
then
systemctl stop keepalived         #如果nginx挂掉,就关闭keepalived
fi

[root@Storage_1 ~]# chmod +x /opt/chk_nginx_status.sh


#修改keepalived配置文件(Backup节点)

[root@Storage_2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     test@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.01
   smtp_connect_timeout 30
   router_id Stroage_2       #在同意个局域网内唯一
}
vrrp_instance VI_1 {               #实力名称
    state BACKUP             #角色
    interface eno16777736    #接口
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.174.100/24    #vip
    }
}


#开启服务

[root@Storage_1 ~]# systemctl start keepalived.service
[root@Storage_2 ~]# systemctl start keepalived.service


#测试

技术分享



#查看ip(vip出现在master上)

[root@Storage_1 ~]# ip addr show eno16777736 
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:87:00:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.174.137/24 brd 192.168.174.255 scope global dynamic eno16777736
       valid_lft 1684sec preferred_lft 1684sec
    inet 192.168.174.100/32 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe87:97/64 scope link 
       valid_lft forever preferred_lft forever
[root@Storage_1 ~]#
[root@Storage_2 ~]# ip addr show eno16777736 
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:78:f4:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.174.138/24 brd 192.168.174.255 scope global dynamic eno16777736
       valid_lft 1595sec preferred_lft 1595sec
    inet6 fe80::20c:29ff:fe78:f4d9/64 scope link 
       valid_lft forever preferred_lft forever
[root@Storage_2 ~]#



#测试

#关闭主上的nginx

[root@Storage_1 ~]# /usr/local/nginx/sbin/nginx -s quit


#主上的vip不见了

[root@Storage_1 ~]# ip addr show eno16777736 
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:87:00:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.174.137/24 brd 192.168.174.255 scope global dynamic eno16777736
       valid_lft 1326sec preferred_lft 1326sec
    inet6 fe80::20c:29ff:fe87:97/64 scope link 
       valid_lft forever preferred_lft forever



#备上出现了vip

[root@Storage_2 ~]# ip addr show eno16777736 
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:78:f4:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.174.138/24 brd 192.168.174.255 scope global dynamic eno16777736
       valid_lft 1397sec preferred_lft 1397sec
    inet 192.168.174.100/24 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe78:f4d9/64 scope link 
       valid_lft forever preferred_lft forever


#访问测试

技术分享



#vip成功漂移



本文出自 “Chauncey” 博客,请务必保留此出处http://cqwujiang.blog.51cto.com/10808946/1918990

keepalived high-availability