负载均衡器:nginx/haproxy/lvs/F5
代理:
正向代理:帮助客户端缓存服务器上的数据
反向代理:帮助服务器缓存数据
HAProxy:
1、安装
[[email protected] bin]# yum install -y haproxy
2、修改配置文件
[[email protected] bin]# vim /etc/haproxy/haproxy.cfg
把# main frontend which proxys to the backends后面部分全部删除,增加以下内容:
定义一个监控页面
listen stats
bind 0.0.0.0:1080
stats refresh 30s
stats uri /mystats
stats realm Ha Manager
stats auth admin:admin
listen web-discuz 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server web1 192.168.4.2:80 cookie a1i1 check inter 2000 rise 2 fall
5
server web2 192.168.4.3:80 cookie a1i2 check inter 2000 rise 2 fall 5
3、启服务
[[email protected] bin]# systemctl start haproxy
4、访问http://192.168.4.4可以实现负载均衡轮询调度,访问http://192.168.4.4:1080/mystats可以看到监控页面
配置vh04为日志服务器,以便于可以接收到haproxy通过网络发来的日志
1、配置vh04接受网络发来的日志
[[email protected] bin]# vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
2、重启日志服务
[[email protected] bin]# systemctl restart rsyslog
3、跟踪日志尾部,访问http://192.168.4.4可以看到日志
[[email protected] bin]# tail -f /var/log/messages
可以执行logger命令,向syslog写日志,如
[[email protected] bin]# logger "my test log"
[[email protected] bin]# tail -2 /var/log/messages
HSRP:热备份路由协议,cisco私有
VRRP:虚拟冗余路由协议,IETF(Internet工程师任务组)共公标准
HA:高可用
心跳:HearBeat,相当于是路由器设备上的hello消息
双机热备:keepalived
一、实现高可用的web集群
1、拓扑:两台Web服务器,一台数据库服务器。
2、在web服务器上安装keepalived
[[email protected] ~]# yum install -y keepalived
3、修改配置
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
# vrrp_strict 注释掉这一行
vrrp_instance VI_1 {
state MASTER 从属服务器改为BACKUP
interface eth0
virtual_router_id 51 虚拟路由器ID
priority 150 优先级
advert_int 1 心跳消息1s发一个
authentication { 两边的共享密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { 虚拟IP地址
192.168.4.200
}
}
后续内容全部删除
4、启服务
[[email protected] ~]# systemctl start keepalived
5、查看虚拟ip地址
[[email protected] ~]# ip address show eth0
6、验证:把vh02的keepalived停掉,vip将出现在vh03上
配置高可用、负载均衡的web集群
1、创建虚拟机vh05(用作额外的调度器)
Vh05.tedu.cn 192.168.4.5/24 selinux/firewall/yum
2、清除vh04上lvs的规则,因为规则将由keepalived配置
[[email protected] bin]# ipvsadm -D -t 192.168.4.100:80
3、在vh05上安装lvs
[[email protected] ~]# yum install -y ipvsadm
4、web服务器需要在lo上配置vip,需要修改内核参数
5、不要在调度器上手工配置VIP,因为VIP由keepalived决定出现在哪台调度器上。所以要把vh04的VIP清除
[[email protected] bin]# ifdown eth0:0
[[email protected] bin]# rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:0
6、在调度器上安装keepalived
[[email protected] bin]# yum install -y keepalived
7、修改配置
[[email protected] bin]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
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 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.100
}
}
virtual_server 192.168.4.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50 # 50秒内,相同客户端总是调度到相同服务器
protocol TCP
real_server 192.168.4.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
删除配置文件后续内容
8、启动服务
[[email protected] bin]# systemctl start keepalived
9、备份lvs调度器启动后,没有VIP。可以把vh04关机,再查看vh05的情况