haproxy是個高性能的tcp和http的反向代理。它就是個代理。不像nginx還做web服務器
nginx的優點和缺點
優點: 1、web服務器,應用比較廣泛,大家都會 2、可以作為7層負載均衡,location設置復雜的基於HTTP的負載均衡 3、性能強大,網絡依賴小 4、安裝配置簡單 缺點: 1、健康檢查單一,不支持基於url的健康檢查(可以使用第三方插件實現) 2、負載均衡算法少 3、不能動態管理,比如踢出某個web節點,需要reload配置 4、沒有集群upstream的狀態頁面
haproxy的優點和缺點
優點: 1、專門做反向代理負載均衡 2、負載均衡算法比較多,大於等於8種,比nginx豐富 3、性能不低於nginx,大於等於nginx 4、支持動態管理,通過和haproxy的sock進行通信,可以進行管理 5、有比較豐富的Dashboard的頁面,監控方便。有管理頁面 6、比較強大的7層反向代理功能,在7層方便,功能強大 7、會話保持比nginx豐富。可以基於cookie和源IP(nginx也能做到基於IP和cookie) 缺點: 配置沒有Nginx簡單(相對熟悉)
先殺掉原先的nginx進程,防止80端口被占用,導致haproxy無法啟動
[root@linux-node1 conf]# pkill nginx [root@linux-node1 conf]# ps aux | grep nginx root 27201 0.0 0.0 112664 972 pts/0 S+ 05:39 0:00 grep --colour=auto nginx [root@linux-node1 conf]#
部署haproxy,這里是編譯安裝,版本是1.6.3,執行命令如下
cd /usr/local/src/ wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.3.tar.gz tar xfz haproxy-1.6.3.tar.gz cd haproxy-1.6.3 make TARGET=linux2628 PREFIX=/usr/local/haproxy-1.6.3 make install cp /usr/local/sbin/haproxy /usr/sbin/ haproxy -v
[root@linux-node1 haproxy-1.6.3]# haproxy -v HA-Proxy version 1.6.3 2015/12/25 Copyright 2000-2015 Willy Tarreau <willy@haproxy.org> [root@linux-node1 haproxy-1.6.3]#
[root@linux-node1 haproxy-1.6.3]# pwd /usr/local/src/haproxy-1.6.3 [root@linux-node1 haproxy-1.6.3]# cd examples/ [root@linux-node1 examples]# ls haproxy.init haproxy.init [root@linux-node1 examples]# cp haproxy.init /etc/init.d/haproxy [root@linux-node1 examples]# chmod +x /etc/init.d/haproxy [root@linux-node1 examples]#
創建haproxy用戶和相關目錄
useradd -r表示創建系統賬號
[root@linux-node1 examples]# useradd -r haproxy [root@linux-node1 examples]# [root@linux-node1 examples]# mkdir /etc/haproxy -p [root@linux-node1 examples]# mkdir /var/lib/haproxy -p [root@linux-node1 examples]#

重啟rsyslog
[root@linux-node1 ~]# vim /etc/rsyslog.conf [root@linux-node1 ~]# systemctl restart rsyslog [root@linux-node1 ~]# netstat -lnup | grep 514 udp 0 0 0.0.0.0:514 0.0.0.0:* 27509/rsyslogd udp6 0 0 :::514 :::* 27509/rsyslogd [root@linux-node1 ~]#
關於mode http 你如果不寫,默認繼承defaults里面的
defaults默認不寫好像也是http。
tcp的需要注明。
mode tcp
[root@linux-node1 ~]# cd /etc/haproxy/ [root@linux-node1 haproxy]# vim haproxy.cfg [root@linux-node1 haproxy]# cat haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info defaults log global #使用全局的日志配置 mode http option httplog option dontlognull #日志中不記錄空連接,比如不記錄健康檢查的連接 timeout client 50000 timeout server 50000 timeout connect 5000 frontend http_front bind *:80 stats uri /haproxy?stats default_backend http_back backend http_back balance roundrobin server linux-node1 10.0.1.105:8080 check server linux-node2 10.0.1.106:8080 check [root@linux-node1 haproxy]#
啟動haproxy
[root@linux-node1 ~]# /etc/init.d/haproxy start Reloading systemd: [ 確定 ] Starting haproxy (via systemctl): [ 確定 ] [root@linux-node1 ~]# [root@linux-node1 ~]# netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27556/haproxy tcp6 0 0 :::8080 :::* LISTEN 20130/httpd [root@linux-node1 ~]#
[root@linux-node1 ~]# grep local3 /etc/rsyslog.conf local3.* /var/log/haproxy.log [root@linux-node1 ~]# [root@linux-node1 ~]# systemctl restart rsyslog [root@linux-node1 ~]#
再次重啟haproxy服務,就可以看到haproxy的日志文件生成了。可以看到啟動過程
[root@linux-node1 ~]# /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 確定 ] [root@linux-node1 ~]# tail -f /var/log/haproxy.log Feb 27 06:33:43 localhost haproxy[27648]: Stopping frontend http_front in 0 ms. Feb 27 06:33:43 localhost haproxy[27648]: Stopping backend http_back in 0 ms. Feb 27 06:33:43 localhost haproxy[27648]: Proxy http_front stopped (FE: 0 conns, BE: 0 conns). Feb 27 06:33:43 localhost haproxy[27648]: Proxy http_back stopped (FE: 0 conns, BE: 0 conns). Feb 27 06:33:43 localhost haproxy[27687]: Proxy http_front started. Feb 27 06:33:43 localhost haproxy[27687]: Proxy http_back started.
繼續優化更改下配置
haproxy可以自定義健康檢查的url,這是nginx不具備的
check:啟用健康檢測
inter:健康檢測間隔
rise:檢測服務可用的連續次數
fall:檢測服務不可用的連續次數
[root@linux-node1 ~]# cd /etc/haproxy/ [root@linux-node1 haproxy]# vim haproxy.cfg [root@linux-node1 haproxy]# cat haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend http_front mode http bind *:80 stats uri /haproxy?stats default_backend http_back backend http_back option httpchk GET /index.html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 haproxy]#
重啟服務
[root@linux-node1 haproxy]# /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 確定 ] [root@linux-node1 haproxy]# netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27849/haproxy tcp6 0 0 :::8080 :::* LISTEN 20130/httpd [root@linux-node1 haproxy]#

