系統:CentOS 6.5 HA-Proxy version 1.5.2 keepalived-1.2.13
php
http://haproxy.com/ http://www.haproxy.org/社區,也就是http://haproxy.1wt.eu http://blog.haproxy.com/ http://www.keepalived.org/
css
http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#2.1 haproxy文檔
html
eth0 192.168.10.1 manager 外網eth1 192.168.22.26/24
eth0 192.168.10.2 backup 外網eth1 192.168.22.118/24前端
192.168.10.3 web1 靜態
192.168.10.4 web2 動態mysql
安裝軟件包
manager和backup 安裝haproxy、keepalived
git
yum install haproxy #HA-Proxy version 1.5.2github
yum install keepalived #keepalived-1.2.13web
web1安裝httpd服務,並啓動httpd服務
redis
yum install httpdsql
[root@web1 ~]# cat /var/www/html/index.html
web1 static
route add default gw 192.168.10.1 #添加默認路由
web2安裝httpd和php服務,並啓動httpd服務
# yum install mysql mysql-server mysql-devel httpd php php-mysql [root@web2 ~]# cat /var/www/html/index.php <?php echo phpinfo(); ?> route add default gw 192.168.10.1 #添加默認路由
配置haproxy
[root@manager haproxy]# grep -v ^\# /etc/haproxy/haproxy.cfg |sed '/^$/d' global #全局設置 log 127.0.0.1 local2 debug #err warning info debug 4種模式 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 #haproxy的最大鏈接數 user haproxy #進程所屬用戶 group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #統計數據 defaults #默認設置 mode http #指定frontend和backend工做模式{tcp|http|health},代理後端web站點用http模式,這裏是7層 log global option httplog option dontlognull #不記錄空信息,不記錄只用於檢測狀態的心跳包 option http-server-close #啓用後端服務器鏈接關閉功能 option forwardfor except 127.0.0.0/8 # forwardfor將用戶請求轉發後端服時,在HTTP請求報文中添加"X-Forwarded-For"特殊首部,以便後端服記錄真實發起請求的客戶端IP地址,而不是代理服務器內網卡地址 option redispatch #當原分配用戶請求的後端服故障時,容許把用戶請求從新分發給其餘後端服務器 option abortonclose #當Haproxy服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接 retries 3 #後端從新鏈接次數 timeout http-request 10s #http請求超時 timeout queue 1m #後端服務器隊列已滿,等待發送請求的超時時間 timeout connect 10s #和後端服務器創建鏈接 timeout client 1m #客戶端處於非活動鏈接的超時 timeout server 1m #後端服務器非活動鏈接的超時時間 timeout http-keep-alive 10s #長鏈接 timeout check 10s #健康狀態監測 maxconn 3000 listen stats mode http bind :8080 #stats的端口 stats enable stats hide-version stats uri /haproxy?stats #stats的管理頁面 stats realm no\ entrance #提示信息 stats auth admin:admin #登陸用戶名和密碼 stats admin if TRUE frontend web #定義前端,這個名字隨便起 bind :80 mode http log global option httpclose #每次請求完畢後主動關閉http通道 option logasap option dontlognull capture request header Host len 20 #haproxy日誌設置 只記錄host頭20字節 capture request header X-Forwarded-For len 15 #請求客戶端的ip地址 capture request header Referer len 60 #點擊連接所在的頁面引用位置 acl url_static path_beg -i /static /p_w_picpath /js #-i爲不區分大小寫,定義一個acl的規則,規則開始 acl url_static path_end -i .html .jpg .gif .png .css .js #acl規則結束 #若是設置下面兩條path_end也是能夠的,不過打開的時候後綴名html或php必須添加,例如http://192.168.1.1/index.php #acl url_static path_end .html .jpg .gif .png .css .js #acl url_dynamic path_end .php #use_backend static_servers if url_static #use_backend dynamic_servers if url_dynamic use_backend static_servers if url_static #知足url_static規則,則匹配下面規則中的static_servers default_backend dynamic_servers #默認按照dynamic_servers規則,就是轉發到動態服務器 backend static_servers #定義後端,名字確定和上面定義規則的同樣 mode http # balance roundrobin #雖然定義輪詢,不過這裏就一個,因此註釋 option redispatch option abortonclose server web1 192.168.10.3:80 check maxconn 5000 #後端是靜態服務器web1 backend dynamic_servers #定義後端,動態服務器規則 mode http balance source #負載均衡方式,原地址hash hash-type consistent #hash類型,consistent-hash一致性hash動態 option redispatch option abortonclose server web2 192.168.10.4:80 check maxconn 500 #後端動態服務器web2
負載均衡方式通常用cookie識別或session識別
這裏把haproxy的日誌改一個路徑,爲/var/log/haproxy.log
# yum install rsyslog [root@manager haproxy]# grep -v ^\# /etc/rsyslog.conf|sed '/^$/d' $ModLoad imuxsock # provides support for local system logging (e.g. via logger command) $ModLoad imklog # provides kernel logging support (previously done by rklogd) $ModLoad imudp #開啓 $UDPServerRun 514 #開啓 $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat $IncludeConfig /etc/rsyslog.d/*.conf *.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages #messages裏面不記錄local2的日誌 local2.* /var/log/haproxy.log #爲local2單獨設置一個日誌文件 authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg * uucp,news.crit /var/log/spooler local7.* /var/log/boot.log [root@manager haproxy]# service rsyslog restart
web1和web2日誌添加X-Forwarded-For首部,讓後端服務器記錄真實請求ip,web1和web2都作如下改動
# vim /etc/httpd/conf/httpd.conf 497 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 改成: LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#採用標準的X-Fowarded-For字段獲取, 在web服務器上修改日誌的格式便可 # Nginx示例 log_format upstream '$time_iso8601 $http_x_forwarded_for $host $upstream_response_time $request $status $upstream_addr'; # Apache示例 SetEnvIf REMOTE_ADDR "(.+)" CLIENTIP=$1 SetEnvIf X-Forwarded-For "^([0-9.]+)" CLIENTIP=$1 LogFormat "%{CLIENTIP}e %D %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" trueip_combined
[root@manager haproxy]# scp haproxy.cfg root@backup:/etc/haproxy 把配置複製一份給backup
檢查並啓動服務查看
# haproxy -f /etc/haproxy/haproxy.cfg
# service haproxy restart
打開manager和backup的頁面都會出現phpinfo的頁面,是由於不知足url_static,走dynamic_servers規則
查看haproxy和web1,web2的日誌來檢查
http://192.168.22.26:8080/haproxy?stats 這個頁面查看狀態
配置keepalived
manager主節點keepalived配置
[root@manager keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { your_email@163.com #收件 } notification_email_from root@localhost #發件 smtp_server 127.0.0.1 #smtp服務器地址 smtp_connect_timeout 3 ##smtp服務器超時時間 router_id LVS_HAPROXY #VRRP組名 } vrrp_script chk_haproxy { #定義chk_haproxy script "killall -0 haproxy" #執行的腳本 interval 2 #執行的間隔時間 weight 2 #若是由主變爲備,那麼優先級-2 } vrrp_instance VI_1 { state MASTER #VI_1爲主 interface eth1 #VIP的網卡,外網網卡,這裏eth1 virtual_router_id 77 #virtual_router_id號 priority 100 #優先級 garp_master_delay 1 #主從切換時間 advert_int 1 authentication { #認證機制 auth_type PASS auth_pass 11111 } virtual_ipaddress { #設置VIP 192.168.22.249/24 dev eth1 } track_script { #執行腳本檢測 chk_haproxy } track_interface { #跟蹤接口,設置額外的監控 eth1 } }
keepalived備節點配置
[root@backup keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { your_email@163.com } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 3 router_id LVS_HAPROXY } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 77 priority 99 garp_master_delay 1 advert_int 1 authentication { auth_type PASS auth_pass 11111 } virtual_ipaddress { 192.168.22.249/24 dev eth1 } track_script { chk_haproxy } track_interface { eth1 } }
此時VIP192.168.22.249在manager上面,當把主節點haproxy服務中止,那麼VIP將漂移到備節點
~~~分割線~~~
下面是keepalived互爲主從的配置,manager節點跑一個web應用,backup也跑一個web應用,不浪費服務器資源。當一個節點掛死,兩個VIP會在另外一個節點上面,這時候網卡上配置兩個VIP資源
manager的keepalived配置
[root@manager ~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 2 weight 2 } vrrp_instance VI_1 { interface eth1 state MASTER priority 100 virtual_router_id 100 garp_master_delay 1 authentication { auth_type PASS auth_pass 11111 } track_interface { eth1 } virtual_ipaddress { 192.168.22.249/24 dev eth1 } track_script { chk_haproxy } } vrrp_instance VI_2 { interface eth1 state BACKUP priority 99 virtual_router_id 101 garp_master_delay 1 authentication { auth_type PASS auth_pass 22222 } track_interface { eth1 } virtual_ipaddress { 192.168.22.147/24 dev eth1 } }
backup的keepalived配置
[root@backup ~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 2 weight 2 } vrrp_instance VI_1 { interface eth1 state BACKUP priority 99 virtual_router_id 100 garp_master_delay 1 authentication { auth_type PASS auth_pass 11111 } track_interface { eth1 } virtual_ipaddress { 192.168.22.249/24 dev eth1 } track_script { chk_haproxy } } vrrp_instance VI_2 { interface eth1 state MASTER priority 100 virtual_router_id 101 garp_master_delay 1 authentication { auth_type PASS auth_pass 22222 } track_interface { eth1 } virtual_ipaddress { 192.168.22.147/24 dev eth1 } }
主備都啓動haproxy和keepalived服務,查看haproxy和keepalived的日誌
/var/log/haproxy.log
/var/log/messages
在manager上把haproxy服務中止,查看節點ip地址,和keepalived日誌,此時VIP192.168.22.249就會轉移到backup上面,日誌就不貼了
manager網卡: