1.Keepalived高可用服务器
proxy:192.168.4.5(客户端主机)
web1:192.168.4.100(Web服务器,部署Keepalived高可用软件)
web2:192.168.4.200(Web服务器,部署Keepalived高可用软件)
Web服务器的浮动VIP地址为192.168.4.80
客户端通过访问VIP地址访问Web页面
1.1 部署两台web服务器页面:
web1 ~]# echo "192.168.4.100" > /var/www/html/index.html
web2 ~]# echo "192.168.4.200" > /var/www/html/index.html
web1 ~]# systemctl restart httpd
web2 ~]# systemctl restart httpd
1.2 web1-2安装Keepalived软件
web1 ~]# yum install -y keepalived
web2 ~]# yum install -y keepalived
1.3 部署Keepalived服务(web1-2)
1.3.1 web1:主服务器
***********************
配置介绍:
]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@xxx.com.cn //设置报警收件人邮箱
}
notification_email_from ka@localhost //设置发件人
smtp_server 127.0.0.1 //定义邮件服务器
smtp_connect_timeout 30
router_id web1 //设置路由ID号(修改后)
}
vrrp_instance VI_1 {
state MASTER //主服务器为MASTER(备服务器需要修改为BACKUP)
interface eth0 //定义网络接口
virtual_router_id 50 //主备服务器VRID号必须一致
priority 100 //服务器优先级,优先级高优先获取VIP(已修改)
advert_int 1
authentication {
auth_type pass
auth_pass 1111 //主备服务器密码必须一致
}
virtual_ipaddress { 192.168.4.80 } //谁是主服务器谁获得该VIP(已修改)
}
***************************
配置web1:
]# vim /etc/keepalived/keepalived.conf
! 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 web1(已改)
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER(主服务器)
interface eth0
virtual_router_id 51(修改后)
priority 100(优先级比BACKUP高)
advert_int 1
authentication { (不要修改,但是要一致)
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.80 (浮动IP)
}
}
##999dd 以下暂时不要
1.3.2 web2:备用服务器
]# vim /etc/keepalived/keepalived.conf
! 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 web2(要改)
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP(此处为备用服务器)
interface eth0
virtual_router_id 51(修改后)
priority 80(优先级比master低)
advert_int 1
authentication { (不要修改,但是要一致)
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.80 (浮动IP)
}
}
##999dd 以下暂时不要
1.3.3 web1-2启动服务(都要操作)
]# systemctl start keepalived
启动keepalived会自动添加一个drop的防火墙规则,需要清空!
]# iptables -F
1.3.4 测试(web1-2)
查看浮动IP(192.168.4.80)
]# ip a s eth0
此时浮动IP在web1
proxy 测试:
]# curl http://192.168.4.80
192.168.4.100
...
关闭web1的网卡,查看备份服务情况
web1]# ifconfig eth0 down
查看浮动IP(192.168.4.80)
]# ip a s eth0
此时浮动IP在web2
proxy 测试:
]# curl http://192.168.4.80
192.168.4.200
...
恢复web1网卡,会恢复到web1.
2.Keepalived+LVS服务器
使用Keepalived为LVS调度器提供高可用功能,防止调度器单点故障,为用户提供Web服务:
LVS1调度器真实IP地址为192.168.4.5
LVS2调度器真实IP地址为192.168.4.6
服务器VIP地址设置为192.168.4.15
真实Web服务器地址分别为192.168.4.100、192.168.4.200
使用加权轮询调度算法,真实web服务器权重不同
方案:使用5台虚拟机,1台作为客户端主机、2台作为LVS调度器、2台作为Real Server,实验拓扑环境结构如图-2所示,基础环境配置如表-2所示。


