down:http://www.haproxy.org/
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
注意:haproxy代理ssl有兩種方式
1.haproxy本身提供ssl證書,后面的web服務器走正常的http
2 haproxy本身只提供代理,后面的web服務器https
第一種方法 需要在編譯haproxy支持ssl
[root@ha02 haproxy-1.4.26]# make TARGET=linux26 USE_OPENSSL=1 ADDLIB=-lz
不過有報錯:
btree/ebsttree.o ebtree/ebimtree.o ebtree/ebistree.o -lcrypt -lz /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status make: *** [haproxy] Error 1 解決方法: # yum install zlib-devel
如果裝完如果沒報錯,查看haproxy並沒有支持ssl
[root@ha02 sbin]# ldd haproxy |grep ssl [root@ha02 sbin]# 查看發現並沒有安裝openssl-devel [root@ha02 sbin]# rpm -qa|grep openssl openssl-1.0.1e-42.el6.x86_64 [root@ha02 sbin]# yum install openssl-devel 安裝上了,好像還是不行: [root@ha02 sbin]# rpm -qa|grep openssl openssl-1.0.1e-48.el6_8.3.x86_64 openssl-devel-1.0.1e-48.el6_8.3.x86_64
還是不行!!!
換了個版本為haproxy-1.4.26成功了
[root@ha02 sbin]# ldd haproxy|grep ssl libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007efd2d921000)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[root@ha01 haproxy-1.4.26]# uname -a Linux ha01 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux [root@ha01 haproxy-1.4.26]# make TARGET=linux26 ARCH=x86_64 PREFIX=/apps/haproxy-1.4.26 gcc -Iinclude -Iebtree -Wall -m64 -march=x86-64 -O2 -g -fno-strict-aliasing -DTPROXY -DCONFIG_HAP_CRYPT -DENABLE_POLL -DENABLE_EPOLL -DENABLE_SEPOLL -DNETFILTER -DUSE_GETSOCKNAME -DCONFIG_HAPROXY_VERSION=\"1.4.26\" -DCONFIG_HAPROXY_DATE=\"2015/01/30\" \ -DBUILD_TARGET='"linux26"' \ -DBUILD_ARCH='"x86_64"' \ -DBUILD_CPU='"generic"' \ -DBUILD_CC='"gcc"' \ -DBUILD_CFLAGS='"-m64 -march=x86-64 -O2 -g -fno-strict-aliasing"' \ -DBUILD_OPTIONS='""' \ -c -o src/haproxy.o src/haproxy.c make: gcc: Command not found make: *** [src/haproxy.o] Error 127
[root@ha01 haproxy-1.4.26]# yum install gcc -y [root@ha01 haproxy-1.4.26]# make TARGET=linux26 ARCH=x86_64 prefix=/apps/haproxy-1.4.26 #TARGET=linux26,ARCH=x86_64#26以上的都用linux26 #TARGET是指內核版本,ARCH指定CPU架構
[root@ha01 haproxy-1.4.26]# make install PREFIX=/apps/haproxy-1.4.26
[root@ha02 haproxy-1.4.26]# tree /apps/ /apps/ └── haproxy-1.4.26 ├── doc │ └── haproxy │ ├── architecture.txt │ ├── configuration.txt │ ├── haproxy-en.txt │ └── haproxy-fr.txt ├── sbin │ └── haproxy └── share └── man └── man1 └── haproxy.1
[root@ha02 haproxy-1.4.26]# cp examples/haproxy.cfg /apps/haproxy-1.4.26/ [root@ha02 haproxy-1.4.26]# pwd /usr/local/src/haproxy-1.4.26
[root@ha02 haproxy-1.4.26]# cat haproxy.cfg
# this config needs haproxy-1.4.26
global
log 127.0.0.1 local0 warning #日志輸出配置,所有日志都記錄在本機,通過local0輸出
maxconn 4096 #最大連接數
#chroot /apps/apps/haproxy-1.4.26 #改變當前工作目錄
uid 99 #所屬用戶的uid
gid 99 #所屬運行的gid
daemon #以后台形式運行haproxy
nbproc 1
#haproxy啟動時的進程數,<=CPU的核數,常見的2棵8核心CPU的服務器>,共16核心,則可以將其值設置為<=16,創建多個進程數,可以減少每個進程的任
務隊列,但是過多的進程數也可能會導致進程的崩潰。
pidfile /var/run/haproxy.pid
defaults
log global
mode http #默認的模式mode {tcp|http|health},tcp是4層,http是7層,health只會返回ok
option httplog
option forwardfor except 127.0.0.0/8
option dontlognull
option redispatch
#當serverId對應的服務器掛掉后,強制定向到其他健康的服務器
option abortonclose
#當服務器負載很高的時候,自動結束掉當隊列處理比較久的鏈接
retries 3 #3次連接失敗就認為是服務器不可用
maxconn 2000 #默認最大連接數
contimeout 5000 #連接超時
clitimeout 50000 #客戶端超時
srvtimeout 50000 #服務器超時
timeout check 5s #心跳檢測超時
stats refresh 30s #統計頁面自動刷新時間
stats uri /stats #統計頁面url
stats realm baison-test-Haproxy #統計頁面密碼框上提示文本
stats auth admin:admin123 #統計頁面用戶名和密碼設置
stats hide-version #隱藏統計頁面上HAProxy的版本信息
frontend www
bind *:80
#這里建議使用bind *:80的方式,要不然做集群高可用的時候有問題,vip切換到其他機器就不能訪問了
acl web hdr(host) -i www.zhirs.com
#acl后面是規則名稱,-i忽略大小寫后面跟要訪問的域名,如果訪問www.zhirs.com這個域名就會分發到下面的webserver的做用域
acl img hdr(host) -i img.zhirs.com
#如果訪問img.baison.com.cn就分發到imgserver這個作用域
use_backend webserver if web
use_backend imgserver if img
backend webserver #webserver作用域
#mode http
balance roundrobin
#balance roudrobin 輪詢,balance source 保存session值,支持static-rr,leastconn,first,uri等參數
option httpchk HEAD /index.html
#檢測文件,如果分發到后台index.html訪問不到就不再分發給它"特別注意HEAD如果沒有它是不成功的"用GET也可以,但我不知道name和get的區別!
server web01 192.168.1.104:80 check inter 2000 fall 3 weight 30
server web01 192.168.1.105:80 check inter 2000 fall 3 weight 20
backend imgserver
#node http
option httpchk /index.php
balance roundrobin
server img01 192.168.1.106:80 check inter 2000 fall 3
server img02 192.168.1.107:80 check inter 2000 fall 3
[root@ha02 haproxy-1.4.26]# ./sbin/haproxy -f ./haproxy.cfg Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2444/./sbin/haproxy tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1631/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1473/master tcp 0 0 :::22 :::* LISTEN 1631/sshd tcp 0 0 ::1:25 :::* LISTEN 1473/master udp 0 0 0.0.0.0:68 0.0.0.0:* 1227/dhclient udp 0 0 0.0.0.0:45396 0.0.0.0:* 2444/./sbin/haproxy [root@ha02 haproxy-1.4.26]# ps auxwwf|grep -v 'grep'|grep haproxy nobody 2444 0.1 0.3 12080 1624 ? Ss 06:29 0:01 ./sbin/haproxy -f ./haproxy.cfg
[root@ha02 haproxy-1.4.26]# ./sbin/haproxy -f /apps/haproxy-1.4.26/haproxy.cfg -c Configuration file is valid #-c 是檢查語法是否有問題
注意:chroot /apps/apps/haproxy-1.4.26 #改變當前工作目錄 會無法啟動,原因查找。
暫時實現結果:

