日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux7自带haprox版本,CentOS7.4—构建最新版haproxy高可用群集

發布時間:2023/12/19 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux7自带haprox版本,CentOS7.4—构建最新版haproxy高可用群集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CentOS7.4—構建haproxy高可用群集

目錄

第一部分 實驗環境

第二部分 搭建配置web服務器

第三部分 安裝配置haproxy服務器

第四部分 測試驗證

第五部分 haproxy配置相關詳細解釋

第一部分 實驗環境

一臺harpoxy調度服務器

IP地址:192.168.80.10

需要軟件:haproxy-1.7.10.tar

兩臺Web服務器(基于nginx)

IP地址:192.168.80.20(web01)

IP地址:192.168.80.30(web02)

需要軟件:nginx-1.13.9.tar.gz

//三臺服務器系統:linux—CentOS7.4

//軟件:

Win7客戶端一臺(驗證測試用)

IP地址:192.168.80.2

第二部分 搭建配置web服務器

第一步:配置web01

[root@web01 ~]# yum install -y \ //安裝相關插件及編譯安裝工具

pcre-devel \

zlib-devel \

make \

gcc \

gcc-c++

[root@web01 ~]# useradd -M -s /sbin/logogin nginx //創建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可執行程序放到系統環境中

[root@web01 nginx-1.13.9]# echo "

SERVER AA

" > /usr/local/nginx/html/index.html

//修改默認主頁顯示內容(便于后面測試)

[root@web01 nginx-1.13.9]# nginx //啟動nginx服務

[root@web01 nginx-1.13.9]# netstat -anpt | grep nginx

Win7訪問http://192.168.80.10

第二步:配置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 "

SERVER BB

" > /usr/local/nginx/html/index.html

[root@web02 nginx-1.13.9]# nginx

[root@web02 nginx-1.13.9]# netstat -anpt | grep nginx

Win7訪問http://192.168.80.20

第三部分 安裝配置haproxy服務器

