haproxy_keepalived安裝配置
參照《ubuntu server最佳方案》,學(xué)習(xí)配置了haproxy的負(fù)載均衡。留個(gè)簡(jiǎn)單的安裝配置記錄,然后繼續(xù)深入學(xué)習(xí)。
安裝環(huán)境:ubuntu server
網(wǎng)絡(luò)環(huán)境
- Load Balancer 1: lb1.test.com, IP address: 192.168.1.10 eth0
- Load Balancer 2: lb2.test.com, IP address: 192.168.1.11 eth0
- Web Server 1: web1.test.com, IP address: 192.168.1.12 eth0
- Web Server 2: web2.test.com, IP address: 192.168.1.13 eth0
- lb1 and lb2共享虛擬IP: 192.168.1.100處理請(qǐng)求
一、 web server的安裝配置 以下操作在兩臺(tái)web server上同時(shí)做 1. apache安裝
- sudo apt-get install apache2 libapache2-mod-php5 php5-mysql
2. 修改apache中日志記錄,以便可以記錄client的ip而不是lb的
- vi /etc/apache2/apache2.conf
- 把logFormat的%h改為%{X-Forwarded-For}i
3. 在你網(wǎng)站目錄創(chuàng)建檢測(cè)文件,用于haproxy檢測(cè)web server是否存活
- echo “It works!” > /var/www/hachecker.php
- 修改你的虛擬主機(jī)配置文件記錄access日志部分,不記錄hachecker.php的訪問(wèn)日志
- SetEnvIf Request_URI “^/hachecker\.php$” dontlog
- CustomLog /var/log/apache2/access.log combined env=!dontlog
4. 重啟apache
- /etc/init.d/apache2 restart
二、 Load Balancer(HAProxy)安裝配置
以下操作在兩臺(tái)Load Balancer上同時(shí)做 1. 安裝HAProxy
- sudo apt-get install haproxy
2. 配置HAProxy
- sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg_BAK
- sudo vi /etc/haproxy/haproxy.cfg
- 加入如下:
- global
- log 127.0.0.1 local0
- log 127.0.0.1 local1 notice
- #log loghost local0 info
- maxconn 4096
- #chroot /usr/share/haproxy
- user haproxy
- group haproxy
- daemon
- #debug
- #quiet
-
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- retries 3
- option redispatch
- maxconn 2000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
-
- listen webfarm 192.168.1.100:80
- stats enable
- stats auth isends:isends
- balance roundrobin
- cookie JSESSIONID prefix
- option forwardfor
- option httpchk HEAD /hachecker.php
- server web1 192.168.1.12:80 cookie A check
- server web2 192.168.1.13:80 cookie B check
-
- option httpclose # disable keep-alive
- #option checkcache # block response if set-cookie & cacheable
-
- #errorloc 502 http:
- #errorfile 503 /etc/haproxy/errors/503.http
- errorfile 400 /etc/haproxy/errors/400.http
- errorfile 403 /etc/haproxy/errors/403.http
- errorfile 408 /etc/haproxy/errors/408.http
- errorfile 500 /etc/haproxy/errors/500.http
- errorfile 502 /etc/haproxy/errors/502.http
- errorfile 503 /etc/haproxy/errors/503.http
- errorfile 504 /etc/haproxy/errors/504.http
#retries--web無(wú)法訪問(wèn)的重試次數(shù)
#cookie JSESSIONID prefix--處理session
#option forwardfor--轉(zhuǎn)發(fā)client的IP給web server(X-Forwarded-For)
3. 調(diào)整系統(tǒng)參數(shù)、開(kāi)機(jī)啟動(dòng)HAProxy
- sudo vi /etc/sysctl.conf
- 加入一行:
- net.ipv4.ip_nonlocal_bind=1
- 執(zhí)行sudo sysctl –p使之生效
- 開(kāi)機(jī)啟動(dòng)HAProxy
- vi /etc/default/haproxy
- 設(shè)置ENABLED=1
- ENABLED=1
4. 啟動(dòng)haproxy
- sudo /etc/init.d/haproxy start
三、 Keepalived安裝配置
lb1和lb2的HAProxy已經(jīng)配置好并監(jiān)聽(tīng)I(yíng)P地址:192.168.1.100。Keepalived用priority參數(shù)把bl1和bl2分配為“主服務(wù)器”和“從服務(wù)器”。正常情況下由主服務(wù)器監(jiān)聽(tīng)I(yíng)P地址并提供服務(wù)。
以下操作在兩臺(tái)Load Balancer上同時(shí)做,但是priority參數(shù)主服務(wù)器為101,從服務(wù)器為100 1. 安裝Keepalived
- sudo apt-get install keepalived
2. 配置Keepalived
- sudo vi /etc/keepalived/keepalived.conf
- 配置如下:
- vrrp_script chk_haproxy {
- script "killall -0 haproxy"
- interval 2
- weight 2
- }
-
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- virtual_ipaddress {
- 192.168.1.100
- # optional label. should be of the form "realdev:sometext" for
- # compatibility with ifconfig.
- #192.168.200.18 label eth0:1
- }
- track_script {
- chk_haproxy
- }
- }
3. 啟動(dòng)Keepalived
- sudo /etc/init.d/keepalived start
- 查看ip是否綁定正確,bl1綁定IP:192.168.1.100。bl2不綁定該IP
- ip addr sh eth0
四、 其他
HAProxy情況查詢:用上面設(shè)置的賬號(hào)密碼登陸 http://192.168.1.100/haproxy?stats
HAProxy中文網(wǎng)站 附件是haproxy的詳細(xì)配置文檔