Osheep

时光不回头,当下最重要。

LVS-NAT+Keepalived高可用群集

构建LVS-NAT+Keepalived高可用群集

——-client——-主LVS—————从LVS———–WEB1————-WEB2————-

1.1.1.254    1.1.1.10        1.1.1.20      192.168.1.100    192.168.1.200   
             192.168.1.10    192.168.1.20 

外网漂移IP:1.1.1.1
内网漂移IP:192.168.1.1
《LVS-NAT+Keepalived高可用群集》

LVS-NAT+keepalived.png

一、配置LVS服务器(主-从两台服务器)

1、配置IP

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
[root@localhost ~]# service network restart

2、调整响应参数(主-从配置一样)

[root@localhost ~]# vim /etc/sysctl.conf
修改:
net.ipv4.ip_forward = 1
添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
[root@localhost ~]# sysctl -p

3、安装并开启ipvsadm服务(主-从配置一样)

[root@localhost ~]#rpm -ivh /media/CentOS_6.5_Final/Packages/ipvsadm-1.26-2.el6.x86_64.rpm 
(要记得挂载光盘,之前我都是挂载到/mnt下)     
[root@localhost ~]# service ipvsadm start
[root@localhost ~]# chkconfig --add ipvsadm
[root@localhost ~]# chkconfig ipvsadm on

二、配置Keepalived服务器

4、安装keepalived软件(主-从配置一样)

[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel 
(这是几个keepalived的依赖软件,我用的centos6.5系统里面自己就安装的有,在这里我就没有写配置
YUM仓库的笔记
[root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/keepalived-1.2.13
[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kerneldir=/usr/
src/kernels/2.6.32-431.el6.x86_64 
[root@localhost keepalived-1.2.13]# make && make install
[root@localhost ~]# /etc/init.d/keepalived start 开启服务
[root@localhost ~]# netstat -anp | grep keepalived  状态
[root@localhost ~]# chkconfig --add keepalived 添加为系统服务
[root@localhost ~]# chkconfig keepalived on

4.1、主服务器配置

[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived
.conf_bak (先拷贝一份配置文件,因为keepalived的配置文件就算是编写错了,启动服务也不会报错)
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
    auth_type PASS
    auth_pass 1111
}
virtual_ipaddress {
    1.1.1.1
 }
} 
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 1
priority 100
advert_int 1
authentication {
    auth_type PASS
    auth_pass 1111
}
virtual_ipaddress {
    192.168.1.1
 }
}       
virtual_server 1.1.1.1 80 {
delay_loop 15   #健康检查的时间
lb_algo rr      #定义调度算法
lb_kind NAT      #定义负载均衡群集的模式
protocol TCP
real_server 192.168.1.100 80 {
    weight 1    #定义权重值
    TCP_CHECK {
        connect_port 80     #检查目标端口
        connect_timeout 3   #链接超时时间
        nb_get_retry 3      #重试次数
        delay_before_retry 4  #重试间隔时间
    }
}
real_server 192.168.1.200 80 {
    weight 1
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 4
    }
}
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0 (ip a查看有没有飘逸地址)

4.2、从服务器配置

[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 90
advert_int 1
authentication {
    auth_type PASS
    auth_pass 1111
}
virtual_ipaddress {
    1.1.1.1
 }
} 
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 1
priority 90
advert_int 1
authentication {
    auth_type PASS
    auth_pass 1111
}
virtual_ipaddress {
    192.168.1.1
 }
}       
virtual_server 1.1.1.1 80 {
delay_loop 15   #健康检查的时间
lb_algo rr      #定义调度算法
lb_kind NAT      #定义负载均衡群集的模式
protocol TCP
real_server 192.168.1.100 80 {
    weight 1    #定义权重值
    TCP_CHECK {
        connect_port 80     #检查目标端口
        connect_timeout 3   #链接超时时间
        nb_get_retry 3      #重试次数
        delay_before_retry 4  #重试间隔时间
    }
}
real_server 192.168.1.200 80 {
    weight 1
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 4
    }
}
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0  (ip a也可以,有没有飘逸地址)

4.3.配置SNAT转发规则(主-从配置一样)

# service iptables start  开启防火墙
清空防火墙规则
# iptables -F -t filter
# iptables -F -t raw
# iptables -F -t mangle
# iptables -F -t nat
转发规则
# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 1.1.1.1
# service iptables save  保存策略

三、配置WEB节点服务器

1、配置IP地址

1)设置IP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

2、配置http服务

WEB1:
[root@localhost ~]# echo "A1111111111" > /var/www/html/index.html

WEB2:
[root@localhost ~]# echo "B2222222222" > /var/www/html/index.html
[root@localhost ~]# service httpd restart
[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig httpd on

三、验证:

一)使用ping命令

客户端:ping -t 1.1.1.1
断开主LVS的网卡,查看客户端的ping情况。
重启主LVS的网卡,查看客户端的ping情况。

二)客户端

1、访问:

http://1.1.1.1/
在主LVS上查看:
[root@localhost ~]# ipvsadm -Lnc

2、断开主LVS的网卡

客户端访问:http://1.1.1.1/
在主-从LVS上查看:
[root@localhost ~]# ipvsadm -Lnc
在主-从LVS上查看:
[root@localhost ~]# ip addr show dev eth0

3、重启主LVS的网卡

客户端访问:http://1.1.1.1/
在主-从LVS上查看:
[root@localhost ~]# ipvsadm -Lnc
在主-从LVS上查看:
[root@localhost ~]# ip addr show dev eth0
点赞