LVS+Keepalived安装过程

为了方便测试,禁掉防火墙和Selinux

(1) 禁掉防火墙

chkconfig iptables off

(2) 禁掉Selinux

vi /etc/selinux/config

SELINUX=disabled

(3) 必须重新启动系统

shutdown -r now

一.检查lvs模块是否已经安装

1)modprobe -l | grep ipvs

LVS+Keepalived安装过程_第1张图片

2)cd /boot  

cat config-2.6.32-358.el6.x86_64 | grep -i ipvs

二.yum安装ipvsadm

yum -y install ipvsadm

三.安装keepalived

1.先安装所需的环境

1)gcc

yum install gcc gcc-c++

2)openssl-devel, openssl

yum install openssl-devel openssl

3)源码内核

yum install kernel-devel kernel

建立软连接

 ln -s /usr/src/kernels/内核名称/  /usr/src/linux

4)popt-devel , libnl-devel

yum install popt-devel libnl-devel

5)libnfnetlink-devel

yum install -y libnfnetlink-devel

2.源码安装keepalived

1)cd /usr/local/src 下载keepalived

wget http://www.keepalived.org/software/keepalived-版本号.tar.gz

http://www.keepalived.org/software/keepalived-1.2.20.tar.gz  

2)解压缩

tar -zxvf  keepalived-版本号.tar.gz

cd keepalived-版本号

3)配置安装,依次执行

./configure

确保以下3个信息都为YES 

Use IPVS Framework           YES

IPVS sync daemon support YES

Use VRRP Framework  YES

make 

make install

  4)创建软连接 

  ln -s /usr/local/sbin/keepalived /sbin/  

 5)检查是否安装成功

keepalived --help

   6)启动设置

cp /usr/local/etc/rc.d/init.d/keepalived   /etc/rc.d/init.d

cp /usr/local/etc/sysconfig/keepalived   /etc/sysconfig/

mkdir /etc/keepalilved

cp /usr/local/etc/keepalived/keepalived.conf  /etc/keepalived/

cp /usr/local/sbin/keepalived  /usr/sbin/

   7) 配置keepalived.conf

vi /etc/keepalived/keepalived.conf

   8) 启动keepalived服务并查看进程

service keepalived start (或者/etc/init.d/keepalived start)

ps -ef | grep keepalived

ipvsadm -L -n

重启 service keepalived restart

    9) 设置开机自启动

chkconfig keepalived on

  ======================keepalived.conf配置文件=========================

 

  1. ! Configuration File for keepalived  
  2. global_defs {  
  3.    notification_email {  
  4.      [email protected]   #设置报警邮件地址,可以设置多个,每行1个,  
  5.      [email protected]  #需开启邮件报警及本机的Sendmail服务。  
  6.      [email protected]  
  7.    }  
  8.    notification_email_from [email protected]  
  9.    smtp_server 192.168.200.1  #设置SMTP Server地址;  
  10.    smtp_connect_timeout 30  
  11.    router_id LVS_DEVEL  
  12. }  
  13.            ########VRRP Instance########  
  14. vrrp_instance VI_1 {  
  15.     state MASTER    #指定Keepalived的角色,MASTER为主机服务器,BACKUP为备用服务器  
  16.     interface eth0  #BACKUP为备用服务器  
  17.     virtual_router_id 51  
  18.     priority 100    #定义优先级,数字越大,优先级越高,主DR必须大于备用DR。  
  19.     advert_int 1  
  20.     authentication {  
  21.         auth_type PASS  #设置验证类型,主要有PASS和AH两种  
  22.         auth_pass 1111  #设置验证密码  
  23.     }  
  24.     virtual_ipaddress {  
  25.         192.168.1.200  #设置主DR的虚拟IP地址(virtual IP),可多设,但必须每行1个  
  26.     }  
  27. }  
  28.            ########Virtual Server########  
  29. virtual_server 192.168.1.200 80 {  #注意IP地址与端口号之间用空格隔开  
  30.     delay_loop 6        #设置健康检查时间,单位是秒  
  31.     lb_algo rr          #设置负载调度算法,默认为rr,即轮询算法,最优秀是wlc算法  
  32.     lb_kind DR          #设置LVS实现LB机制,有NAT、TUNN和DR三个模式可选  
  33.     nat_mask 255.255.255.0  
  34.     persistence_timeout 50  #会话保持时间,单位为秒  
  35.     protocol TCP        #指定转发协议类型,有TCP和UDP两种  
  36.     real_server 192.168.1.132 80 {  
  37.         weight 1          #配置节点权值,数字越大权值越高  
  38.         TCP_CHECK {  
  39.             connect_timeout 3     #表示3秒无响应,则超时  
  40.             nb_get_retry 3        #表示重试次数  
  41.             delay_before_retry 3  #表示重试间隔  
  42.         }  
  43.     }  
  44.     real_server 192.168.1.133 80 {  #配置服务器节点,即Real Server2的public IP  
  45.         weight 3            #配置节点权值,数字越大权值越高  
  46.         TCP_CHECK {  
  47.             connect_timeout 3       #表示3秒无响应,则超时  
  48.             nb_get_retry 3          #表示重试次数  
  49.             delay_before_retry 3    #表示重试间隔  
  50.         }  
  51.     }  
  52. ====================keepalived.conf配置完成======================
  53. 对于slave的keepalived.conf中与上相同,更改
  54. 1. state 改为 BACKUP  //标明为备用服务器
  55. 2. priority 90  //设置优先级低于主服务器
  56. ========================================================


状态监控
1.测试lvs能否进行负载均衡转发,利用ipvsadm命令查看
2. 如果想只当当前的 请求转发到哪个服务器去了,可以用ipvsadm选项来查看,其完整形式为:ipvsadm –lcn | grep 192.168.1.115
     3. 查看详细日志 tail -f /var/log/messages


你可能感兴趣的:(架构)