頁面測試,目前也是輪詢的
多訪問幾次,健康頁面有新的數據變化
sessions這里可以看到有沒有失敗的訪問

結合haproxy的acl配置反向代理功能,先備份原先配置文件
設置acl
這樣能支持多個域名,讓不同的域名,訪問不同的backend上面去
[root@linux-node1 conf]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.ori [root@linux-node1 conf]# vim /etc/haproxy/haproxy.cfg
修改配置文件為如下
注意,配置文件中,前端和后端不要用特殊符號以及點。它對這些敏感。推薦使用下划線
[root@linux-node1 conf]# vim /etc/haproxy/haproxy.cfg [root@linux-node1 conf]# cat /etc/haproxy/haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin stats timeout 2m defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend www_nmap_com mode http bind *:80 stats uri /haproxy?stats default_backend www_nmap_com_backend acl is_other_nmap_com hdr_end(host) other.nmap-blog.com use_backend other_nmap_com_backend if is_other_nmap_com backend www_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index.html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 backend other_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index.html balance roundrobin server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 conf]#
重啟haproxy
[root@linux-node1 conf]# /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 確定 ]
windows客戶端配置host文件
10.0.1.105 www.nmap-blog.com other.nmap-blog.com
這樣也實現了haproxy的多域名反向代理
haproxy的acl,也可以根據正則,和后綴設置,下面2種方法。推薦第一種,正則方式匹配
acl is_static_reg url_reg /*.(css|jpg|png|js|jpeg|gif)$ acl is_static_path path_end .gif .png .js
修改配置文件做基於正則的acl
[root@linux-node1 conf]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.2 [root@linux-node1 conf]# vim /etc/haproxy/haproxy.cfg [root@linux-node1 conf]# cat /etc/haproxy/haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin stats timeout 2m defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend www_nmap_com mode http bind *:80 stats uri /haproxy?stats default_backend www_nmap_com_backend acl is_static_reg url_reg /*.(css|jpg|png|js|jpeg|gif)$ use_backend other_nmap_com_backend if is_static_reg #acl is_static_path path_end .gif .png .js #acl is_other_nmap_com hdr_end(host) other.nmap-blog.com #use_backend other_nmap_com_backend if is_other_nmap_com backend www_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index.html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 backend other_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index.html balance roundrobin server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 conf]#
重啟服務
[root@linux-node1 conf]# /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 確定 ] [root@linux-node1 conf]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME haproxy 48521 haproxy 5u IPv4 1371377 0t0 TCP *:http (LISTEN) [root@linux-node1 conf]#
因為匹配后會連接到node2,這里就在node2上設置一個js文件,node1不做任何設置。
[root@linux-node2 ~]# echo 'test111' >/var/www/html/test.js [root@linux-node2 ~]#
測試成功
關於后端web節點記錄檢查日志的問題,因為我設置檢查check inter 2000 ,也就是2秒發一次檢查包。后端節點日志這里也能看到
[root@linux-node2 ~]# tail -f /var/log/httpd/access_log 10.0.1.105 - - [04/Mar/2017:00:35:46 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:48 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:50 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:52 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:54 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:56 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:35:58 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:36:00 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:36:02 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:36:04 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:36:06 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04/Mar/2017:00:36:08 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-"
關於怎么讓后端apache不記錄健康檢查日志,以及如何記錄真正的客戶端IP,這里不做實驗。