1.目的node
經過此做業指導書,知道如何使用ubuntu+haproxy+heartbeat搭建大規模WEB集羣環 境,實現負載均衡。linux
2.適用範圍web
全部系統管理員。redis
3.定義算法
l Ubuntu 開源的LINUX服務器操做系統.apache
l Haproxy Haproxy提供高可用性、負載均衡 以及基於TCP和HTTP應用ubuntu
的代理,它是免費、快速而且可靠的一種解決方案後端
l Heartbeat 用於實現故障轉移,當一臺機器DOWN後自動切換到備用機器安全
l Web服務器 包括Apache,Lighttpd,Nginx服務器
l Openfiler 這是一個基於LINUX的NAS,用來創建Iscsi服務器
一 環境
1.硬件
服務器:
DELL 2950 1臺
DELL 1950 1臺
2U組裝機 2臺
1U組裝機 1臺
交換機:
H3C千M交換機
2.網絡環境
· Load Balancer 1: lb1.ylmf.com, IP address: 192.168.1.100 eth0
· Load Balancer 2: lb2.ylmf.com, IP address: 192.168.1.101 eth0
· Web Server 1: web1.ylmf.com, IP address: 192.168.1.102 eth0
· Web Server 2: web2.ylmf.com, IP address: 192.168.1.103 eth0
· Iscsi target:openfiler.ylmf.com,IP address:192.168.1.104 eth0
· lb1 and lb2共享IP: 192.168.1.99
3.所須要的軟件
操做系統:Ubuntu server 8.0.4
Lb1和lb2:haproxy heartbeat
Web1和web2:Apache2
Iscsi target:Openfiler
二 Web服務器配置
在web1和 web2上配置
1.設置自動登陸到iscsi target(openfiler)
安裝open-iscsi
Apt-get install open-iscsi
發現iscsi-target
iscsiadm -m discovery -t sendtargets -p 192.168.1.104
設置開機自動登陸到iscsi-target
iscsiadm -m node -T iqn.2006-01.com.openfiler:tsn.6f5d0fb29fc9 -p 192.168.1.104 –op update -n node.startup -v automatic
說明:-T後面的字符串是執行發現iscsi-target所得到的鏈接字符串
不少時候設置自動登陸不成功,是由於在/etc/rc.local裏面設置了dhclient
自動獲取IP,這樣自動登陸到ISCSI的時候網絡是不通的,因此不成功。
須要在/etc/network/interfaces裏面設置自動獲取IP地址
如: auto eth0
iface eth0 inet dhcp
2.格式化ISCSI磁盤並設置自動掛載
使用fdisk –l查看磁盤分區
fdisk -l
說明:/dev/sdb 164.4GB是iscsi磁盤
fdisk /dev/sdb #對磁盤進行分區
mkfs.ext3 /dev/sdb1 #將sdb1分區格式化爲ext3文件系統
創建掛載目錄
mkdir /iscsi
開機自動掛載
vi /etc/fstab
/dev/sdb1 /iscsi ext3 relatime 0 2 #增長這一行
3.安裝web server
Apt-get install apache2
2)修改web服務器日誌格式
Vi /etc/apache2/apache2.conf
#LogFormat 「%h %l %u %t \」%r\」 %>s %b \」%{Referer}i\」 \」%{User-Agent}i\」" combined
LogFormat 「%{X-Forwarded-For}i %l %u %t \」%r\」 %>s %b \」%{Referer}i\」 \」%{User-Agent}i\」" combined
註釋掉原來的CustomLog,並修改文檔根路徑爲/iscsi/www
vi /etc/apache2/sites-available/default
SetEnvIf Request_URI 「^/check\.txt$」 dontlog
CustomLog /var/log/apache2/access.log combined env=!dontlog
/etc/init.d/apache2 restart #重啓web服務器
4)創建check文件,haproxy使用該文件檢查WEB狀態,必須創建
Touch /iscsi/www/check.txt
Web服務器配置完成,很簡單,lb1和lb2就要複雜一些
三配置負載均衡haproxy
在lb1和lb2上配置
1.修改hosts文件
Vi /etc/hosts
192.168.1.100 lb1.ylmf.com
192.168.1.101 lb2.ylmf.com
192.168.1.102 web1.ylmf.com
192.168.1.103 web2.ylmf.com
2.安裝軟件
Apt-get install haproxy
3.修改haproxy配置文件
Cp /etc/haproxy.cfg /etc/haproxy.cfg.bak
Cat /dev/null > /etc/haproxy.cfg
Vi /etc/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#debug
#quiet
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webfarm 192.168.1.99:80
mode http
stats enable
stats auth ylmf:ylmf
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
server webA 192.168.1.102:80 cookie A check
server webB 192.168.1.103:80 cookie B check
4.修改默認配置,讓haproxy開機自動啓動
Vi /etc/default/haproxy
# Set ENABLED to 1 if you want the init script to start haproxy.
ENABLED=1
# Add extra flags here.
#EXTRAOPTS=」-de -m 16″
四配置heartbeat高可用
Active/Standby 模式
Lb1和lb2上配置
1.安裝
Apt-get install heartbeat
2.修改內核參數
Vi /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1
sysctl –p #讓設置生效
3.設置受權KEY
vi /etc/ha.d/authkeys
auth 3
3 md5 somerandomstring
chmod 600 /etc/ha.d/authkeys #修改權限
4.添加ha資源
vi /etc/ha.d/haresources
lb1.ylmf.com 192.168.1.99
在lb1上配置
vi /etc/ha.d/ha.cf
# # keepalive: how many seconds between heartbeats # keepalive 2 # # deadtime: seconds-to-declare-host-dead # deadtime 10 # # What UDP port to use for udp or ppp-udp communication? # udpport 694 bcast eth0 mcast eth0 225.0.0.1 694 1 0 ucast eth0 192.168.1.101 #這個IP是lb2的IP # What interfaces to heartbeat over? udp eth0 # # Facility to use for syslog()/logger (alternative to log/debugfile) # logfacility local0 # # Tell what machines are in the cluster # node nodename … — must match uname -n node lb1.ylmf.com node lb2.ylmf.com |
在lb2上配置
vi /etc/ha.d/ha.cf
# # keepalive: how many seconds between heartbeats # keepalive 2 # # deadtime: seconds-to-declare-host-dead # deadtime 10 # # What UDP port to use for udp or ppp-udp communication? # udpport 694 bcast eth0 mcast eth0 225.0.0.1 694 1 0 ucast eth0 192.168.1.100 #這個IP是lb1的IP # What interfaces to heartbeat over? udp eth0 # # Facility to use for syslog()/logger (alternative to log/debugfile) # logfacility local0 # # Tell what machines are in the cluster # node nodename … — must match uname -n node lb1.ylmf.com node lb2.ylmf.com |
5.設置開機自動啓動heartbeat
vi /etc/rc.local
/etc/init.d/heartbeat start
Active/Active模式
在lb1和lb2的ha.cf裏面增長一行
auto_failback on
在lb1上
vi /etc/ha.d/haresources
lb1.ylmf.com 192.168.1.99
在lb2上
vi /etc/ha.d/haresources
lb2.ylmf.com 192.168.1.99
參考文檔http://www.hiadmin.com/ha%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95/
五測試
1.啓動heartbeat
/etc/init.d/heartbeat start
2.啓動haproxy
/etc/init.d/haproxy start
3.在lb1和lb2上查看接口信息
Ifconfig 或 ip addr sh eth0
4.查看haproxy狀態
http://192.168.1.99/haproxy?stats
六 haproxy參考文檔
官方網址:http://cn.haproxy.org/
HAProxy提供高可用 性、負載均衡 以及基於TCP和HTTP應用的代理,它是免費、快速而且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點一般又須要會 話保持或七層處理。 HAProxy運行在當前的硬件上,徹底能夠支持數以萬計的併發鏈接。而且它的運行模式使得它能夠很簡單安全的整合進您當前的架構中,同時能夠保護你的 web服務器不被暴露到網絡上, 以下所示:
當前,版本1.3, 它支持以下新特性:
和其餘」免費」的負載均衡解決方案不一樣的是, HAproxy僅被幾百或者幾千的遍及全球的用戶使用, 可是這些用戶一般運行很大的站點, 天天有數百萬的點擊量和幾Gbps甚至幾萬兆(terabytes)吞吐量。 他們須要7×24的可用性而且願意承受免費軟件解決方案的風險, 同時有這些使用技術. 一般, 這種解決方案僅配置爲內部使用, 我僅在他們提供給我反饋或者須要新特性的時候纔會知道.
Heartbeat介紹
Linux-HA項目在普遍的平臺上提供成熟的高可用(故障切換)能力,在全球支持上萬個關鍵任務節點。 其中一小部分記錄在成功案例。
Linux-HA項目是目前可用的時間最長,最多能力,和通過最好測試的開放源碼高可用性解決方案,並具備最大的輔助社區.根據項目的規則,它一直 保證編譯時沒有警告,沒有靜態分析工具發現的問題.安全專家按期審查源程序.
它提供對節點,應用的監視功能,提供一個成熟的基於規則的資源配置依賴模型.當一個故障發生,或一個規則變化,用戶提供的規則將指導指望的資源的重 新配置.
通常來講,它具備至少與Veritas VCS, SunCluster, LifeKeeper, ServiceGuard等商業集羣軟件相似的功能和一樣方便的使用方法.
· 活動的隔離機制(STONITH)提供強大的數據完整性保證-即便在發生一些不 尋常的故障狀況下.
· 全特性的圖形用戶界面 – 能夠用來配置,控制,監視服務和服務器的運行
· 支持工業標準的系統管理協議 CIM (Common Information Model)
官方網址:http://www.linux-ha.org/zh/HomePage_zh