[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 //標識64位系統

[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運行haproxy賬號并設置及屬主與屬組

[root@haproxy haproxy-1.7.10]# vi /etc/haproxy/haproxy.cfg //創建并編輯haproxy配置文件

--------------全局配置----------------

global

log 127.0.0.1 local2

#chroot /usr/local/haproxy-1.7.10

pidfile /var/run/haproxy.pid

maxconn 4000 //最大連接數

user haproxy

group haproxy

daemon //創建1個進程進入deamon模式運行,此參數要求將運行模式設置為daemon

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

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

use if not designated in their block

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

defaults

mode http //默認模式,tcp是四層,http是七層,health只會返回OK,若是混合模式則mode不需要設置

log global //采用全局定義的日志

option dontlognull //不記錄健康檢查的日志信息

option httpclose //每次請求完畢后主動關閉http通道

option httplog //日志類別http日志格式;如果是混合模式,此處還需要加上tcpclog

#option forwardfor //如果后端服務器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip

option redispatch //serverId對應的服務器掛掉后,強制定向到其他健康的服務器

timeout connect 10s //超時連接10s

timeout client 10s //客戶端超時連接10s

timeout server 10s //服務器連接超時

maxconn 60000 //最大連接數

retries 3 //3次連接失敗就認為服務不可用

--------------統計頁面配置------------------

listen admin_stats

bind 0.0.0.0:8089 //監聽端口

stats enable //啟用監聽端口

mode http

log global

stats uri /stats //統計頁面url

stats realm Haproxy\ Statistics //統計頁面密碼框上提示文本

stats auth admin:admin //統計頁面用戶名和密碼設置

#stats hide-version //隱藏統計頁面上HAProxy的版本信息

stats admin if TRUE //當通過認證才可管理

stats refresh 30s //頁面自動刷新時間30s

---------------web設置-----------------------

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

[root@haproxy haproxy-1.7.10]# systemctl stop firewalld

[root@haproxy haproxy-1.7.10]# setenforce 0

第四部分 驗證測試

Win7訪問調度器地址http://192.168.80.30

等待一會,再次訪問

//驗證成功

第五部分 haproxy配置相關詳細解釋

#全局配置, 用于設定義全局參數, 屬于進程級的配置, 通常與操作系統配置有關.

global

#定義全局日志, 配置在本地, 通過local0 輸出, 默認是info級別,可配置兩條

log 127.0.0.1 local0 warning

#定義日志級別【error warning info debug】

#log 127.0.0.1 local1 info

#運行路徑

chroot /usr/local/haproxy

#PID 文件存放路徑

pidfile /var/run/haproxy.pid

#設置每haproxy進程的最大并發連接數, 其等同于命令行選項“-n”; “ulimit -n”自動計算的結果參照此參數設定.

maxconn 4096

#運行haproxy 用戶, 或者使用關鍵字uid

user haproxy

#運行haproxy 用戶組, 或者使用關鍵字gid

group haproxy

#后臺運行haproxy

daemon

#設置啟動的haproxy進程數量, 只能用于守護進程模式的haproxy;

#默認只啟動一個進程, 鑒于調試困難等多方面的原因, 一般只在單進程僅能打開少數文件描述符的場景中才使用多進程模式.

nbproc 1

#設置每進程所能夠打開的最大文件描述符數目, 默認情況其會自動進行計算, 因此不推薦修改此選項.

#ulimit-n 819200

#調試級別, 一般只在開啟單進程時調試, 且生產環境禁用.

#debug

#haproxy啟動后不會顯示任何相關信息, 這與在命令行啟動haproxy時加上參數“-q”相同

#quiet

#定義統計信息保存位置

stats socket /usr/local/haproxy/stats

#默認配置

defaults

#默認的模式【tcp:4層; http:7層; health:只返回OK】

mode http

#繼承全局的日志定義輸出

log global

#日志類別, httplog

#option httplog

#如果后端服務器需要記錄客戶端真實ip, 需要在HTTP請求中添加”X-Forwarded-For”字段;

#但haproxy自身的健康檢測機制訪問后端服務器時, 不應將記錄訪問日志,可用except來排除127.0.0.0,即haproxy本身.

#option forwardfor except 127.0.0.0/8

option forwardfor

#開啟http協議中服務器端關閉功能, 每個請求完畢后主動關閉http通道, 使得支持長連接,使得會話可以被重用,使得每一個日志記錄都會被記錄.

option httpclose

#如果產生了一個空連接,那這個空連接的日志將不會記錄.

option dontlognull

#當與后端服務器的會話失敗(服務器故障或其他原因)時, 把會話重新分發到其他健康的服務器上; 當故障服務器恢復時, 會話又被定向到已恢復的服務器上;

#還可以用”retries”關鍵字來設定在判定會話失敗時的嘗試連接的次數

option redispatch

retries 3

#當haproxy負載很高時, 自動結束掉當前隊列處理比較久的鏈接.

option abortonclose

#默認http請求超時時間

timeout http-request 10s

#默認隊列超時時間, 后端服務器在高負載時, 會將haproxy發來的請求放進一個隊列中.

timeout queue 1m

#haproxy與后端服務器連接超時時間.

timeout connect 5s

#客戶端與haproxy連接后, 數據傳輸完畢, 不再有數據傳輸, 即非活動連接的超時時間.

timeout client 1m

#haproxy與后端服務器非活動連接的超時時間.

timeout server 1m

#默認新的http請求連接建立的超時時間,時間較短時可以盡快釋放出資源,節約資源.

timeout http-keep-alive 10s

#心跳檢測超時時間

timeout check 10s

#最大并發連接數

maxconn 2000

#設置默認的負載均衡方式

#balance source

#balnace leastconn

#統計頁面配置, frontend和backend的組合體, 監控組的名稱可按需自定義

listen admin_status

#配置監控運行模式

mode http

#配置統計頁面訪問端口

bind 0.0.0.0:1080

#統計頁面默認最大連接數

maxconn 10

#http日志格式

option httplog

#開啟統計

stats enable

#隱藏統計頁面上的haproxy版本信息

stats hide-version

#監控頁面自動刷新時間

stats refresh 30s

#統計頁面訪問url

stats uri /stats

#統計頁面密碼框提示文本

stats realm mCloud\ Haproxy

#監控頁面的用戶和密碼:admin, 可設置多個用戶名

stats auth admin:admin

#手工啟動/禁用后端服務器, 可通過web管理節點

stats admin if TRUE

#設置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

#監控haproxy后端服務器的監控狀態

listen site_status

bind 0.0.0.0:1081 #監聽端口

mode http #http的7層模式

log 127.0.0.1 local2 err #[err warning info debug]

monitor-uri /site_status #網站健康檢測URL,用來檢測HAProxy管理的網站是否可以用,正常返回200,不正常返回503

acl site_dead nbsrv(php_server) lt 1 #定義網站down時的策略當掛在負載均衡上的指定backend的中有效機器數小于1臺時返回true

acl site_dead nbsrv(html_server) lt 1

acl site_dead nbsrv(backend_default) lt 1

monitor fail if site_dead #當滿足策略的時候返回503,網上文檔說的是500,實際測試為503

monitor-net 192.168.4.171/32 #來自192.168.4.152的日志信息不會被記錄和轉發

monitor-net 192.168.4.172/32

#frontend, 名字自定義

frontend HAproxy_Cluster

#定義前端監聽端口, 建議采用bind :80的形式,否則做集群高可用的時候有問題,vip切換到其余機器就不能訪問.

bind 0.0.0.0:80

#acl后面是規則名稱,當請求的url末尾是以.php結尾時,匹配觸發php_web規則,以下兩種寫法均可.

#當請求的url末尾是以.css、.jpg、.png、.jpeg、.js、.gif結尾時,匹配并觸發static_web規則.

#acl static_web path_end .gif .png .jpg .css .js .jpeg

#acl static_web url_reg /.(css|jpg|png|jpeg|js|gif)$

#-i為忽略大小寫,當被請求的是以www.test.com開頭的主機時,匹配并觸發dns_name規則.

acl html_web hdr_beg(host) -i www.haproxytest.com

#acl html_web hdr_beg(host) 10.11.4.152

#當客戶端的IP是x.x.x.x時,匹配并觸發src_ip規則.

#acl src_ip src x.x.x.x

#如果匹配acl規則php_web,將請求轉交到php_server組處理;如果匹配acl規則html_web,將請求轉交到html_server組處理.

use_backend php_server if php_web

use_backend html_server if html_web

#如果以上規則都不匹配時,將請求轉交到default_backend組處理.

default_backend backend_default

#backend后端配置, 配置php_server組與html_server組

backend php_server

#定義負載均衡方式為roundrobin方式, 即基于權重進行輪詢調度的算法, 在服務器性能分布較均勻情況下推薦.

#另有如下幾種負載均衡方式:

#-- static-rr: 也是基于權重進行輪轉調度, 但屬于靜態方法, 運行時調整后端機組權重不會使用新的權重;

#-- source: 基于請求源IP進行hash運算匹配后端服務器組;

#-- leastconn: 不適合會話較短的環境, 如基于http的應用;

#-- uri: 對整個URI進行hash運算;

#-- uri_param: 對URI中的參數進行轉發;

#-- hdr():根據http頭進行轉發, 無該頭部則轉為使用roundrobin.

balance roundrobin

mode http

#允許插入serverid到cookie中,serverid后面可定義

cookie SERVERID

#心跳檢測方式為檢測后端服務器index.html文件,還有其他方式

option httpchk GET /index.html

#后端服務器定義, maxconn 1024表示該服務器的最大連接數, cookie 1表示serverid為1, weight代表權重(默認1,最大為265,0則表示不參與負載均衡),

#check inter 1500是檢測心跳頻率, rise 2是2次正確認為服務器可用, fall 3是3次失敗認為服務器不可用.

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

總結

以上是生活随笔為你收集整理的linux7自带haprox版本,CentOS7.4—构建最新版haproxy高可用群集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。