更多内容请访问我的个人网站【Linux札记】

一、服务器环境

1、拓扑图

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8

OS:Redhat 5.8 64位系统

LVS-Master : 192.168.19.60

LVS-Backup : 192.168.19.61

LVS-VIP : 192.168.19.65

Realserver-1 : 192.168.19.62 (Apache)

Realserver-2 : 192.168.19.63 (Nginx)

Realserver-2 : 192.168.19.64 (IIS)

2、安装LVS依赖软件

[root@xunbang_master ~]# yum install -y gcc gcc-c++ make openssl-devel kernel-devel

注:系统采用最少化方法安装

所有服务器关闭Selinux

[root@xunbang_master ~]# vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted


 

把SELINUX=disabled保存,然后重启服务器即可。

二、安装ipvsadm

1、建立编译时必须的一个软链接

[root@xunbang_master ~]# ln -s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux

(这里如果找不到以上内核目录,是因为kernerl-devel没有装,先去安装上就有了)

[root@xunbang_master ~]# ll /usr/src/linux

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_02

2、下载 ipvsadm

[root@xunbang_master ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

Redhat5.8 64位LVS实例环境讲解【一】_ 64位_03

3、解压缩

[root@xunbang_master ~]# tar -zxvf ipvsadm-1.24.tar.gz
 
[root@xunbang_master ~]# cd ipvsadm-1.24

4、编译安装

[root@xunbang_master ipvsadm-1.24]# make && make install

5、确认安装成功

[root@xunbang_master ipvsadm-1.24]# whereis ipvsadm

Redhat5.8 64位LVS实例环境讲解【一】_LVS实例环境讲解_04

1
[root@xunbang_master ipvsadm-1.24]# ipvsadm --version

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_05

三、安装keepalived

1、下载keepalived

[root@xunbang_master ~]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

Redhat5.8 64位LVS实例环境讲解【一】_LVS实例环境讲解_06

2、解压keepalived

[root@xunbang_master ~]# tar -zxvf keepalived-1.2.2.tar.gz
[root@xunbang_master ~]# cd keepalived-1.2.2
[root@xunbang_master keepalived-1.2.2]# ./configure

Redhat5.8 64位LVS实例环境讲解【一】_ 64位_07

Redhat5.8 64位LVS实例环境讲解【一】_LVS实例环境讲解_08

(注意这个步骤要看到以上字样才是正常的)

 

3、安装

[root@xunbang_master keepalived-1.2.2]# make && make install

如果报错,如图所示这样

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_09

注意:

把#include linux/types.h /* For __beXX types in userland */

移到#include sys/types.h 这行的下面

再编译就可以通过了。(这个可能是keepalived版本问题)

[root@xunbang_master keepalived-1.2.2]# vim keepalived/libipvs-2.6/ip_vs.h

Redhat5.8 64位LVS实例环境讲解【一】_LVS实例环境讲解_10

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_11

 

4. 配置

先看下有没有文件

Redhat5.8 64位LVS实例环境讲解【一】_ 64位_12

[root@xunbang_master ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
 
[root@xunbang_master ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
 
[root@xunbang_master ~]# mkdir /etc/keepalived
 
[root@xunbang_master ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
 
[root@xunbang_master ~]# cp /usr/local/sbin/keepalived /usr/sbin/
 
[root@xunbang_master ~]# chkconfig  keepalived on


[root@xunbang_master ~]# service keepalived restart

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_13

查看keepalived进程在不在

Redhat5.8 64位LVS实例环境讲解【一】_Redhat5.8_14

5、从 keepalived跟主keepalived服务器同样的安装方法,唯一不同的就是keepalived.conf配置文件不同。

四、keepalived.conf配置文件解释

1、keepalived.conf主配置文件内容

! Configuration File for keepalived
 
global_defs {
   notification_email {
         790028862@qq.com #表示发送通知邮件时邮件源地址是谁
   }
   notification_email_from 790028862@qq.com
   smtp_server 127.0.0.1 #表示发送email时使用的smtp服务器地址,这里可以用本地的sendmail来实现
  # smtp_connect_timeout 30 #连接smtp连接超时时间
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER  # 备份LB将MASTER改为BACKUP
    interface eth0 #实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
    virtual_router_id 51  #这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
    priority 100 # 优先级,备份LB值改为比这个值小
    advert_int 1 #检查间隔,默认为1秒
    authentication { #这里设置认证
        auth_type PASS #认证方式,可以是PASS或AH两种认证方式
        auth_pass 1111 #认证密码
    }
    virtual_ipaddress {
        192.168.19.65 # 多个VIP换行写
    }
}
 
    virtual_server 192.168.19.65 80 {
        delay_loop 6  #每隔6秒查询realserver状态
        lb_algo wlc # LVS 算法
        lb_kind DR  # LVS模式 DR是直接路由
        persistence_timeout 60 #同一IP连接60秒内分配到同一台realserver
        protocol TCP #TCP协议检测realserver状态
 
    real_server 192.168.19.62 80 {
        weight 3 #权重
        TCP_CHECK {
        connect_timeout 10 #10秒无响应超时
        nb_get_retry 3 #重连次数
        delay_before_retry 3 #重连间隔
        connect_port 80 #监控检查的端口
        }
    }
    real_server 192.168.19.63 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 192.168.19.64 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

2、keepalived.conf从配置文件内容

! Configuration File for keepalived
 
global_defs {
   notification_email {
         790028862@qq.com
   }
   notification_email_from 790028862@qq.com
   smtp_server 127.0.0.1
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.19.65 
    }
}
 
    virtual_server 192.168.19.65 80 {
        delay_loop 6 
        lb_algo wlc 
        lb_kind DR 
        persistence_timeout 60
        protocol TCP 
 
    real_server 192.168.19.62 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10 
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.19.63 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 192.168.19.64 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

到此主从LVS配置完成,下一篇讲物理机的配置(也就是real_server)