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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

haproxy实现高可用及负载均衡

發布時間:2023/11/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 haproxy实现高可用及负载均衡 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Haproxy簡介:

? ? ? ?? Haproxy是一個使用c語言編寫的自由開發源代碼軟件,它提供高可用性、負載均衡、以及基于http和tcp的應用程序代理。Haproxy特別使用于那些負載特別大的web站點。Haproxy運行在當前的硬件上,完全可以支持數以萬計的并發連接,并且他的運行模式使得它可以很簡單安全的整合進當前的架構中,同時也可以保護我們的web服務器不被暴露再網絡上。

haproxy采用的八種算法

roundrobin????????????????? 簡單的輪詢
static-rr??????????????????????? 根據權重
leastconn??????????????????? 最少連接者先處理
source???????????????????????? 根據請求源IP
uri??????????????????????????????? 根據請求的URI
url_param???? ? ? ? ? ? ?? 根據請求的URl參數
hdr(name)????????????????? 根據HTTP請求頭來鎖定每一次HTTP請求
rdp-cookie(name)???? 根據cookie(name)來鎖定并哈希每一次TCP請求
?

haproxy的安裝配置及負載均衡

環境: rhel6.5

主機:foundations: 172.25.1.250??????? //用于客戶端進行測試

test1: 172.25.1.11???????? //安裝haproxy,用來實現負載均衡,但有瓶頸

test2: 172.25.1.12?????? ? //開啟http服務 ,實現負載均衡的真實服務器

test3: 172.25.1.13???? ? ? //開啟http服務 ,實現負載均衡的真實服務器

test1端:

首先在test1中下載haproxy的源碼包,解壓

?yum install rpm-build? -y? ? ? ? ? # 安裝rpmbuild命令

rpmbuild -tb haproxy-1.6.13.tar.gz ? ? ?? # 建立haproxy的二進制軟件包, 有依賴性,需下載

yum install -y pcre-devel

rpmbuild -tb haproxy-1.6.13.tar.gz?????? //再次執行

cd rpmbuild/RPMS/x86_64

rpm? -ivh? haproxy-1.6.13-1.x86_64.rpm ? ? # 安裝haproxy軟件包

rpm? -ql? haproxy ? ?? # 查看軟件相關文件的路徑,生成haproxy的配置文件

cd? /root/

?tar? zxf? haproxy-1.6.13.tar.gz??????? # 解壓壓縮包。

cd haproxy-1.6.13/examples

touch /etc/haproxy/haproxy.cfg???????? # 若目錄haproxy不存在,則須新建

cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg ? ? ? ? ? ?

useradd? haproxy?????????????????? //創建haproxy用戶,并修改配置文件

id haproxy

vim? /etc/haproxy/haproxy.cfg
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.

global
??????? maxconn???????? 10000???????? //最大連接數

??????? stats socket??? /var/run/haproxy.stat mode 600 level admin
??????? log???????????? 127.0.0.1 local0???????????????? //日志設備

??????? uid???????????? 500????????????????? //用戶的gid和uid
??????? gid???????????? 500
??????? chroot????????? /var/empty????????? //安全設置,根目錄切換,鎖在/var/empty下
??????? daemon?????????????????????????? //設置為后臺進程

# The public 'www' address in the DMZ
frontend public
?? ?bind???????????? 172.25.1.11:1080 name clear
?? ?bind???????????? *:1080????????? //端口隨意設置,但不可與其他服務沖突,默認1080
??????? #bind??????????? 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
??????? mode???????????? http???????????????? //haproxy工作模式,四層工作模式為TCP,七層為HTTP
? ? ? ? log????????????? global
?? ???? option?????????? httplog??????????? //啟用http日志
?? ???? option?????????? dontlognull???
?? ???? monitor-uri????? /monitoruri
??????? maxconn???????? 8000??????????????????? //每個進程的最大連接數,會覆蓋global中的maxconn
??????? timeout client? 30s???????????? //客戶端超時時長

??????? stats uri?????? /admin/stats???????????? //監控頁面的URL設置
#??????? use_backend???? static if { hdr_beg(host) -i img }
#??????? use_backend???? static if { path_beg /img /css?? }
?? ?default_backend static

