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

keepalived支持LVS高可用服務

準備:四臺主機,其中兩臺爲高可用節點,IP地址分別外172.16.86.4172.16.86.5,兩臺爲real serverIP地址分別爲172.16.86.7172.16.86.8 html

1、首先讓兩臺高可用節點時間同步,而後下載安裝包並安裝。咱們這裏是老師準備好的,大家能夠在網上自行下載。 node

節點1 linux

[root@node1 ~]# service ntpd stop web

[root@node1 ~]# ntpdate 172.16.0.1 vim

[root@node1 ~]# date bash

Thu May 16 11:01:39 CST 2013 服務器

[root@node1 ~]# lftp 172.16.0.1 ide

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 oop

[root@node1 ~]# yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm

[root@node1 ~]# scp keepalived-1.2.7-5.el5.i386.rpm node2:/root/

節點2

[root@node2 ~]# service ntpd stop

[root@node2 ~]# ntpdate 172.16.0.1

[root@node2 ~]# date

Thu May 16 11:01:39 CST 2013

[root@node2 ~]#yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm

2、爲兩臺real server提供web服務提供網頁,並配置DR模型。

RS1

[root@localhost ~]# yum install httpd -y

[root@localhost ~]#echo "RS1.magedu.com" > /var/www/html/index.html

[root@localhost ~]#echo 2 > proc/sys/net/ipv4/conf/all/arp_announce

[root@localhost ~]#echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce

[root@localhost ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

[root@localhost ~]#echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore

[root@localhost ~]# ifconfig lo:0 172.16.86.1 broadcast 172.16.86.1 netmask 255.255.255.255 up

[root@localhost ~]# route add -host 172.16.86.1 dev lo:0

[root@localhost ~]# ifconfig

RS2

[root@localhost ~]# yum install httpd -y

[root@localhost ~]#echo "RS2.magedu.com" > /var/www/html/index.html

[root@localhost ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

[root@localhost ~]#echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce

[root@localhost ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

[root@localhost ~]#echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore

[root@localhost ~]# ifconfig lo:0 172.16.86.1 broadcast 172.16.86.1 netmask 255.255.255.255 up

[root@localhost ~]# route add -host 172.16.86.1 dev lo:0

[root@localhost ~]# ifconfig

3、編輯兩臺高可用節點上keepalived的配置文件

節點1

[root@node1 ~]# cd /etc/keepalived/

[root@node1 keepalived]# ls

keepalived.conf  keepalived.conf.haproxy_example  notify.sh

[root@node1 keepalived]# 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
        }
    }
}

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

節點2

[root@node1 keepalived]# 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
        }
    }
}


4、在兩臺高可用節點上啓動keepalived服務,並安裝ipvsadm工具

[root@node1 ~]# service keepalived start

[root@node2 ~]# service keepalived start

若是節點1成爲主的,那麼節點1就會有IP地址

[root@node1 ~]# yum install ipvsadm -y

[root@node2 ~]# yum install ipvsadm -y

查看生成的規則:

網頁測試:

刷新網頁:

測試:若是把RS2web服務關掉

[root@station51 ~]# service httpd stop

Stopping httpd:                                            [  OK  ]

結果:

若是real server都出現故障,此時應該提供一個提示網頁,因此要在這兩臺高可用節點上也安裝web服務用來提供一個提示網頁。

節點1

[root@node1 ~]# yum  -y install httpd

[root@node1 ~]#echo "Wronging now" > /var/www/html/index.html

[root@node1 ~]# service httpd start

節點2

[root@node2 ~]# yum  -y install httpd

[root@node2 ~]#echo "Wronging now" > /var/www/html/index.html

[root@node2 ~]# service httpd start

配置節點1和節點2keepalived配置文件

[root@node1 keepalived]# vim keepalived.conf

測試:全部real server都出現故障時登錄網頁的情況,先把real server 上的web服務所有關掉。

[root@localhost ~]# service httpd stop     ##關閉RS1web服務

Stopping httpd:                                            [  OK  ]

[root@localhost ~]# service httpd stop ##關閉RS2web服務

Stopping httpd:                                            [  OK  ]

登陸網頁:

此時若是real service上的web服務又從新啓動了,那麼此時再訪問就會去訪問real server 上的web服務器提供的網頁

模擬高可用節點壞掉的情境,編輯配置文件使其可以手動關閉其中任意一個高可用節點。

[root@node1 keepalived]# vim keepalived.conf

節點2的配置內容和節點1同樣,而後重新啓動服務

[root@node1 keepalived]# service keepalived restart

[root@node2 keepalived]# service keepalived restart

節點1:此時地址在節點1

/etc/keepalived目錄下建立一個名爲down的文件

[root@node1 keepalived]# touch down

節點1

節點2:此時地址已經轉移到節點2

Keepalived支持web服務高可用功能

1、這裏就用不到real server,分別在兩個節點安裝web服務,並提供網頁。

[root@node1 ~]# service keepalived stop

[root@node1 ~]# yum -y  install httpd

[root@node1 ~]# echo "<h1>node1</h1>" > /var/www/html/index.html

[root@node1 ~]# service httpd start

測試網頁:

[root@node2 ~]# service keepalived stop

[root@node2 ~]# yum -y  install httpd

[root@node2 ~]# echo "<h1>node2</h1>" > /var/www/html/index.html

[root@node1 ~]# service httpd start

測試網頁:

2、編輯節點1keepalived的配置文件並提供腳本,編輯好後並複製給節點2一份。

[root@node1 keepalived]# 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腳本

[root@node1 keepalived]# vim notify.sh

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# 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


[root@node1 keepalived]# scp keepalived.conf notify.sh node2:/etc/keepalived/

在節點2修改複製過來的配置文件只需作以下改動。

[root@node1 keepalived]# service keepalived restart

[root@node2 keepalived]# service keepalived restart

登陸網頁:

手動切換主節點到另一個節點上。

[root@node1 keepalived]# touch down

刷新網頁:

只要刪除/etc/keepalived目錄下的down文件主節點仍是會切換到節點以1,到此一個結合腳原本提供web服務的高可用功能的服務就完成了

相關文章
相關標籤/搜索