tongyi007 发表于 2018-12-30 15:15:39

keepalived支持LVS高可用服务以及支持web服务的高可用

keepalived支持LVS高可用服务

准备:四台主机,其中两台为高可用节点,IP地址分别外172.16.86.4、172.16.86.5,两台为real server,IP地址分别为172.16.86.7、172.16.86.8



一、首先让两台高可用节点时间同步,然后下载安装包并安装。我们这里是老师准备好的,你们可以在网上自行下载。
节点1:
# service ntpd stop
# ntpdate 172.16.0.1
# date
Thu May 16 11:01:39 CST 2013
# lftp 172.16.0.1
lftp 172.16.0.1:~> cd pub/Sources/keepalived/
lftp 172.16.0.1:/pub/Sources/keepalived> get get keepalived-1.2.7-5.el5.i386.rpm
# yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm
# scp keepalived-1.2.7-5.el5.i386.rpm node2:/root/


节点2:
# service ntpd stop
# ntpdate 172.16.0.1
# date
Thu May 16 11:01:39 CST 2013
#yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm

二、为两台real server提供web服务提供网页,并配置DR模型。
RS1:
# yum install httpd -y
#echo "RS1.magedu.com" > /var/www/html/index.html
#echo 2 > proc/sys/net/ipv4/conf/all/arp_announce
#echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
#echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# ifconfig lo:0 172.16.86.1 broadcast 172.16.86.1 netmask 255.255.255.255 up
# route add -host 172.16.86.1 dev lo:0
# ifconfig
http://blog.运维网.com/attachment/201305/212107145.png
RS2:
# yum install httpd -y
#echo "RS2.magedu.com" > /var/www/html/index.html
#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
#echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
#echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# ifconfig lo:0 172.16.86.1 broadcast 172.16.86.1 netmask 255.255.255.255 up
# route add -host 172.16.86.1 dev lo:0
# ifconfig
http://blog.运维网.com/attachment/201305/212130249.png
三、编辑两台高可用节点上keepalived的配置文件
节点1:
# cd /etc/keepalived/
# ls
keepalived.confkeepalived.conf.haproxy_examplenotify.sh
# vim keepalived.conf


! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
172.16.86.1/16 dev eth0 label eth0:0
}
}
virtual_server 172.16.86.1 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
protocol TCP
real_server 172.16.86.7 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.86.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
# scp keepalived.conf node2:/etc/keepalived/

节点2:
# vim keepalived.conf

! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
172.16.86.1/16 dev eth0 label eth0:0
}
}
virtual_server 172.16.86.1 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
protocol TCP
real_server 172.16.86.7 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.86.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}


四、在两台高可用节点上启动keepalived服务,并安装ipvsadm工具
# service keepalived start
# service keepalived start
如果节点1成为主的,那么节点1就会有IP地址
http://blog.运维网.com/attachment/201305/212154467.png
# yum install ipvsadm -y
# yum install ipvsadm -y
查看生成的规则:
http://blog.运维网.com/attachment/201305/212209379.png
网页测试:
http://blog.运维网.com/attachment/201305/212225921.png
刷新网页:
http://blog.运维网.com/attachment/201305/212245258.png
测试:如果把RS2的web服务关掉
# service httpd stop
Stopping httpd:                                          
结果:
http://blog.运维网.com/attachment/201305/212301431.png

如果real server都出现故障,此时应该提供一个提示网页,所以要在这两台高可用节点上也安装web服务用来提供一个提示网页。
节点1:
# yum-y install httpd
#echo "Wronging now" > /var/www/html/index.html
# service httpd start
节点2:
2 ~]# yum-y install httpd
2 ~]#echo "Wronging now" > /var/www/html/index.html
2 ~]# service httpd start
配置节点1和节点2的keepalived的配置文件
# vim keepalived.conf
http://blog.运维网.com/attachment/201305/212323826.png