# The static backend backend for 'Host: img', /img and /css.
backend static
??????? mode??????????? http
??????? balance???????? roundrobin???????? //實現輪詢
??????? option prefer-last-server
??????? retries???????? 2????????????????? //服務器連接失敗后的重試次數
??????? option redispatch??????
??????? timeout connect 5s?????????? //連接最大超時時間
??????? timeout server? 5s??????????? //服務器最大超時時間
??????? server????????? statsrv1 172.25.1.12:80 check inter 1000
??????? server????????? statsrv2 172.25.1.13:80 check inter 1000

修改內核最大文件數

vim? /etc/security/limits.conf???????

haproxy ? ? - ? ? nofile ?? 10000????????????? //寫在最后一行

啟動服務,測試haproxy是否安裝成功

/etc/init.d/haproxy ? start

netstat? -antlp| grep? 1080 ? ? ? ? # 查看1080端口是否開啟

test2端和test3端:

yum install -y httpd??????? //下載http服務

/etc/init.d/httpd start????? //開啟服務

vim /var/www/html/index.html????????? //寫發布頁面

test2?????????????? //為了方便觀察哪個主機出現故障及是否輪詢,故 如果是主機test3,發布頁面就寫test3

?

此時,在真機中進行負載均衡測試,輪詢的方式

[root@foundation1 ~]# curl test1:1080

網頁驗證:

http://172.25.1.11:1080/admin/stats???????????????? //對后端服務器管理和頁面數據的監控

http://172.25.1.11:1080/monitoruri????????????? //網站健康檢測URL,檢測haproxy管理的網站是否可用,若正常則返回200 OK

基于tcp和http的應用程序代理

動態頁面和靜態頁面的分離

在test2中的httpd默認發布目錄中編寫文件index.html(test2);在test3中http默認發布目錄編寫文件index.php

test2:??? 頁面不用變,即:

[root@test2 ~]# cat /var/www/html/index.html

test3:

yum install -y php php-mysql ? ? ? ?? # 在test3中安裝php動態頁面

vim /var/www/html/index.php?

/etc/init.d/httpd? restart

test1:

vim? /etc/haproxy/haproxy.cfg?????????????? //在haproxy的配置文件中修改訪問動態頁面和靜態頁面

# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#

global
??????? maxconn???????? 10000
??????? stats socket??? /var/run/haproxy.stat mode 600 level admin
??????? log???????????? 127.0.0.1 local0
??????? uid???????????? 500
??????? gid???????????? 500
??????? chroot????????? /var/empty
??????? daemon
defaults

??????? mode??????????? http
??????? log???????????? global
??????? option????????? httplog
??????? option????????? dontlognull
??????? monitor-uri???? /monitoruri
??????? maxconn???????? 8000
??????? timeout client? 30s
??????? stats uri?????? /admin/stats
??????? option prefer-last-server
??????? retries???????? 2
??????? option redispatch
??????? timeout connect 5s
??????? timeout server? 5s
# The public 'www' address in the DMZ
frontend public
???????? bind??????????? 172.25.1.11:1080 name clear
??????? #bind??????????? *:80
??????? #bind??????????? 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem??????? #use_backend???? static if { hdr_beg(host) -i img }
??????? #use_backend???? static if { path_beg /img /css?? }

??????? use_backend???? static2 if { path_end -i .php?? }??????????????????? //如果以.php結尾,則顯示static2,即172.25.1.13:80的內容
??????? default_backend static1?????????????????? //默認顯示static1的內容,即172.25.1.12:80的內容

??????? #acl blacklist src 172.25.1.250

?? ? ?? #http-request deny if blacklist
?? ? ?? #errorloc?? 403 http://172.25.1.11:8080

# The static backend backend for 'Host: img', /img and /css.
backend static1
??????? balance???????? roundrobin
??????? server????????? statsrv1 172.25.1.12:80 check inter 1000
backend static2
??????? balance??????? roundrobin
??????? server?????????? statsrv2 172.25.1.13:80 check inter 1000

[root@test1 ~]# /etc/init.d/haproxy restart

[root@test1 ~]# netstat -antlp |grep haproxy

在瀏覽器中進行測試:

默認靜態訪問test2的index.html頁面

動態訪問test3的index.php界面

?

修改haproxy的日志文件

test1:?????? 修改日志服務配置文件從而修改日志存儲位置

vim /etc/rsyslog.conf????????

?/etc/init.d/rsyslog restart?????????????? //重啟日志服務

?> /var/log/haproxy.log????????????? //將日志清空

