本次实验基于DR负载均衡模式(直接路由),设置一个VIP(Virtual IP)为192.168.1.225,用户只需要访问这个IP地址即可获得网页服务。其中,负载均衡主机为192.168.1.221(master),备机为 192.168.1.222(slave)。Web服务器A为192.168.1.223(web01),Web服务器B为192.168.1.220(web02)
配置两台web服务器
[root@web01 ~]# systemctl stop firewalld [root@web01 ~]# setenforce 0 [root@web02 ~]# service iptables stop iptables:将链设置为政策 ACCEPT:filter [确定] iptables:清除防火墙规则:[确定] iptables:正在卸载模块:[确定] [root@web02 ~]# setenforce 0
[root@web01 ~]# yum -y install httpd
[root@web02 ~]# yum -y install httpd
[root@web01 ~]# systemctl start httpd [root@web01 ~]# echo "192.168.1.223" > /var/www/html/index.html [root@web01 ~]# curl 192.168.1.223 192.168.1.223
[root@web02 ~]# service httpd start 正在启动 httpd:httpd: apr_sockaddr_info_get() failed for web02 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [确定] [root@web02 ~]# echo "192.168.1.220" > /var/www/html/index.html [root@web02 ~]# curl 192.168.1.220 192.168.1.220
将这两台web服务器都配置成lvs的real server,编辑realserver脚本文件,进入指定文件夹:cd /etc/init.d/,编辑脚本文件:vim realserver
[root@web01 ~]# cat /etc/rc.d/init.d/realserver #!/bin/bash # description: Config realserver lo and apply noarp #Written by :NetSeek http://www.linuxtone.org SNS_VIP=192.168.1.225 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
[root@web01 ~]# cd /etc/init.d/ [root@web01 init.d]# chmod 755 realserver [root@web01 init.d]# service realserver start Reloading systemd: [ 确定 ] Starting realserver (via systemctl): [ 确定 ] [root@web02 ~]# cd /etc/init.d/ [root@web02 init.d]# chmod 755 realserver [root@web02 init.d]# service realserver start RealServer Start OK
注意:配置real server是web服务器都需要配置的,有多少台就配置多少台!
配置负载服务器(master)
[root@master ~]# yum install -y keepalived ipvsadm [root@master ~]# cd /etc/keepalived/ [root@master keepalived]# ls keepalived.conf [root@master keepalived]# cp keepalived.conf keepalived.conf.bak [root@master keepalived]# echo "" > keepalived.conf
[root@master keepalived]# cat keepalived.conf
global_defs {
notification_email {
1350748936@qq.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 192.168.1.114
smtp_connection_timeout 30
router_id LVS_MASTER # 设置lvs的id,在一个网络应该是唯一的
}
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 66 # 虚拟路由编号,主从要一直
priority 100 # 优先级,数值越大,获取处理请求的优先级越高
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.1.221 ##自己ip
unicast_peer { ##单播模式
192.168.1.222 ##另一个ip
}
virtual_ipaddress {
192.168.1.225 # 定义虚拟ip(VIP),可多设,每行一个
}
}
# 定义对外提供的LVS的VIP以及port
virtual_server 192.168.1.225 80 {
delay_loop 6 # 设置健康检查时间,单位为秒
lb_algo wrr # 设置负载调度的算法为wrr
lb_kind DR # 设置lvs实现负载的机制,有NAT、TUN、DR三个模式
nat_mask 255.255.255.0
persistence_timeout 0 # 同一IP 0秒内的请求都发到同个real server
protocol TCP
real_server 192.168.1.223 80 { # 指定real server1的ip地址
weight 3 # 配置节点权值,数值越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.220 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]# systemctl start keepalived
配置负载服务器(slave)
[root@slave ~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
1350748936@qq.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 192.168.1.114
smtp_connection_timeout 30
router_id LVS_BACKUP # 设置lvs的id,在一个网络应该是唯一的
}
vrrp_instance VI_1 {
state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 66 # 虚拟路由编号,主从要一直
priority 90 # 优先级,数值越大,获取处理请求的优先级越高
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.1.222 ##自己ip
unicast_peer { ##单播模式
192.168.1.221 ##另一个ip
}
virtual_ipaddress {
192.168.1.225 # 定义虚拟ip(VIP),可多设,每行一个
}
}
# 定义对外提供的LVS的VIP以及port
virtual_server 192.168.1.225 80 {
delay_loop 6 # 设置健康检查时间,单位为秒
lb_algo wrr # 设置负载调度的算法为wrr
lb_kind DR # 设置lvs实现负载的机制,有NAT、TUN、DR三个模式
nat_mask 255.255.255.0
persistence_timeout 0 # 同一IP 0秒内的请求都发到同个real server
protocol TCP
real_server 192.168.1.223 80 { # 指定real server1的ip地址
weight 3 # 配置节点权值,数值越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.220 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@slave ~]# systemctl restart keepalived