测试:所有real server都出现故障时登陆网页的状况,先把real server 上的web服务全部关掉。
# service httpd stop   ##关闭RS1的web服务
Stopping httpd:                                          
# service httpd stop ##关闭RS2的web服务
Stopping httpd:                                          
登录网页:
http://blog.运维网.com/attachment/201305/212341891.png
此时如果real service上的web服务又重新启动了,那么此时再访问就会去访问real server 上的web服务器提供的网页

模拟高可用节点坏掉的情境,编辑配置文件使其能够手动关闭其中任意一个高可用节点。
# vim keepalived.conf
http://blog.运维网.com/attachment/201305/212400624.png
节点2的配置内容和节点1一样,然后从新启动服务
# service keepalived restart
2 keepalived]# service keepalived restart
节点1:此时地址在节点1
http://blog.运维网.com/attachment/201305/212413839.png
在/etc/keepalived目录下创建一个名为down的文件
# touch down
节点1:
http://blog.运维网.com/attachment/201305/212425299.png
节点2:此时地址已经转移到节点2了
http://blog.运维网.com/attachment/201305/212458382.png
Keepalived支持web服务高可用功能

一、这里就用不到real server,分别在两个节点安装web服务,并提供网页。
# service keepalived stop
# yum -yinstall httpd
# echo "node1" > /var/www/html/index.html
# service httpd start
测试网页:
http://blog.运维网.com/attachment/201305/212512161.png


# service keepalived stop
# yum -yinstall httpd
# echo "node2" > /var/www/html/index.html
# service httpd start
测试网页:
http://blog.运维网.com/attachment/201305/212524749.png
二、编辑节点1的keepalived的配置文件并提供脚本,编辑好后并复制给节点2一份。
# vim keepalived.conf

! Configuration File for keepalived
global_defs {
notification_email {
linuxedu@foxmail.com
mageedu@126.com
}
notification_email_from kanotify@magedu.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_httpd {
script "killall -0 httpd"
interval 2
weight -2
fall 2
rise 1
}
vrrp_script chk_schedown {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 2
weight -2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
priority 101
virtual_router_id 51
garp_master_delay 1
authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
virtual_ipaddress {
172.16.86.1/16 dev eth0 label eth0:0
}
track_script {
chk_httpd
chk_schedown
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}


脚本:在/etc/keepalived目录下提供一个名为notify.sh脚本
# vim notify.sh

#!/bin/bash
# Author: MageEdu
# description: An example of notify script
ifalias=${2:-eth0:0}
interface=$(echo $ifalias | awk -F: '{print $1}')
vip=$(ip addr show $interface | grep $ifalias | awk '{print $2}')
#contact='linuxedu@foxmail.com'
contact='root@localhost'
workspace=$(dirname $0)
notify() {
subject="$ip change to $1"
body="$ip change to $1 $(date '+%F %H:%M:%S')"
echo $body | mail -s "$1 transition" $contact
}
case "$1" in
master)
notify master
exit 0;;
backup)
notify backup
/etc/rc.d/init.d/httpd restart
exit 0;;
fault)
notify fault
exit 0;;
*)
echo 'Usage: $(basename $0) {master|backup|fault}'
exit 1;;
esac

# scp keepalived.conf notify.sh node2:/etc/keepalived/
在节点2修改复制过来的配置文件只需做如下改动。
http://blog.运维网.com/attachment/201305/212537678.png
# service keepalived restart
2 keepalived]# service keepalived restart
http://blog.运维网.com/attachment/201305/212550259.png
登录网页:
http://blog.运维网.com/attachment/201305/212613353.png
手动切换主节点到另外一个节点上。
# touch down
http://blog.运维网.com/attachment/201305/212624483.png
刷新网页:
http://blog.运维网.com/attachment/201305/212636862.png
只要删除/etc/keepalived目录下的down文件主节点还是会切换到节点以1,到此一个结合脚本来提供web服务的高可用功能的服务就完成了


  




页: [1]
查看完整版本: keepalived支持LVS高可用服务以及支持web服务的高可用