溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS7.4—構(gòu)建最新版haproxy高可用群集

發(fā)布時間:2020-06-28 01:56:22 來源:網(wǎng)絡(luò) 閱讀:5715 作者:0行云流水0 欄目:建站服務(wù)器

CentOS7.4—構(gòu)建haproxy高可用群集

目錄
第一部分 實驗環(huán)境
第二部分 搭建配置web服務(wù)器
第三部分 安裝配置haproxy服務(wù)器
第四部分 測試驗證
第五部分 haproxy配置相關(guān)詳細(xì)解釋

第一部分 實驗環(huán)境

  1. 一臺harpoxy調(diào)度服務(wù)器
    IP地址:192.168.80.10
    需要軟件:haproxy-1.7.10.tar
  2. 兩臺Web服務(wù)器(基于nginx)
    IP地址:192.168.80.20(web01)
    IP地址:192.168.80.30(web02)
    需要軟件:nginx-1.13.9.tar.gz
    //三臺服務(wù)器系統(tǒng):linux—CentOS7.4
    CentOS7.4—構(gòu)建最新版haproxy高可用群集
    //軟件:
    CentOS7.4—構(gòu)建最新版haproxy高可用群集
    CentOS7.4—構(gòu)建最新版haproxy高可用群集
  3. Win7客戶端一臺(驗證測試用)
    IP地址:192.168.80.2

第二部分 搭建配置web服務(wù)器
第一步:配置web01
[root@web01 ~]# yum install -y \ //安裝相關(guān)插件及編譯安裝工具

pcre-devel \
zlib-devel \
make \
gcc \
gcc-c++
[root@web01 ~]# useradd -M -s /sbin/logogin nginx //創(chuàng)建nginx程序用戶
[root@web01 ~]# tar xzvf nginx-1.13.9.tar.gz
[root@web01 ~]# cd nginx-1.13.9
[root@web01 nginx-1.13.9]# ./configure \ //定義配置
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@web01 nginx-1.13.9]# make && make install //編譯及安裝
[root@web01 nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //將nginx可執(zhí)行程序放到系統(tǒng)環(huán)境中
[root@web01 nginx-1.13.9]# echo "<h2>SERVER AA</h2>" > /usr/local/nginx/html/index.html
//修改默認(rèn)主頁顯示內(nèi)容(便于后面測試)
[root@web01 nginx-1.13.9]# nginx //啟動nginx服務(wù)
[root@web01 nginx-1.13.9]# netstat -anpt | grep nginx
CentOS7.4—構(gòu)建最新版haproxy高可用群集
Win7訪問http://192.168.80.10
CentOS7.4—構(gòu)建最新版haproxy高可用群集
第二步:配置web02(與web01一樣配置)
[root@web02 ~]# yum install -y \
pcre-devel \
zlib-devel \
gcc \
gcc-c++ \
make
[root@web02 ~]# useradd -M -s /sbin/nologin nginx
[root@web02 ~]# tar xzvf nginx-1.13.9.tar.gz
[root@web02 ~]# cd nginx-1.13.9
[root@web02 nginx-1.13.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@web02 nginx-1.13.9]# make && make install
[root@web02 nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@web02 nginx-1.13.9]# echo "<h2>SERVER BB</h2>" > /usr/local/nginx/html/index.html
[root@web02 nginx-1.13.9]# nginx
[root@web02 nginx-1.13.9]# netstat -anpt | grep nginx
CentOS7.4—構(gòu)建最新版haproxy高可用群集
Win7訪問http://192.168.80.20
CentOS7.4—構(gòu)建最新版haproxy高可用群集

第三部分 安裝配置haproxy服務(wù)器
[root@haproxy ~]# yum install -y \ //安裝插件及編譯工具

pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
[root@haproxy ~]# tar xzvf haproxy-1.7.10.tar.gz
[root@haproxy ~]# cd haproxy-1.7.10
[root@haproxy haproxy-1.7.10]# make TARGET=linux26 //標(biāo)識64位系統(tǒng)
[root@haproxy haproxy-1.7.10]# make install
[root@haproxy haproxy-1.7.10]# mkdir /etc/haproxy
[root@haproxy haproxy-1.7.10]# groupadd haproxy
[root@haproxy haproxy-1.7.10]# useradd -s /sbin/nologin -M -g haproxy haproxy //添加haproxy運(yùn)行haproxy賬號并設(shè)置及屬主與屬組
[root@haproxy haproxy-1.7.10]# vi /etc/haproxy/haproxy.cfg //創(chuàng)建并編輯haproxy配置文件
--------------全局配置----------------
global
log 127.0.0.1 local2
#chroot /usr/local/haproxy-1.7.10
pidfile /var/run/haproxy.pid
maxconn 4000 //最大連接數(shù)
user haproxy
group haproxy
daemon //創(chuàng)建1個進(jìn)程進(jìn)入deamon模式運(yùn)行,此參數(shù)要求將運(yùn)行模式設(shè)置為daemon

#---------------------------------------------------------------------

common defaults that all the 'listen' and 'backend' sections will

use if not designated in their block

#---------------------------------------------------------------------
defaults
mode http //默認(rèn)模式,tcp是四層,http是七層,health只會返回OK,若是混合模式則mode不需要設(shè)置
log global //采用全局定義的日志
option dontlognull //不記錄健康檢查的日志信息
option httpclose //每次請求完畢后主動關(guān)閉http通道
option httplog //日志類別http日志格式;如果是混合模式,此處還需要加上tcpclog
#option forwardfor //如果后端服務(wù)器需要獲得客戶端真實ip需要配置的參數(shù),可以從Http Header中獲得客戶端ip
option redispatch //serverId對應(yīng)的服務(wù)器掛掉后,強(qiáng)制定向到其他健康的服務(wù)器
timeout connect 10s //超時連接10s
timeout client 10s //客戶端超時連接10s
timeout server 10s //服務(wù)器連接超時
maxconn 60000 //最大連接數(shù)
retries 3 //3次連接失敗就認(rèn)為服務(wù)不可用
--------------統(tǒng)計頁面配置------------------
listen admin_stats
bind 0.0.0.0:8089 //監(jiān)聽端口
stats enable //啟用監(jiān)聽端口
mode http
log global
stats uri /stats //統(tǒng)計頁面url
stats realm Haproxy\ Statistics //統(tǒng)計頁面密碼框上提示文本
stats auth admin:admin //統(tǒng)計頁面用戶名和密碼設(shè)置
#stats hide-version //隱藏統(tǒng)計頁面上HAProxy的版本信息
stats admin if TRUE //當(dāng)通過認(rèn)證才可管理
stats refresh 30s //頁面自動刷新時間30s
---------------web設(shè)置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web01 192.168.80.10:80 check inter 2000 fall 3
server web02 192.168.80.20:80 check inter 2000 fall 3
保存退出
[root@haproxy haproxy-1.7.10]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.7.10]# chmod 755 /etc/init.d/haproxy
[root@haproxy haproxy-1.7.10]# chkconfig --add haproxy
[root@haproxy haproxy-1.7.10]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy haproxy-1.7.10]# service haproxy start
[root@haproxy haproxy-1.7.10]# netstat -anpt | grep haproxy
CentOS7.4—構(gòu)建最新版haproxy高可用群集
[root@haproxy haproxy-1.7.10]# systemctl stop firewalld
[root@haproxy haproxy-1.7.10]# setenforce 0

