keepalived+haproxy實現高可用負載均衡集羣


keepalived+haproxy 實現高可用、負載均衡服務器集羣前端




實驗環境:iptablesselinux關閉linux

前端高可用負載均衡調度主機:vm7 172.25.28.7   vm8 172.25.28.8web

後臺應用服務器:                   vm1 172.25.28.1   vm2 172.25.28.4redis

配置以下:#######################################################################瀏覽器

分別在兩臺調度主機上面修改/etc/haproxy/haproxy.cfbash

global服務器

log         127.0.0.1 local2負載均衡

chroot      /var/lib/haproxysocket

pidfile     /var/run/haproxy.pid測試

maxconn     4000

user        haproxy

group       haproxy

daemon

stats socket /var/lib/haproxy/stats

defaults

mode                    http

log                     global

option                  httplog

option                  dontlognull

option http-server-close

option forwardfor       except 127.0.0.0/8

option                  redispatch

retries                 3

timeout http-request    10s

timeout queue           1m

timeout connect         10s

timeout client          1m

timeout server          1m

timeout http-keep-alive 10s

timeout check           10s

maxconn                 3000


listen stats_auth *:8080

stats enable

stats uri /status

stats auth admin:westos

stats refresh 5s

listen www.westos.org *:80

balance roundrobin

server web1 172.25.28.1:80 check weight 3

server web2 172.25.28.4:80 check weight 1

server backup 127.0.0.1:8081 backup


兩臺調度主機:分別修改/etc/keepalived/keepalived.conf

MASTER節點:

! Configuration File for keepalived

vrrp_script check_haproxy {      #使用haproxy_check 腳本對後臺服務器作一個健康檢查

script "/opt/haproxy_check"    #此處目錄爲腳本的絕對路徑

interval 5

weight 2

}

global_defs {

notification_email {

root@localhost

}

notification_email_from keepalived@vm7.example.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER     #備機的爲BACKUP

interface eth0

virtual_router_id 51

priority 100                      #兩臺主備調度器的值設置必須不同

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.25.28.100   #對外虛擬ip

}

track_script {

check_haproxy

}

}



BACKUP:

! Configuration File for keepalived

vrrp_script check_haproxy {

script "/opt/haproxy_check"

interval 5

weight 2

}

global_defs {

notification_email {

root@localhost

}

notification_email_from keepalived@vm7.example.com

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 50     注意修改此處

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.25.28.100     #對外虛擬ip

}

track_script {

check_haproxy

}

}


分別在兩臺調度主機上面操做:

添加檢測後臺服務器健康情況的腳本:在/etc/keepalived/keepalived.conf 中的對應目錄下添加文件/opt/haproxy_check

#!/bin/bash

/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null

if [ $? -ne 0 ];then

/etc/init.d/keepalived stop &> /dev/null

fi



測試:分別在兩臺後臺應用服務器上面開啓httpd服務,並寫上首頁文件。 而且要在安裝瀏覽器的主機的/etc/hosts下面寫解析。

1.高可用測試:中止master上的keepalived服務,查看backup是否會接管而且根據腳本會不會自動啓動haproxy服務

2.負載均衡測試:訪問www.westos.org,看到頁面在兩個後臺服務器上切換表示成功!

3.故障切換測試:任意關閉後臺服務器上面的httpd服務,查看是否haproxy能夠及時發現故障主機,而且將服務轉移到正常節點上來執行。

相關文章
相關標籤/搜索