当前位置 博文首页 > 超负荷小生的博客:LVS+keepalived---DR模式的负载均衡
#关闭所有服务器的防火墙和SElinux
systemctl stop firewalld
setenforce 0
#在每个web服务器上安装httpd
yum install -y httpd
#在每个网站的根目录上设置每个web服务器容易辨识的index.html
vim /var/www/html/index.html
#web1输入的内容
this is web1
#web2输入的内容是
this is web2
#通过浏览器访问ip查看是否搭建成http服务器
两台LVS服务器(192.168.160.128 192.168.160.129)都设置 keepalived* ipvsadm
yum -y install keepalived* ipvsadm //安装keepalived和lvs管理工具
modprobe ip_vs //加载内核模块
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak //备份配置文件
LVS1
按照以下内容修改keepalived.conf 或 新建keepalived.conf文件将下面的内容复制进去
(keepalived.conf 文件所在位置:/etc/keepalived/)
global_defs {
router_id LVS_TEST #服务器名字
}
vrrp_instance VI_1 {
state MASTER #配置主备,备用机此配置项为BACKUP
interface ens33 #指定接口
virtual_router_id 51 #指定路由ID,主备必须一样
priority 101 #设置优先级,主略高于备份
advert_int 1 #设置检查时间
authentication {
auth_type PASS #设置验证加密方式
auth_type 1234 #设置验证密码
}
virtual_ipaddress {
192.168.160.100/24 #加上子网掩码
}
}
virtual_server 192.168.160.100 80 {
delay_loop 15 #健康检查时间
lb_algo rr #LVS调度算法
lb_kind DR #LVS工作模式
!persistence 60 #是否保持连接,!不保持
persistence_timeout 0 #同一时间内,同一个客户端在一定时间内访问的是同一个sever
protocol TCP #服务采用TCP协议
real_server 192.168.160.131 80 {
weight 1 #权重
TCP_CHECK { #TCP检查
connect_port 80 #检查端口80
connect_timeout 3 #超时时间3秒
nb_get_retry 3 #重试次数3次
delay_before_retry 4 #重试间隔4秒
}
}
real_server 192.168.160.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
LVS2
按照以下内容修改keepalived.conf 或 新建keepalived.conf文件将下面的内容复制进去,基本和LVS类似只需要修改以下内容(keepalived.conf 文件所在位置:/etc/keepalived/)
global_defs {
router_id LVS_TEST02
}
vrrp_instance VI_1 {
state BACKUP #类型为备份
interface ens33
virtual_router_id 51
priority 99 #优先级
advert_int 1
authentication {
auth_type PASS
auth_type 1234
}
virtual_ipaddress {
192.168.160.100/24
}
}
virtual_server 192.168.160.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
persistence_timeout 0 #同一时间内,同一个客户端在一定时间内访问的是同一个sever
!persistence 60
protocol TCP
real_server 192.168.160.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.160.131 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
LVS1和LVS2配置完keepalived.conf,后进行查看配置是否成功
ip a #查看ens33网卡上是不是有自己绑定的虚拟出来的ip
两个web服务器上绑定VIP
#在每个web上的lo网卡绑定VIP:ip addr add VIP/32 dev lo
ip addr add 192.168.160.100/32 dev lo
#查看lo网卡是否绑定成功
ip a
修改完毕后启动
systemctl restart keepalived
测试:
浏览器访问 192.168.160.100
进行多次访问,出现的内容是web1服务器的index.html内容 或者 web2服务器 的index.html内容
参考链接:
LVS之DR、NAT、TUN三种模式快速搭建
LVS+Keepalive双机热备