使用Haproxy,Keepalived,Tproxy實現高可用透明反向代理
阿新 • • 發佈:2019-01-24
宣告
本文為Gleasy原創文章,轉載請指明引自Gleasy團隊部落格
一。需求場景
具體需求如下:
4臺Server,2臺為Proxy Server,2臺為Web Server,均為雙網絡卡;
1個公網IP(183.129.228.91);
要求如下:
1. 2臺Proxy Server反向代理2臺WEB Server,作負載均衡
2. 2臺Proxy Server為主備模式,公網IP在兩臺Server之間自動切換
3. Proxy Server作透明代理(web server日誌中要記錄真實的訪問IP)
二。配置步驟
1. 安裝軟體(proxy server)
Gleasy有自己的Yum庫,全部軟體已經打成RPM包供安裝;
其中haproxy打包時使用了下面的引數:
1 |
make TARGET=linux26 CPU=x86_64 USE_STATIC_PCRE=1 USE_LINUX_TPROXY=1 |
2 |
make install target=linux26 |
安裝:
1 |
yum install keepalived |
2 |
yum install haproxy |
2. 配置keepalived(proxy server)
配置主:
01 |
vrrp_instance VI_3{ |
02 |
interface eth1 #這裡的eth1是內網(192.168.1.X網段)的網絡卡!!! |
03 |
state MASTER
|
04 |
priority 100 #從為99 |
05 |
virtual_router_id 101#路由ID,可通過#tcpdump vrrp檢視。 |
06 |
garp_master_delay 1 #主從切換時間,單位為秒。 |
07 |
08 |
advert_int 1 #檢查間隔,預設1秒 |
09 |
authentication { |
10 |
auth_type PASS |
11 |
auth_pass KJj23576hYgu23IP |
12 |
} |
13 |
virtual_ipaddress { |
14 |
192.168.1.1/32 dev eth1 |
15 |
183.129.228.91/27 brd 183.129.228.95 dev eth0 |
16 |
} |
17 |
virtual_routes { |
18 |
via 183.129.228.65 dev eth0 |
19 |
} |
20 |
} |
配置備:
01 |
vrrp_instance VI_3{ |
02 |
interface eth1 #這裡的eth1是內網(192.168.1.X網段)的網絡卡!!! |
03 |
state BACKUP # |
04 |
priority 99 # 主為100 |
05 |
virtual_router_id 101#路由ID,可通過#tcpdump vrrp檢視。 |
06 |
garp_master_delay 1 #主從切換時間,單位為秒。 |
07 |
08 |
advert_int 1 #檢查間隔,預設1秒 |
09 |
authentication { |
10 |
auth_type PASS |
11 |
auth_pass KJj23576hYgu23IP |
12 |
} |
13 |
virtual_ipaddress { |
14 |
192.168.1.1/32 dev eth1 |
15 |
183.129.228.91/27 brd 183.129.228.95 dev eth0 |
16 |
} |
17 |
virtual_routes { |
18 |
via 183.129.228.65 dev eth0 |
19 |
}
|
20 |
} |
3. 配置tproxy(proxy server)
01 |
#!/bin/sh |
02 |
/sbin/iptables -I FORWARD -i eth+ -j ACCEPT
|
03 |
/sbin/iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE |
04 |
/sbin/iptables -t mangle -N DIVERT |
05 |
/sbin/iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT |
06 |
/sbin/iptables -t mangle -A DIVERT -j MARK --set-mark 1 |
07 |
/sbin/iptables -t mangle -A DIVERT -j ACCEPT |
08 |
09 |
/sbin/ip rule add fwmark 1 lookup 100 |
10 |
/sbin/ip route add local 0.0.0.0/0 dev lo table 100 |
11 |
12 |
echo 1 > /proc/sys/net/ipv4/conf/all/forwarding
|
13 |
echo 1 > /proc/sys/net/ipv4/conf/all/send_redirects
|
14 |
echo 1 > /proc/sys/net/ipv4/conf/eth0/send_redirects |
4. 配置haproxy(proxy server)
01 |
global |
02 |
log 127.0.0.1 local3 err |
03 |
chroot /usr/local/haproxy |
04 |
maxconn 13000 |
05 |
daemon |
06 |
nbproc 4 #設定併發程序 |
07 |
pidfile /usr/local/haproxy/logs/haproxy.pid |
08 |
09 |
defaults |
10 |
log global |
11 |
option dontlognull # 不記錄空連線 |
12 |
option redispatch |
13 |
timeout connect 50000 |
14 |
timeout client 120000 |
15 |
timeout server 120000 |
16 |
maxconn 10000 |
17 |
balance roundrobin # 設定伺服器分配演算法 |
18 |
retries 5 # 重連次數
|
19 |
errorfile 400 /usr/local/haproxy/html/400.http |
20 |
errorfile 403 /usr/local/haproxy/html/403.http |
21 |
errorfile 408 /usr/local/haproxy/html/408.http |
22 |
errorfile 500 /usr/local/haproxy/html/500.http |
23 |
errorfile 502 /usr/local/haproxy/html/502.http |
24 |
errorfile 503 /usr/local/haproxy/html/503.http |
25 |
errorfile 504 /usr/local/haproxy/html/504.http |
26 |
27 |
28 |
frontend proxy-80 |
29 |
bind *:80 |
30 |
mode http |
31 |
option httplog |
32 |
option dontlognull |
33 |
option forwardfor # This sets X-Forwarded-For |
34 |
option http_proxy |
35 |
maxconn 13000 |
36 |
timeout client 50s |
37 |
timeout http-keep-alive 1s |
38 |
timeout http-request 10s |
39 |
default_backend proxy-80 |
40 |
41 |
42 |
#backend配置 |
43 |
backend proxy-80 |
44 |
mode http |
45 |
timeout server 120s |
46 |
timeout connect 50s |
47 |
option nolinger |
48 |
option http_proxy |
49 |
option forwardfor # This sets X-Forwarded-For |
50 |
option httplog |
51 |
option http-server-close |
52 |
cookie JSESSIONID prefix |
53 |
stats enable |
54 |
balance roundrobin |
55 |
source 0.0.0.0 usesrc clientip #透明代理!!!!! |
56 |
option httpchk GET /check.html |
57 |
server S 192.168.1.27:80 weight 3 cookie JSESS1 check inter 1500 rise 3 fall 3
|
58 |
server S 192.168.1.28:80 weight 3 cookie JSESS2 check inter 1500 rise 3 fall 3 |
5. 配置Web server閘道器(web server)
1 |
ip route default via 192.168.1.1 dev eth0 |
特別說明一下,這裡的192.168.1.1是一個VIP(虛擬IP,它的配置在上面keepalived配置中可以找到)。它會在兩臺proxy server間自動切換。
6. 注意事項:
HAPROXY所在的伺服器閘道器必須配置為外網(ip route default必須為外網閘道器);
web伺服器閘道器必須配置為HAPROXY所在伺服器的IP地址;