第四部分 驗證測試
Win7訪問調(diào)度器地址http://192.168.80.30
CentOS7.4—構(gòu)建最新版haproxy高可用群集
等待一會,再次訪問
CentOS7.4—構(gòu)建最新版haproxy高可用群集
//驗證成功

第五部分 haproxy配置相關(guān)詳細(xì)解釋
#全局配置, 用于設(shè)定義全局參數(shù), 屬于進(jìn)程級的配置, 通常與操作系統(tǒng)配置有關(guān).
global
#定義全局日志, 配置在本地, 通過local0 輸出, 默認(rèn)是info級別,可配置兩條
log 127.0.0.1 local0 warning
#定義日志級別【error warning info debug】
#log 127.0.0.1 local1 info
#運(yùn)行路徑
chroot /usr/local/haproxy
#PID 文件存放路徑
pidfile /var/run/haproxy.pid
#設(shè)置每haproxy進(jìn)程的最大并發(fā)連接數(shù), 其等同于命令行選項“-n”; “ulimit -n”自動計算的結(jié)果參照此參數(shù)設(shè)定.
maxconn 4096
#運(yùn)行haproxy 用戶, 或者使用關(guān)鍵字uid
user haproxy
#運(yùn)行haproxy 用戶組, 或者使用關(guān)鍵字gid
group haproxy
#后臺運(yùn)行haproxy
daemon
#設(shè)置啟動的haproxy進(jìn)程數(shù)量, 只能用于守護(hù)進(jìn)程模式的haproxy;
#默認(rèn)只啟動一個進(jìn)程, 鑒于調(diào)試?yán)щy等多方面的原因, 一般只在單進(jìn)程僅能打開少數(shù)文件描述符的場景中才使用多進(jìn)程模式.
nbproc 1
#設(shè)置每進(jìn)程所能夠打開的最大文件描述符數(shù)目, 默認(rèn)情況其會自動進(jìn)行計算, 因此不推薦修改此選項.
#ulimit-n 819200
#調(diào)試級別, 一般只在開啟單進(jìn)程時調(diào)試, 且生產(chǎn)環(huán)境禁用.
#debug
#haproxy啟動后不會顯示任何相關(guān)信息, 這與在命令行啟動haproxy時加上參數(shù)“-q”相同
#quiet
#定義統(tǒng)計信息保存位置
stats socket /usr/local/haproxy/stats
#默認(rèn)配置
defaults
#默認(rèn)的模式【tcp:4層; http:7層; health:只返回OK】
mode http
#繼承全局的日志定義輸出
log global
#日志類別, httplog
#option httplog
#如果后端服務(wù)器需要記錄客戶端真實ip, 需要在HTTP請求中添加”X-Forwarded-For”字段;
#但haproxy自身的健康檢測機(jī)制訪問后端服務(wù)器時, 不應(yīng)將記錄訪問日志,可用except來排除127.0.0.0,即haproxy本身.
#option forwardfor except 127.0.0.0/8
option forwardfor
#開啟http協(xié)議中服務(wù)器端關(guān)閉功能, 每個請求完畢后主動關(guān)閉http通道, 使得支持長連接,使得會話可以被重用,使得每一個日志記錄都會被記錄.
option httpclose
#如果產(chǎn)生了一個空連接,那這個空連接的日志將不會記錄.
option dontlognull
#當(dāng)與后端服務(wù)器的會話失敗(服務(wù)器故障或其他原因)時, 把會話重新分發(fā)到其他健康的服務(wù)器上; 當(dāng)故障服務(wù)器恢復(fù)時, 會話又被定向到已恢復(fù)的服務(wù)器上;
#還可以用”retries”關(guān)鍵字來設(shè)定在判定會話失敗時的嘗試連接的次數(shù)
option redispatch
retries 3
#當(dāng)haproxy負(fù)載很高時, 自動結(jié)束掉當(dāng)前隊列處理比較久的鏈接.
option abortonclose
#默認(rèn)http請求超時時間
timeout http-request 10s
#默認(rèn)隊列超時時間, 后端服務(wù)器在高負(fù)載時, 會將haproxy發(fā)來的請求放進(jìn)一個隊列中.
timeout queue 1m
#haproxy與后端服務(wù)器連接超時時間.
timeout connect 5s
#客戶端與haproxy連接后, 數(shù)據(jù)傳輸完畢, 不再有數(shù)據(jù)傳輸, 即非活動連接的超時時間.
timeout client 1m
#haproxy與后端服務(wù)器非活動連接的超時時間.
timeout server 1m
#默認(rèn)新的http請求連接建立的超時時間,時間較短時可以盡快釋放出資源,節(jié)約資源.
timeout http-keep-alive 10s
#心跳檢測超時時間
timeout check 10s
#最大并發(fā)連接數(shù)
maxconn 2000
#設(shè)置默認(rèn)的負(fù)載均衡方式
#balance source
#balnace leastconn
#統(tǒng)計頁面配置, frontend和backend的組合體, 監(jiān)控組的名稱可按需自定義
listen admin_status
#配置監(jiān)控運(yùn)行模式
mode http
#配置統(tǒng)計頁面訪問端口
bind 0.0.0.0:1080
#統(tǒng)計頁面默認(rèn)最大連接數(shù)
maxconn 10
#http日志格式
option httplog
#開啟統(tǒng)計
stats enable
#隱藏統(tǒng)計頁面上的haproxy版本信息
stats hide-version
#監(jiān)控頁面自動刷新時間
stats refresh 30s
#統(tǒng)計頁面訪問url
stats uri /stats
#統(tǒng)計頁面密碼框提示文本
stats realm mCloud\ Haproxy
#監(jiān)控頁面的用戶和密碼:admin, 可設(shè)置多個用戶名
stats auth admin:admin
#手工啟動/禁用后端服務(wù)器, 可通過web管理節(jié)點(diǎn)
stats admin if TRUE
#設(shè)置haproxy錯誤頁面
errorfile 400 /usr/local/haproxy/errorfiles/400.http
errorfile 403 /usr/local/haproxy/errorfiles/403.http
errorfile 408 /usr/local/haproxy/errorfiles/408.http
errorfile 500 /usr/local/haproxy/errorfiles/500.http
errorfile 502 /usr/local/haproxy/errorfiles/502.http
errorfile 503 /usr/local/haproxy/errorfiles/503.http
errorfile 504 /usr/local/haproxy/errorfiles/504.http
#監(jiān)控haproxy后端服務(wù)器的監(jiān)控狀態(tài)
listen site_status
bind 0.0.0.0:1081 #監(jiān)聽端口
mode http #http的7層模式
log 127.0.0.1 local2 err #[err warning info debug]
monitor-uri /site_status #網(wǎng)站健康檢測URL,用來檢測HAProxy管理的網(wǎng)站是否可以用,正常返回200,不正常返回503
acl site_dead nbsrv(php_server) lt 1 #定義網(wǎng)站down時的策略當(dāng)掛在負(fù)載均衡上的指定backend的中有效機(jī)器數(shù)小于1臺時返回true
acl site_dead nbsrv(html_server) lt 1
acl site_dead nbsrv(backend_default) lt 1
monitor fail if site_dead #當(dāng)滿足策略的時候返回503,網(wǎng)上文檔說的是500,實際測試為503
monitor-net 192.168.4.171/32 #來自192.168.4.152的日志信息不會被記錄和轉(zhuǎn)發(fā)
monitor-net 192.168.4.172/32
#frontend, 名字自定義
frontend HAproxy_Cluster
#定義前端監(jiān)聽端口, 建議采用bind :80的形式,否則做集群高可用的時候有問題,vip切換到其余機(jī)器就不能訪問.
bind 0.0.0.0:80
#acl后面是規(guī)則名稱,當(dāng)請求的url末尾是以.php結(jié)尾時,匹配觸發(fā)php_web規(guī)則,以下兩種寫法均可.
#當(dāng)請求的url末尾是以.css、.jpg、.png、.jpeg、.js、.gif結(jié)尾時,匹配并觸發(fā)static_web規(guī)則.
#acl static_web path_end .gif .png .jpg .css .js .jpeg
#acl static_web url_reg /
.(css|jpg|png|jpeg|js|gif)$
#-i為忽略大小寫,當(dāng)被請求的是以www.test.com開頭的主機(jī)時,匹配并觸發(fā)dns_name規(guī)則.
acl html_web hdr_beg(host) -i www.haproxytest.com
#acl html_web hdr_beg(host) 10.11.4.152
#當(dāng)客戶端的IP是x.x.x.x時,匹配并觸發(fā)src_ip規(guī)則.
#acl src_ip src x.x.x.x
#如果匹配acl規(guī)則php_web,將請求轉(zhuǎn)交到php_server組處理;如果匹配acl規(guī)則html_web,將請求轉(zhuǎn)交到html_server組處理.
use_backend php_server if php_web
use_backend html_server if html_web
#如果以上規(guī)則都不匹配時,將請求轉(zhuǎn)交到default_backend組處理.
default_backend backend_default
#backend后端配置, 配置php_server組與html_server組
backend php_server
#定義負(fù)載均衡方式為roundrobin方式, 即基于權(quán)重進(jìn)行輪詢調(diào)度的算法, 在服務(wù)器性能分布較均勻情況下推薦.
#另有如下幾種負(fù)載均衡方式:
#-- static-rr: 也是基于權(quán)重進(jìn)行輪轉(zhuǎn)調(diào)度, 但屬于靜態(tài)方法, 運(yùn)行時調(diào)整后端機(jī)組權(quán)重不會使用新的權(quán)重;
#-- source: 基于請求源IP進(jìn)行hash運(yùn)算匹配后端服務(wù)器組;
#-- leastconn: 不適合會話較短的環(huán)境, 如基于http的應(yīng)用;
#-- uri: 對整個URI進(jìn)行hash運(yùn)算;
#-- uri_param: 對URI中的參數(shù)進(jìn)行轉(zhuǎn)發(fā);
#-- hdr(<name>):根據(jù)http頭進(jìn)行轉(zhuǎn)發(fā), 無該頭部則轉(zhuǎn)為使用roundrobin.
balance roundrobin
mode http
#允許插入serverid到cookie中,serverid后面可定義
cookie SERVERID
#心跳檢測方式為檢測后端服務(wù)器index.html文件,還有其他方式
option httpchk GET /index.html
#后端服務(wù)器定義, maxconn 1024表示該服務(wù)器的最大連接數(shù), cookie 1表示serverid為1, weight代表權(quán)重(默認(rèn)1,最大為265,0則表示不參與負(fù)載均衡),
#check inter 1500是檢測心跳頻率, rise 2是2次正確認(rèn)為服務(wù)器可用, fall 3是3次失敗認(rèn)為服務(wù)器不可用.
server php1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3
backend html_server
balance source
mode http
server html1 192.168.4.172:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3
backend backend_default
balance source
mode http
server default1 192.168.4.171:80 maxconn 1024 cookie 1 weight 3 check inter 1500 rise 2 fall 3

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI