Haproxy安裝及配置

wget https://github.com/haproxy/haproxy/archive/v1.7.0.tar.gz
tar -xzvf v1.7.0.tar.gz
cd haproxy-1.7.0
make TARGET=linux26 PREFIX=/data/haproxy #haproxy安裝位置
make install PREFIX=/data/haproxy
#參數說明TARGET=linux26 #內核版本,使用uname -r查看內核,如:2.6.18-371.el5,此時該參數就爲linux26;kernel 大於2.6.28的用:TARGET=linux2628ARCH=x86_64 #系統位數PREFIX=/usr/local/haprpxy #/usr/local/haprpxy爲haprpxy安裝路徑

建立配置文件:Haproxy.cfg
啓動:
/data/haproxy/sbin/haproxy -f /data/haproxy/haproxy.cfg
結束
ps -ef|grep haproxy
Kill -2 pid
查看狀態
http://ip:1080/stats
Haproxy.cfg:
###########全局配置#########
global
    log 127.0.0.1 local0 #[日誌輸出配置,所有日誌都記錄在本機,通過local0輸出]
    log 127.0.0.1 local1 notice #定義haproxy 日誌級別[error warringinfo debug]
    daemon #以後臺形式運行harpoxy
    nbproc 1 #設置進程數量
    maxconn 4096 #默認最大連接數,需考慮ulimit-n限制
    #user haproxy #運行haproxy的用戶
    #group haproxy #運行haproxy的用戶所在的組
    #pidfile /var/run/haproxy.pid #haproxy 進程PID文件
    #ulimit-n 819200 #ulimit 的數量限制
    #chroot /usr/share/haproxy #chroot運行路徑
    #debug #haproxy 調試級別,建議只在開啓單進程的時候調試
    #quiet

########默認配置############
defaults
    log global
    mode http #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
    option httplog #日誌類別,採用httplog
    option dontlognull #不記錄健康檢查日誌信息
    retries 2 #兩次連接失敗就認爲是服務器不可用,也可以通過後面設置
    #option forwardfor #如果後端服務器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip
    option httpclose #每次請求完畢後主動關閉http通道,haproxy不支持keep-alive,只能模擬這種模式的實現
    #option redispatch #當serverId對應的服務器掛掉後,強制定向到其他健康的服務器,以後將不支持
    option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接
    maxconn 4096 #默認的最大連接數
    timeout connect 5000ms #連接超時
    timeout client 30000ms #客戶端超時
    timeout server 30000ms #服務器超時
    #timeout check 2000 #心跳檢測超時
    #timeout http-keep-alive10s #默認持久連接超時時間
    #timeout http-request 10s #默認http請求超時時間
    #timeout queue 1m #默認隊列超時時間
     #balance roundrobin #設置默認負載均衡方式,輪詢方式
    #balance source #設置默認負載均衡方式,類似於nginx的ip_hash
   balnace leastconn #設置默認負載均衡方式,最小連接數

########統計頁面配置########
listen stats
    bind 0.0.0.0:1080 #設置Frontend和Backend的組合體,監控組的名稱,按需要自定義名稱
    mode http #http的7層模式
    option httplog #採用http日誌格式
    #log 127.0.0.1 local0 err #錯誤日誌記錄
    maxconn 10 #默認的最大連接數	
    stats refresh 30s #統計頁面自動刷新時間
    stats uri /stats #統計頁面url
    stats realm XingCloud\ Haproxy #統計頁面密碼框上提示文本
    stats auth admin:admin #設置監控頁面的用戶和密碼:admin,可以設置多個用戶名
    stats auth Frank:Frank #設置監控頁面的用戶和密碼:Frank
    stats hide-version #隱藏統計頁面上HAProxy的版本信息
    stats admin if TRUE #設置手工啓動/禁用,後端服務器(haproxy-1.4.9以後版本)

########設置haproxy 錯誤頁面#####
#errorfile 403 /home/haproxy/haproxy/errorfiles/403.http
#errorfile 500 /home/haproxy/haproxy/errorfiles/500.http
#errorfile 502 /home/haproxy/haproxy/errorfiles/502.http
#errorfile 503 /home/haproxy/haproxy/errorfiles/503.http
#errorfile 504 /home/haproxy/haproxy/errorfiles/504.http
####################################################################
listen http_front
	bind 0.0.0.0:1080           #監聽端口  
	stats refresh 30s           #統計頁面自動刷新時間  
	stats uri /haproxy?stats            #統計頁面url  
	stats realm Haproxy Manager #統計頁面密碼框上提示文本  
	stats auth admin:admin      #統計頁面用戶名和密碼設置  
	#stats hide-version         #隱藏統計頁面上HAProxy的版本信息

#####################我把RabbitMQ的管理界面也放在HAProxy後面了
listen rabbitmq_admin 
    bind 0.0.0.0:15673
    timeout connect 5000ms 
    timeout client 30000ms 
    timeout server 30000ms
    server node1 10.160.84.74:15672
    server node2 10.160.84.75:15672 
####################################################################
listen rabbitmq_cluster 
    bind 0.0.0.0:5673
    option tcplog
    mode tcp
    timeout connect 5000ms 
    timeout client 30000ms 
    timeout server 30000ms 
    option          clitcpka
    balance roundrobin      #負載均衡算法(#banlance roundrobin 輪詢,balance source 保存session值,支持static-rr,leastconn,first,uri等參數)
    #balance url_param userid
    #balance url_param session_id check_post 64
    #balance hdr(User-Agent)
    #balance hdr(host)
    #balance hdr(Host) use_domain_only
    #balance rdp-cookie
    #balance leastconn
    #balance source //ip
    server   node1 ip1:5672 check inter 5s rise 2 fall 3   #check inter 2000 是檢測心跳頻率,rise 2是2次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用
    server   node2 ip2:5672 check inter 5s rise 2 fall 3




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章