負載均衡器: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的情況