2.1 web1-2配置VIP地址
2.1.1 web1配置VIP地址
]# cd /etc/sysconfig/network-scripts/
]# cp ifcfg-lo{,:0}
]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.15
NETMASK=255.255.255.255
NETWORK=192.168.4.15
BROADCAST=192.168.4.15
ONBOOT=yes
NAME=lo:0
#手动写入如下4行内容
]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.15时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.15
]# systemctl restart network
2.1.2 web2配置VIP地址
]# cd /etc/sysconfig/network-scripts/
]# cp ifcfg-lo{,:0}
]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.15
NETMASK=255.255.255.255
NETWORK=192.168.4.15
BROADCAST=192.168.4.15
ONBOOT=yes
NAME=lo:0
#手动写入如下4行内容
]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.15时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.15
]# systemctl restart network
2.2 配置后端Web服务器页面
web1 ~]# echo "192.168.4.100" > /var/www/html/index.html
web2 ~]# echo "192.168.4.200" > /var/www/html/index.html
web1 ~]# systemctl restart httpd
web2 ~]# systemctl restart httpd
2.3 调度器安装Keepalived与ipvsadm软件(两台LVS调度器执行相同的操作)
proxy1,proxy2
]# yum install -y keepalived
]# systemctl enable keepalived
]# yum install -y ipvsadm
]# ipvsadm -C
2.4 部署Keepalived实现LVS-DR模式调度器的高可用
2.4.1 LVS1调度器设置Keepalived,并启动服务(web1)
global_defs {
notification_email {
admin@tarena.com.cn
}
notification_email_from ka@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs1 //设置路由ID号
}
vrrp_instance VI_1 {
state MASTER //主服务器为MASTER
interface eth0
virtual_router_id 51 //主辅VRID号必须一致
priority 100 //服务器优先级
advert_int 1
authentication {
auth_type pass
auth_pass 1111 //主辅服务器密码必须一致
}
virtual_ipaddress { 192.168.4.15 } //配置VIP
}
virtual_server 192.168.4.15 80 { //设置ipvsadm的VIP规则
delay_loop 6
lb_algo wrr //设置LVS调度算法为WRR
lb_kind DR //设置LVS的模式为DR
#persistence_timeout 50
#注意这样的作用是保持连接,开启后,客户端在一定时间内始终访问相同服务器
protocol TCP
real_server 192.168.4.100 80 { //设置后端web服务器真实IP
weight 1 //设置权重为1
TCP_CHECK { //对后台real_server做健康检查
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.200 80 { //设置后端web服务器真实IP(实验需要修改)
weight 2 //设置权重为2
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
]# systemctl start keepalived
]# ipvsadm -Ln #查看LVS规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.4.15:80 wrr
-> 192.168.4.100:80 Route 1 0 0
-> 192.168.4.200:80 Route 2 0 0
]# ip a s eth0 #查看VIP配置(浮动IP在web1)
2.4.2 LVS2调度器设置Keepalived,并启动服务(web2)
global_defs {
notification_email {
admin@tarena.com.cn
}
notification_email_from ka@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs2 //设置路由ID号
}
vrrp_instance VI_1 {
state BACKUP //从服务器为BACKUP
interface eth0
virtual_router_id 51 //主辅VRID号必须一致
priority 50 //服务器优先级
advert_int 1
authentication {
auth_type pass
auth_pass 1111 //主辅服务器密码必须一致
}
virtual_ipaddress { 192.168.4.15 } //配置VIP
}
virtual_server 192.168.4.15 80 { //设置ipvsadm的VIP规则
delay_loop 6
lb_algo wrr //设置LVS调度算法为WRR
lb_kind DR //设置LVS的模式为DR
#persistence_timeout 50
#注意这样的作用是保持连接,开启后,客户端在一定时间内始终访问相同服务器
protocol TCP
real_server 192.168.4.100 80 { //设置后端web服务器真实IP
weight 1 //设置权重为1
TCP_CHECK { //对后台real_server做健康检查
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.200 80 { //设置后端web服务器真实IP(实验需要修改)
weight 2 //设置权重为2
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
]# systemctl start keepalived
]# ipvsadm -Ln #查看LVS规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.4.15:80 wrr
-> 192.168.4.100:80 Route 1 0 0
-> 192.168.4.200:80 Route 2 0 0
]# ip a s eth0 #查看VIP配置(浮动IP在web1)
2.4.3 客户端测试(client)
]# curl http://192.168.4.15
192.168.4.100
]# curl http://192.168.4.15
192.168.4.200
]# curl http://192.168.4.15
192.168.4.200
...
3.配置HAProxy负载平衡集群
3.配置HAProxy负载平衡集群
准备4台Linux服务器,两台做Web服务器,1台安装HAProxy,1台做客户端,实现如下功能:
客户端访问HAProxy,HAProxy分发请求到后端Real Server
开启HAProxy监控页面,及时查看调度器状态
设置HAProxy为开机启动
3.1 部署两台web服务器页面:


web1 ~]# echo "192.168.2.100" > /var/www/html/index.html
web2 ~]# echo "192.168.2.200" > /var/www/html/index.html
web1 ~]# systemctl restart httpd
web2 ~]# systemctl restart httpd
3.2 部署HAProxy服务器
3.2.1 配置网络,安装软件
]# echo 'net.ipv4.ip_forward = 1' >> sysctl.conf //开启路由转发
]# sysctl -p
]# yum -y install haproxy
3.2.2 修改配置文件
]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2 ###[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路径
maxconn 4000 ###最大连接数,默认4000
user haproxy
group haproxy
daemon ###创建1个进程进入deamon模式运行
defaults
mode http ###默认的模式mode { tcp|http|health } log global ###采用全局定义的日志
option dontlognull ###不记录健康检查的日志信息
option httpclose ###每次请求完毕后主动关闭http通道
option httplog ###日志类别http日志格式
option forwardfor ###后端服务器可以从Http Header中获得客户端ip
option redispatch ###serverid服务器挂掉后强制定向到其他健康服务器
timeout connect 10000 #如果backend没有指定,默认为10s
timeout client 300000 ###客户端连接超时
timeout server 300000 ###服务器连接超时
maxconn 60000 ###最大连接数
retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置
listen stats
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
listen websrv-rewrite 0.0.0.0:80(此处要用80端口!)
balance roundrobin
server web1 192.168.2.100:80 check inter 2000 rise 2 fall 5
server web2 192.168.2.200:80 check inter 2000 rise 2 fall 5
3.2.3 启动服务器并设置开机启动
]# netstat -antulp | grep :80(查看是否占用,占用则停掉)
]# systemctl start haproxy
]# netstat -antulp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9094/haproxy
]# systemctl enable haproxy
3.2.3 client访问
]# firefox http://192.168.4.5:1080/stats
备注:
Queue队列数据的信息(当前队列数量,最大值,队列限制数量);
Session rate每秒会话率(当前值,最大值,限制数量);
Sessions总会话量(当前值,最大值,总量,Lbtot: total number of times a server was selected选中一台服务器所用的总时间);
Bytes(入站、出站流量);
Denied(拒绝请求、拒绝回应);
Errors(错误请求、错误连接、错误回应);
Warnings(重新尝试警告retry、重新连接redispatches);
Server(状态、最后检查的时间(多久前执行的最后一次检查)、权重、备份服务器数量、down机服务器数量、down机时长)。
ab访问测试:
]# ab -c 10000 -n 1000 http://192.168.4.5/
测试健康检查:停用web1
]# systemctl stop httpd
]# firefox http://192.168.4.5:1080/stats #web1会变红