keepalived入门与掌握之keepalive+lvs实例部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived入门与掌握之keepalive+lvs实例部署相关的知识,希望对你有一定的参考价值。

  在实际环境中,keepalive常常与lvs,nginx,haproxy,mysql等等应用组成高可用集群服务,本章主要讲解关于keepalive+lvs实例部署

 

Keepalive+Lvs(lvs/nat模式)实例部署

如图所示为整体的拓扑图:


技术分享

一.部署前说明:

(1)系统版本: centos 6.6(64位)

(2)角色及ip相关信息:


角色名网络ip信息
客户端(CIP)10.58.137.203
Lvs_Master_DIReth0:172.51.96.105/24 &&  eth1:192.168.0.105/24
Lvs_Backup_DIReth0:172.51.96.119/24 &&  eth1:192.168.0.119/24
RS_RIP1eth0:172.51.96.235/24 &&  eth1:192.168.0.236/24
RS_RIP2eth0:172.51.96.236/24 &&  eth1:192.168.0.236/24
Lvs_vip172.51.96.175/32

(3)相关中间件信息

keepalive版本信息: keepalived-1.2.15

httpd版本信息:   httpd-2.2  (提供http服务)

ipvsadm版本信息: ipvsadm-1.2.1


二.部署操作:

负载均衡器上配置操作

(1)分别在Lvs_Master_DIR和Lvs_backup_DIR上安装Keepalive,ipvsadm所需要的相关依赖包:

# yum install openssl-devel popt-devel libnl-devel kernel-devel  -y

(2)分别在Lvs_Master_DIR和Lvs_backup_DIR上安装Keepalive以及ipvsadm,如下:

1. 安装ipvsadm软件

# yum install  ipvsadm  -y

2. 编译安装keepalive

1.1 keepalived的源码获取 

keepalived源码包我们可以到keepalived的官网:http://www.keepalived.org/去下载,相关说明文档亦可在其官网查看,比如keepalived的使用,相关配置说明,这里演示的版本为:1.2.15

# cd ~
# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

1.2 编译安装keepalived

<--编译安装keepalived-->

# ln -s /usr/src/kernels/2.6.32-573.18.1.el6.x86_64/ /usr/src/linux
# tar zxvf keepalived-1.2.15.tar.gz -C /usr/local/src
# cd /usr/local/src/keepalived-1.2.15/
# ./configure \
  --prefix=/usr/local/keepalived \
  --with-kernel-dir=/usr/src/linux
# make 
# make install

<--对keepalived进行相关路径优化调整-->

<---拷贝keepalived相关启动命令--->
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

<---将keepalived启动脚本添加到系统服务--->

# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# chkconfig --add keepalived
# chkconfig --level 2345 keepalived on

<---创建keepalived相关配置文件--->
# mkdir -p /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived

备注说明:
1.keepalived安装完成后,安装目录为:/usr/lccal/keepalived, 配置文件路径为:/etc/keepalive/
2.安装完成后,需要将启动脚本复制到/etc/ini.d/下
3.注意一定要执行述上的相关操作,不然有可能导致keepalived服务起不来

(3)分别配置Lvs_Master_DIR以及Lvs_Backup_DIR上的keepalive实例,如下所示:

1. Lvs_master_dr配置

 vim /usr/local/keepalived/etc/keepalived/keepalived.conf

内容如下


! Configuration File for keepalived

global_defs {
   notification_email {
       [email protected]
   }
   notification_email_from  [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_HTTP_HA
}

vrrp_instance http {
    state MASTER
    interface eth1
#    ont_track_primary

    track_interface {
    eth0
    eth1
    }

    mcast_src_ip 172.51.96.105
    garp_master_delay 10
    virtual_router_id 51
    priority 100
    advert_int 3

    authentication {
    auth_type PASS
    autp_pass 1234
    }

    virtual_ipaddress {
    172.51.96.175/32 dev eth1
    }

#    preemtp_delay 300
#    debug
}
   script "/usr/local/bin/health_check"
   interval 10
   weight 10
}

track_script {
   check_running 
}

virtual_server 172.51.96.175 80 {        
delay_loop 3                          
lb_algo rr    
lb_kind DR                                    
persistence_timeout 120                
nat_mask 255.255.255.0
protocol TCP  
                                                                             
real_server 192.168.0.235 80 {
weight 1                              
inhibit_on_failure   
HTTP_GET   {
   url {                                  
       path /info.php                                
#      digest                                           
       status_code 200                        
       }                                            
   connect_timeout   3                   
   nb_get    ·        _retry 3                         
   delay_before_retry 2                   
   }





1.3:启动keepalived服务

# service keepalived start
如果keepalived已经启动,这时我们就可以查看以下命令查看启动情况:
# service keepalived status  

# ps aux | grep keepalived

到这里,keepalived部署基本OK,下一章,我们将会讲到keepalived相关配置





(2)在DIR上新建一个shell脚本文件,如下操作所示:

vim /etc/init.d/lvs-dr

CIP:192.168.1.13/24
VIP:192.168.1.100/24

DIR:eth0:192.168.1.2/24 

Real-server:192.168.1.10/24 和 192.168.1.11/24. 192.168.1.12/24(提供http服务)



本文出自 “菜鸟的成长记” 博客,请务必保留此出处http://blief.blog.51cto.com/6170059/1748403

以上是关于keepalived入门与掌握之keepalive+lvs实例部署的主要内容,如果未能解决你的问题,请参考以下文章

Keepalive 之 keepalive概念介绍

keepalive 之 配置介绍

Keepalive 之 keepalive概念介绍

keepalive简单入门

Linux集群之keepalive+Nginx

keepalive 配置文件在哪