/etc/init.d/haproxy restart??????????? ? //重啟haproxy服務,
cat? /var/log/haproxy.log ? ? ? ?? # 查看是否生成日志

訪問控制

test1:?? 修改配置文件,添加訪問控制的主機

vim /etc/haproxy/haproxy.conf??????? //設置訪問黑名單,在blacklist中的主機均不可訪問,可寫ip或網段

//這里設置不允許主機foundation1訪問

/etc/init.d/haproxy? restart

在瀏覽器測試

http://172.25.1.11:1080/

重定向(當主機foundation1進行訪問時,自動重定向到test1中)

vim? /etc/haproxy/haproxy.conf??????? # 打開httpd服務

??????? acl blacklist src 172.25.1.250????????????????? //黑名單
??????? http-request deny if blacklist???????????????? ?? //若是黑名單則服務拒絕
??????? errorloc?? 403 http://172.25.1.11:80???????

//如訪問到403則直接重定向到172.25.1.11 的http服務,即test1的本機http服務,端口為http的端口,這里http端口是80

/etc/init.d/haproxy ? restart

vim /var/www/html/index.html?????? //內容如下

<h1>服務正在維修,請等待......</h1>

/etc/init.d/httpd? start

netstat -antlp | grep httpd

測試結果:

172.25.1.11:1080

//已經重定位到了test1的http頁面

讀寫分離

在test1中編輯配置文件

vim? /etc/haproxy.haproxy.conf?????????? //當進行上傳操作時,轉到test3

??????? acl???????????? write? ? ? method? POST??????????? ? ? ? ? //寫的方式有 POST和PUT
??????? acl???????????? write????? method? PUT
??????? use_backend???????? static2?? if????? write????????????????? //當寫的時候用static2
??????? default_backend?? static1???????????????????????????????? ?? //默認使用static1

/etc/init.d/haproxy? restart

在test2和test3中安裝php,把上傳腳本放在http默認發布目錄

yum install -y php

test3:

cd /root?????????????????? //注意:這里需要自己下載上傳腳本,提前將上傳腳本放到/root下

mv update /var/www/html??

cd /var/www/html/

chmod 777 update

cd update

ls

mv * ..

此時,在瀏覽器進行訪問:

在網頁進行上傳照片,然后在test3中的update目錄下查看是否已經上傳到update目錄,已上傳則表示測試成功。

corosync+pacemaker心跳組件實現haproxy的高可用

pacemaker是一個集群管理資源器。利用集群基礎構件(corosync或heartbeat)提供消息和成員管理能力來探測并從節點或資源級別的故障恢復,以實現集群服務的最大可能性

為了實現test1與test4的集群管理,test4上與test1作相同的配置。當一個節點故障時,另一個節點會替代進行工作。

故在test4中安裝haproxy服務。操作步驟與test1相同:

結果為:

在test1和test4中,開啟corosync服務,執行下列操作:

yum install pacemaker corosync -y

cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf

///安裝crm管理工具,下面這兩個軟件須自行下載

yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-4.1.x86_64.rpm

下載時需要依賴包,python-pssh,須自行下載

?????????? Requires: python-pssh = 2.3.1-4.1??????? //報錯

?yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-4.1.x86_64.rpm python-pssh-2.3.1-4.1.x86_64.rpm

vim /etc/corosync/corosync.conf

compatibility: whitetank

totem {
??????? version: 2
??????? secauth: off
??????? threads: 0
??????? interface {
??????????????? ringnumber: 0
??????????????? bindnetaddr: 172.25.1.0
??????????????? mcastaddr: 226.94.1.1
??????????????? mcastport: 5405
??????????????? ttl: 1
??????? }
}

logging {
??????? fileline: off
??????? to_stderr: no
??????? to_logfile: yes
??????? to_syslog: yes
??????? logfile: /var/log/cluster/corosync.log
??????? debug: off
??????? timestamp: on
??????? logger_subsys {
??????????????? subsys: AMF
??????????????? debug: off
??????? }
}

amf {
??????? mode: disabled
}
service{
??????? name:pacemaker
??????? ver:0
}

aisexce {
??????? user: root
??????? group: root
}

quorum {
??????? provider: corosync_votequorum
??????? expected_votes: 2
??????? two_node: 1
}

進行測試:

[root@test1 ~]# crm status

[root@test1 ~]# crm node standby

總結

以上是生活随笔為你收集整理的haproxy实现高可用及负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。

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

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:haproxy实现高可用及负载均衡