LVS+KeepAlived搭建

在Centos6.5上搭建LVS+KeepAlived作为负载,以下是实验的例子

1. 环境

操作系统是:

$ cat /etc/redhat-release
CentOS release 6.5 (Final)

三台服务器ip分别是:

LVS server realserver1 realserver2
192.168.10.20(VIP:192.168.10.200) 192.168.10.21 192.168.10.22

这里写图片描述

1. 下载安装

1.1 安装相关依赖包

$ yum install -y pcre-devel kernel-devel openssl-devel libnl-devel popt-devel
# 安装ipvsadm和keepalived
$ yum intall -y ipvsadm keepalived
$ keepalived -v #查看安装成功与否
Keepalived v1.2.13 (03/19,2015)

1.2 安装tcptraceroute,作为存活检测

$ yum install –y tcptraceroute  
#或者下载tcptraceroute-1.5-0.beta7.el6.rf.x86_64.rpm安装

2. 开启路由转发

在LVS Server服务器上开启路由转发

$ vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 #修改这里
$ sysctl -p
net.ipv4.ip_forward = 1 #修改成功后显示
...

3. 修改keepalived配置

3.1 修改keepalived.conf

$ cd /etc/keepalived/
$ cp keepalived.conf keepalived.conf.bak # 备份原来的
$ vi keepalived.conf

# 以下是做实验的例子配置的conf
! Configuration File for keepalived

global_defs {
   router_id LVS_MASTER 
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.200  #虚拟IP
    }
}

#----mtreceiver begin ---- 192.168.10.200 80 -------
    virtual_server 192.168.10.200 80 { #虚拟IP和端口
        delay_loop 6 #检查真实服务器时间,单位秒
        lb_algo rr   #设置负载调度算法,rr为轮训
        lb_kind DR   #设置LVS负载均衡DR模式
        nat_mask 255.255.0.0
        ## persistence_timeout 50
        protocol TCP
                        #------realserver-1 ------
        real_server 192.168.10.21 80 {
            weight 1
            MISC_CHECK {
            misc_path "/etc/keepalived/tcp_port.sh 192.168.10.21 80"
            misc_timeout 2
            }
        }
                        #------realserver-2 ------
        real_server 192.168.10.22 80 {
            weight 1
            MISC_CHECK {
            misc_path "/etc/keepalived/tcp_port.sh 192.168.10.22 80"
            misc_timeout 2
            }
        }

    }
#-------realserver end ------------

下载并使用traceroute进行存活检测:

$ yum install -y tcptraceroute
$ touch tcp_port.sh
$ chmod 755 tcp_port.sh
$ vi tcp_port.sh

# tcp_port.sh内容如下
#!/bin/bash
/usr/bin/tcptraceroute -f 255 -m 255 -q 1 -w1  $1 $2 | grep open >/dev/null

3.2 添加检测脚本tcp_port.sh

$ cd /etc/keepalived/
$ touch tcp_port.sh
$ vi tcp_port.sh
# 内容如下
#!/bin/bash
/usr/bin/tcptraceroute -f 255 -m 255 -q 1 -w1  $1 $2 | grep open >/dev/null
exit $?

3.3 启动停止keepalived

$ service keepalived start | stop

4. 配置real_server

# 分别在realserver1 和 realserver2 上新建realserver.sh脚本,通过命令绑定绑定VIP
$ touch realserver.sh
$ vi realserver.sh
# 内容为:

#!/bin/bash
# chkconfig: - 60 50
# description: start realserver
# processname: lvsrs
VIP=192.168.10.200  #注意修改每台服务器对应修改标记的VIP
case "$1" in
    start)
        echo "start LVS of REALServer"
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
        ;;
    stop)
        /sbin/ifconfig lo:0 down
        echo "close LVS REALserver"
        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
        ;;       
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac

# 启动realserver绑定绑定VIP
$ sh realserver.sh start

# 成功后可以看到以下输出:
$ ip add show lo:0
1: lo:  mtu 65536 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
       valid_lft forever preferred_lft forever
    inet 192.168.10.200/32 brd 192.168.10.200 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever

5. 查看当前运行状态

在LVS Server服务器上监测运行状态:

$ ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.10.200:80 rr
  -> 192.168.10.21:80           Route   1      0          0         
  -> 192.168.10.22:80           Route   1      0          0    

$ tail -f /var/log/messages
Jun 11 23:27:40 localhost Keepalived_vrrp[3197]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jun 11 23:27:40 localhost Keepalived_healthcheckers[3196]: Using LinkWatch kernel netlink reflector...
Jun 11 23:27:40 localhost Keepalived_healthcheckers[3196]: Activating healthchecker for service [192.168.10.21]:80
Jun 11 23:27:40 localhost Keepalived_healthcheckers[3196]: Activating healthchecker for service [192.168.10.22]:80
Jun 11 23:27:41 localhost Keepalived_vrrp[3197]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 11 23:27:42 localhost Keepalived_vrrp[3197]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 11 23:27:42 localhost Keepalived_vrrp[3197]: VRRP_Instance(VI_1) setting protocol VIPs.
Jun 11 23:27:42 localhost Keepalived_vrrp[3197]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.200
Jun 11 23:27:42 localhost Keepalived_healthcheckers[3196]: Netlink reflector reports IP 192.168.10.200 added
Jun 11 23:27:47 localhost Keepalived_vrrp[3197]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.200

你可能感兴趣的:(linux,load-balan)