keepalived lvs 實現lvs高可用


keepalived 簡介

keepalived是借用VRRP協議來實現高可用性的,VRRP協議是解決單點故障,使路由器和層三交換機實現冗余功能。

untitled

keepalived啟動后會有三個進程

父進程:內存管理,子進程管理等等

子進程:VRRP子進程

子進程:healthchecker子進程

從圖上可以看出,兩個子進程都被系統WatchDog看管,兩個子進程各自操作自己的事,healthchecker子進程負責檢查各自服務器的健康程度,例如HTTP,LVS等等,如果healthchecker子進程檢查到MASTER上服務不可用了,就會通知本機上的VRRP子進程,讓他刪除通告,並且去掉虛擬IP,轉換為BACKUP狀態

 

環境准備

1 操作系統

                   centos6.4 x86_64  

2 實驗拓撲

image

3 服務器時間同步

[root@node1 ~]#ntpdate ntp.api.bz

[root@node2 ~]#ntpdate ntp.api.bz

[root@web1 ~]#ntpdate ntp.api.bz

[root@web2 ~]#ntpdate ntp.api.bz

 

4 安裝keepalived ipvsadm

[root@node1 ~]# yum install keepalived ipvsadm

 

5 修改配置文件

! 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 LVS_DEVEL
}

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 {
192.168.200.10
}
}
virtual_server 192.168.200.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
net_mask 255.255.255.0
protocol TCP
real_server 192.168.200.138 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 192.168.200.139 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

 

6 復制配置文件到從服務器

[root@node1 ~]# scp /etc/keepalived/keepalived.conf 192.168.200.18:/etc/keepalived/keepalived.conf

7 從服務器 安裝keepalived ipvsadm

[root@node2 ~]# yum install keepalived ipvsadm

 

8 修改配置文件

[root@node2 ~]# vim /etc/keepalived/keepalived.conf 
state BACKUP
priority 99

 

9 開啟服務

[root@node1 ~]# service keepalived start
[root@node1 ~]# chkconfig --add keepalived
[root@node1 ~]# chkconfig keepalived on

 

[root@node2 ~]# service keepalived start
[root@node2 ~]# chkconfig --add keepalived
[root@node2 ~]# chkconfig keepalived on

 

10  安裝 http

[root@web1 ~]# yum install httpd
[root@web2 ~]# yum install httpd

 

11 建立測試網頁

[root@web1 html]# cd /var/www/html/
[root@web1 html]# vim index.html
this is web1
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# vim index.html
this is web2

 

12 啟動服務

[root@web1 html]# service httpd start
[root@web1 html]# chkconfig --add httpd
[root@web1 html]# chkconfig httpd on
[root@web2 html]# service httpd start
[root@web2 html]# chkconfig --add httpd
[root@web2 html]# chkconfig httpd on

 

13 修改內存參數 配置vip

[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web1 ~]# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up
[root@web1 ~]# route add -host 192.168.200.10 dev lo:0

 

[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web2 ~]# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up
[root@web2 ~]# route add -host 192.168.200.10 dev lo:0

 

14 查看相關vip  ipvs規則

[root@node1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3e:54:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.17/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.10/32 scope global eth0
inet6 fe80::20c:29ff:fe3e:5430/64 scope link
valid_lft forever preferred_lft forever

 

[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0 0

 

15 測試

web1

image

web2

image

停止master的keepalived服務 ,vip ipvs規則會切換到backup服務器上

[root@node1 ~]# service keepalived stop

[root@node2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:af:d6:a0 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.18/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.10/32 scope global eth0
inet6 fe80::20c:29ff:feaf:d6a0/64 scope link
valid_lft forever preferred_lft forever

[root@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0 0

 

停止web1服務 ipvs規則會清除web1服務器

[root@web1 ~]# service httpd stop

[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.10:80 wrr
-> 192.168.200.139:80 Route 1 0

 

開啟web1服務 ipvs自動會添加web1服務器

[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.200.10:80 wrr
  -> 192.168.200.138:80           Route   1      0          0        
  -> 192.168.200.139:80           Route   1      0 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com