lvs(ipvs)配置
实验环境介绍
1、实验需要4台linux虚拟机,两台dr,两台real server(本实验使用VM,linux版本为Centos7↑)
2、dr主机需要安装ipvs,ipvsadm,keepalived软件
3、real server需要安装web服务(本实验采用nginx)
4、四台主机需要在相同网段
5、采用直接路由模式配置
安装
yum install ipvsadm keepalived
采用rpm命令安装需要在软件名称后加版本号和.rpm后缀
rpm -ivh ipvsadm
rpm -ivh keepalived
IP分配
DIP1 192.168.106.99
DIP2 192.168.106.100
RIP1 192.168.106.132
RIP2 192.168.106.133
VIP 192.168.106.123
负载均衡器配置
DR1:
1、在两台dr服务器上绑定vip地址,两台dr使用vip对外提供服务
ifconfig ens33:0 192.168.106.123 broadcast 192.168.106.123 netmask 255.255.255.255 up
2、添加dr服务器,添加指向vip的静态路由
route add 192.168.106.123 dev ens33:0
3. 使用ipvsadm命令配置ipvs服务
ipvsadm -A -t 192.168.106.123:80 -s rr
ipvsadm -a -t 192.168.106.123:80-r 192.168.106.132:80 -g
ipvsadm -a -t 192.168.106.123:80 -r 192.168.106.133:80 –g
负载均衡器keepalived配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.106.123
}
}
virtual_server 192.168.106.123 80 {
delay_loop 3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 10
protocol TCP
real_server 192.168.106.132 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.106.133 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
DR2:
1、在两台dr服务器上绑定vip地址,两台dr使用vip对外提供服务
ifconfig ens33:0 192.168.106.123 broadcast 192.168.106.123 netmask 255.255.255.255 up
2、添加dr服务器,添加指向vip的静态路由
route add 192.168.106.123 dev ens33:0
3. 使用ipvsadm命令配置ipvs服务
ipvsadm -A -t 192.168.106.123:80 -s rr
ipvsadm -a -t 192.168.106.123:80-r 192.168.106.132:80 -g
ipvsadm -a -t 192.168.106.123:80 -r 192.168.106.133:80 –g
负载均衡器keepalived配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.106.123
}
}
virtual_server 192.168.106.12380 {
delay_loop 3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 10
protocol TCP
real_server 192.168.106.132 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.106.133 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
web服务器配置:
tips:已经安装好了nginx及其依赖包,并开启服务,将nginx.cnf里的server_name修改为每个server的ip地址。将html目录下的index.html内容修改为方便自己辨别的网页,我的是server的ip地址。两个服务器的配置一模一样,列出一个。
1、配置real server环回地址,绑定vip,real server使用vip作为源地址返回数据
ifconfig lo:0 192.168.106.123 netmask 255.255.255.255 broadcast 192.168.106.123 up
2、配置real server添加指向vip的静态路由
route add 192.168.106.123 dev lo:0
3、配置linux系统参数
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
测试
tips:如果觉得修改防火墙端口相关的配置过于麻烦,直接关闭就好
用一台新的虚拟机,编写一个脚本测试一下
第一次写完整的配置过程,因为也是初次安装,可能会有一些地方错误,希望发现的朋友可以提出哦(´。• ᵕ •。`)(´。• ᵕ •。`)
网友评论