LVS模式
LVS的组成
LVS中每个主机IP地址的定义
LVS的四种转发模式
DR
DR模式
1 首先在关闭虚拟机server1上的varnish
编写yum源的配置文件如图,再进行yum install ipvsadm -y 安装
调度算法 – rr轮询
编写策略并保存如图:
给三台机子给vip
测试:
两台测试机都好着
当某台测试机宕机(server2) 如图:(也就是停掉httpd后)
arp -d 是删除之前的缓存。
1 server1 配置yum源如图:再安装 ldirectord-3.9.5-3.1.x86_64.rpm
查看配置文件,并且复制到/etc/ha.d中
一 编写配置文件如图:vim ldirectord.cf
修改server1的httpd 文件中的端口为80(因为之前的实验改为了8080)
关掉配置的 ipvsadm服务,开启httpd服务
编写一个server1的index.html
二 编辑配置文件
开启服务:
查看策略是否成功如图:
测试:如图
当关闭server2 3 的httpd后 ,也就是两个都宕机后会出现server1中index.html的内容
三 Keepalived(高可用)
安装Keepalived
之后操作如下:
然后执行命令make install
在虚拟机server1中安装mailx (yum install mailx)因为等会改配置文件中要用到此功能
server1上编写配置文件
查看策略:
再server4上修改如下:
当主机关闭keepalived时,server4开启时,server4上会自动加上ip/32
当主机开启keepalived时,server4开启时,server1上会自动加上ip/32 而server4上没有
由此可见,keepalived服务时 ,当主的停掉时,辅助的会自动补上,当主的开启时,辅助的则不影响。这样以来客户端的测试不会受到影响。lvs+keepalived达到了真正的负载均衡,高可用。
当server1上同时作两个服务的轮询时,配置文件如下: 首先给server2 和server3 安装vsftp服务开启它,并开启httpd
[root@server1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@loclhost
}
notification_email_from keepalived@localhost
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 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100
172.25.254.200
}
}
virtual_server 172.25.254.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 172.25.254.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 172.25.254.200 21 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 172.25.254.2 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
然后执行命令/etc/init.d/keepaliverd restart
查看ip addr
测试:结果如图:
实验两个虚拟机的之间两种服务的间高可用。
给server2 和server3 安装vsftp服务开启它,并开启httpd
再server1 上 编写 vim keepalived.conf:内容如下
然后重启服务 /etc/init.d/keepalived restart
! Configuration File for keepalived
global_defs {
notification_email {
root@loclhost
}
notification_email_from keepalived@localhost
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 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100
}
}
virtual_server 172.25.254.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 172.25.254.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.200
}
}
virtual_server 172.25.254.200 21 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 172.25.254.2 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
再server4 上编写vim keepalived.conf:内容如下
然后重启服务 /etc/init.d/keepalived restart
! Configuration File for keepalived
global_defs {
notification_email {
root@loclhost
}
notification_email_from keepalived@localhost
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 BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100
}
}
virtual_server 172.25.254.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 172.25.254.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.200
}
}
virtual_server 172.25.254.200 21 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 172.25.254.2 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.254.3 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
之后查看ip addr (100/32 的再server1上,200/32的再server4上)
测试:由于keepalived的开启,server1和sserver4互相形成主副的关系
当server2的vsftpd服务关闭时,会自动去找server3
当server3的vsftpd关闭,server2的开启会自动去找server2
当server1上的keepalived服务停止运行,server4就会自动补上去,不影响客户端的体验。
再server1上的keepalived重新开启后,server4则会自动退去,继续由server1来支持它的轮询。