CentOS7—HAProxy安裝與配置詳解
概述
Haproxy下載地址:http://pkgs.fedoraproject.org/repo/pkgs/haproxy/
關(guān)閉SElinux、配置防火墻
1、vi /etc/selinux/config
#SELINUX=enforcing #注釋掉 #SELINUXTYPE=targeted #注釋掉 SELINUX=disabled #增加 :wq! #保存退出 setenforce 0 #使配置立即生效
2、vi /etc/sysconfig/iptables #編輯
-A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT #允許組播地址通信 -A RH-Firewall-1-INPUT -p vrrp -j ACCEPT #允許VRRP(虛擬路由器冗余協(xié))通信 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允許80端口通過防火墻 :wq! #保存退出 /etc/init.d/iptables restart #重啟防火墻使配置生效
安裝HAProxy
1、創(chuàng)建HAProxy運(yùn)行賬戶和組
groupadd haproxy #添加haproxy組 useradd -g haproxy haproxy -s /bin/false #創(chuàng)建nginx運(yùn)行賬戶haproxy并加入到haproxy組,不允許haproxy用戶直接登錄系統(tǒng)
2、安裝:
[root@A local]# yum install -y gcc [root@A local]# tar zxvf haproxy-1.6.9.tar.gz [root@A local]# cd haproxy-1.6.9 [root@A local]# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy #編譯 uname -r #查看系統(tǒng)內(nèi)核版本號(hào) [root@A local]# make install PREFIX=/usr/local/haproxy #安裝 #數(shù)說明: #TARGET=linux3100 #使用uname -r查看內(nèi)核,如:2.6.18-371.el5,此時(shí)該參數(shù)就為linux26 #kernel 大于2.6.28的用:TARGET=linux2628 #CPU=x86_64 #使用uname -r查看系統(tǒng)信息,如x86_64 x86_64 x86_64 GNU/Linux,此時(shí)該參數(shù)就為x86_64 #PREFIX=/usr/local/haprpxy #/usr/local/haprpxy為haprpxy安裝路徑
3、設(shè)置HAProxy
mkdir -p /usr/local/haproxy/conf #創(chuàng)建配置文件目錄 mkdir -p /etc/haproxy #創(chuàng)建配置文件目錄 touch /usr/local/haproxy/conf/haproxy.cfg #創(chuàng)建配置文件 ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件軟連接 cp -r /usr/local/src/haproxy-1.6.9/examples/errorfiles /usr/local/haproxy/errorfiles #拷貝錯(cuò)誤頁面 ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加軟連接 mkdir -p /usr/local/haproxy/log #創(chuàng)建日志文件目錄 touch /usr/local/haproxy/log/haproxy.log #創(chuàng)建日志文件 ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加軟連接 cp /usr/local/src/haproxy-1.6.9/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷貝開機(jī)啟動(dòng)文件 chmod +x /etc/rc.d/init.d/haproxy #添加腳本執(zhí)行權(quán)限 chkconfig haproxy on #設(shè)置開機(jī)啟動(dòng) ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加軟連接
4、配置haproxy.cfg參數(shù)
cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg-bak #備份 vi /usr/local/haproxy/conf/haproxy.cfg #編輯,修改
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global log 127.0.0.1 local2 ###[err warning info debug] chroot /usr/local/haproxy pidfile /var/run/haproxy.pid ###haproxy的pid存放路徑,啟動(dòng)進(jìn)程的用戶必須有權(quán)限訪問此文件 maxconn 4000 ###最大連接數(shù),默認(rèn)4000 user haproxy group haproxy daemon ###創(chuàng)建1個(gè)進(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)的模式mode { tcp|http|health },tcp是4層,http是7層,health只會(huì)返回OK log global ###采用全局定義的日志 option dontlognull ###不記錄健康檢查的日志信息 option httpclose ###每次請(qǐng)求完畢后主動(dòng)關(guān)閉http通道 option httplog ###日志類別http日志格式 option forwardfor ###如果后端服務(wù)器需要獲得客戶端真實(shí)ip需要配置的參數(shù),可以從Http Header中獲得客戶端ip option redispatch ###serverId對(duì)應(yīng)的服務(wù)器掛掉后,強(qiáng)制定向到其他健康的服務(wù)器 timeout connect 10000 #default 10 second timeout if a backend is not found timeout client 300000 ###客戶端連接超時(shí) timeout server 300000 ###服務(wù)器連接超時(shí) maxconn 60000 ###最大連接數(shù) retries 3 ###3次連接失敗就認(rèn)為服務(wù)不可用,也可以通過后面設(shè)置 #################################################################### listen stats bind 0.0.0.0:1080 #監(jiān)聽端口 stats refresh 30s #統(tǒng)計(jì)頁面自動(dòng)刷新時(shí)間 stats uri /stats #統(tǒng)計(jì)頁面url stats realm Haproxy Manager #統(tǒng)計(jì)頁面密碼框上提示文本 stats auth admin:admin #統(tǒng)計(jì)頁面用戶名和密碼設(shè)置 #stats hide-version #隱藏統(tǒng)計(jì)頁面上HAProxy的版本信息 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main bind 0.0.0.0:80 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend static if url_static ###滿足策略要求,則響應(yīng)策略定義的backend頁面 default_backend dynamic ###不滿足則響應(yīng)backend的默認(rèn)頁面 #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin ###負(fù)載均衡模式輪詢 server static 127.0.0.1:80 check ###后端服務(wù)器定義 backend dynamic balance roundrobin server websrv1 10.252.97.106:80 check maxconn 2000 server websrv2 10.117.8.20:80 check maxconn 2000 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- #errorloc 503 http://www.osyunwei.com/404.html errorfile 403 /etc/haproxy/errorfiles/403.http errorfile 500 /etc/haproxy/errorfiles/500.http errorfile 502 /etc/haproxy/errorfiles/502.http errorfile 503 /etc/haproxy/errorfiles/503.http errorfile 504 /etc/haproxy/errorfiles/504.http
:wq! #保存退出 service haproxy start #啟動(dòng) service haproxy stop #關(guān)閉 service haproxy restart #重啟
5、設(shè)置HAProxy日志
vi /etc/syslog.conf #編輯,在最下邊增加 # haproxy.log local0.* /var/log/haproxy.log local3.* /var/log/haproxy.log :wq! #保存退出 vi /etc/sysconfig/syslog #編輯修改 SYSLOGD_OPTIONS="-r -m 0" #接收遠(yuǎn)程服務(wù)器日志 :wq! #保存退出 service syslog restart #重啟syslog
6.瀏覽器打開haproxy的監(jiān)控頁面
如下:http://120.55.95.103:1080/stats //說明:1080即haproxy配置文件中監(jiān)聽端口,stats 即haproxy配置文件中的監(jiān)聽名稱
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Linux運(yùn)維基礎(chǔ)httpd靜態(tài)網(wǎng)頁教程
這篇文章主要介紹了Linux運(yùn)維基礎(chǔ)中怎樣制作httpd靜態(tài)網(wǎng)頁,附含源碼及圖片示例,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝進(jìn)步2021-09-09VirtualBox安裝Centos6.8出現(xiàn)E_INVALIDARG(0x80070057)的解決方法
這篇文章主要為大家詳細(xì)介紹了VirtualBox安裝Centos6.8出現(xiàn)E_INVALIDARG(0x80070057)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07linux中串口調(diào)試工具minicom的使用詳解
minicom?是?Linux?下的一個(gè)串口終端工具,主要用于與串口設(shè)備交互,本文主要來和大家聊聊minicom?的詳細(xì)使用教程,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12阿里云OSS訪問權(quán)限配置(RAM權(quán)限控制)實(shí)現(xiàn)
這篇文章主要介紹了阿里云OSS訪問權(quán)限配置(RAM權(quán)限控制)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08linux下實(shí)時(shí)查看tomcat運(yùn)行日志的方法
下面小編就為大家?guī)硪黄猯inux下實(shí)時(shí)查看tomcat運(yùn)行日志的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起 小編過來看看吧2016-11-11