[root@ha02 haproxy-1.4.26]# ./sbin/haproxy -v HA-Proxy version 1.4.26 2015/01/30 Copyright 2000-2015 Willy Tarreau <w@1wt.eu>
http://www.cnblogs.com/MacoLee/p/5853413.html
通過web看狀態信息:
stats enable
stats uri /admin?stats
stats auth proxy:xxxxx
-----------------------------------------
option forwarfor:讓realserver記真實的用戶的ip

inter 5000 是指間隔5秒做一個檢測!!!!!
fall 5 5次失敗會被提掉!!!!!
rise 2 檢測2次好了,再把恢復的服務器加進來!!!
check port 22 對22號端口做健康檢查,如果不通就踢出去相當於沒配這個一行參數
=========================================
平滑重啟:
haproxy -f $CONFFILE -sf $(cat $PIDFILE)
上面的平滑重啟好像有問題:
改成下面的命令:
[root@ha02 ~]# /apps/haproxy-1.4.26/sbin/haproxy -f /apps/haproxy-1.4.26/haproxy.cfg -st $(cat /var/run/haproxy.pid) [root@ha02 ~]# ps aux|grep haproxy nobody 4228 0.0 0.2 11960 1372 ? Ss 22:24 0:00 /apps/haproxy-1.4.26/sbin/haproxy -f /apps/haproxy-1.4.26/haproxy.cfg -st 4220 root 4230 0.0 0.1 103312 856 pts/0 S+ 22:24 0:00 grep haproxy
--------------------------------------------------------
frontend:用來匹配接收客戶所請求的域名,uri等,並針對不同的區配,做不同的請求處理。
===============================================================
stats realm baison-test-Haproxy #統計頁面密碼框上提示文本

HAProxy的算法現在也越來越多了,具體有如下8種: ①roundrobin,表示簡單的輪詢,這個不多說,這個是負載均衡基本都具備的; ②static-rr,表示根據權重,建議關注; ③leastconn,表示最少連接者先處理,建議關注; ④ource,表示根據請求源IP,這個跟Nginx的IP_hash機制類似,我們用其作為解決session問題的一種方法,建議關注; ⑤ri,表示根據請求的URI; ⑥rl_param,表示根據請求的URl參數'balance url_param' requires an URL parameter name; ⑦hdr(name),表示根據HTTP請求頭來鎖定每一次HTTP請求; ⑧rdp-cookie(name),表示根據cookie(name)來鎖定並哈希每一次TCP請求。
stats uri /stats #統計頁面url

可以改一下:
stats uri /vip

==================================================


HEAD配置方法: option httpchk HEAD /checkstatus.html HTTP/1.0 這種檢測方式就相當於curl http://192.168.1.82/checkstatus.html
在配置內核參數開發轉發功能:
在/etc/sysctl.conf中加入如下配置
net.ipv4.ip_forward=1
sysctl -p
-----------------注意haproxy監聽端口不能重復,backend端口可以相同(前題在不能的服務器上面)---------------------------------
如果重復會報錯
frontend和backend的端口可以為不同的端口(規范考慮最好一致)
frontend server01_test.xxxxx.com:8010 bind *:8010 mode http default_backend server-name01 frontend server02_test.guoeryyyyyyyyy.com:8011 bind *:8011 mode http default_backend server-name02 backend server-name02 mode http balance roundrobin cookie SERVERID option httpchk HEAD /chk/chk.html server test2 10.17x.xxx.xxx:8010 cookie test2 weight 6 check inter 2000 rise 2 fall 3 backend server-name01 mode http balance roundrobin cookie SERVERID insert nocache option httpchk HEAD /chk/chk.html server srv130 10.1x2.2xx.xxx:8010 cookie srv130 weight 6 check inter 2000 rise 2 fall 3
