您好,登錄后才能下訂單哦!
haproxy配置文件詳解參考:haproxy
環(huán)境如下:
一、準備:
1、下載haproxy 軟件包,haproxy???提取碼: 9it6?
2、web 網(wǎng)站可以使用Apache、Nginx、搭建都可以,這里為了方便我就直接使用系統(tǒng)盤帶的httpd服務(wù)了。
web網(wǎng)站的搭建可參考:基于 Linux 安裝 web 服務(wù)及基本配置;基于 Centos 7 搭建Nginx;
3、配置防火墻放行流量
4、我這里使用的全部是centos 7系統(tǒng),注意,該環(huán)境不是生產(chǎn)環(huán)境,若是在生產(chǎn)環(huán)境中,肯定還有后端存儲來存放網(wǎng)頁文件,web服務(wù)器讀取存儲服務(wù)器上的網(wǎng)頁返回給客戶端。這樣才可保證網(wǎng)頁內(nèi)容的一致性。
二、開始搭建:
1、配置主服務(wù)器:
[root@haproxy1?/]#?yum?-y?install?pcre-devel?bzip2-devel?keepalived??????????#?安裝相關(guān)的依賴包和軟件包 [root@haproxy1?media]#?tar?zxf?haproxy-1.5.19.tar.gz?-C?/usr/src/ [root@haproxy1?media]#?cd?/usr/src/haproxy-1.5.19/ [root@haproxy1?haproxy-1.5.19]#?make?TARHET=linux26??&&??make??install?????????#?編譯安裝,TARGET表示64位操作系統(tǒng) [root@haproxy1?haproxy-1.5.19]#?mkdir?/etc/haproxy????????????????????#?創(chuàng)建用來存放主配文件的目錄 [root@haproxy1?haproxy-1.5.19]#?cp?examples/haproxy.cfg?/etc/haproxy/?????????????#?將編譯包中的主配文件復制到配置文件目錄,注意啊:這個配置文件是在編譯包中,注意看路徑 [root@haproxy1?/]#?vim?/etc/haproxy/haproxy.cfg? #?this?config?needs?haproxy-1.1.28?or?haproxy-1.2.1 global ????????log?/dev/log????local0?info ????????log?/dev/log????local0?notice ????????#log?loghost????local0?info ????????maxconn?4096 ????????#chroot?/usr/share/haproxy????????????????#?將此行注釋掉 ????????uid?99 ????????gid?99 ????????daemon ????????#debug ????????#quiet defaults ????????log?????global ????????mode????http ????????option??httplog ????????option??dontlognull ????????retries?3 ????????redispatch ????????maxconn?2000 ????????contimeout??????5000 ????????clitimeout??????50000 ????????srvtimeout??????50000 listen??webcluster??0.0.0.0:80?????????????????????????????#?將端口號修改為80,webcluster為群集名稱,可修改 ????????option??httpchk?GET??/index.html ????????balance?roundrobin????????????????????????????#?表示采用輪詢算法 ????????server??web1?192.168.1.20:80?check?inter?2000?fall?3??????????????????#?兩個web節(jié)點 ????????server??web2?192.168.1.30:80?check?inter?2000?fall?3 [root@haproxy1?/]#?cp?/usr/src/haproxy-1.5.19/examples/haproxy.init?/etc/init.d/haproxy?????????????#?復制服務(wù)控制腳本 [root@haproxy1?/]#?ln?-s?/usr/local/sbin/haproxy?/usr/sbin/haproxy??????????????????#?創(chuàng)建鏈接使命令使用更方便 [root@haproxy1?/]#?chmod?+x?/etc/init.d/haproxy??????????????????????#?添加執(zhí)行權(quán)限 [root@haproxy1?/]#?chkconfig?--add?/etc/init.d/haproxy????????????????#?添加為系統(tǒng)服務(wù) [root@haproxy1?/]#?/etc/init.d/haproxy?start?????????????????#?啟動 Starting?haproxy?(via?systemctl):??????????????????????????[??OK??] [root@haproxy1?/]#?vim?/etc/rsyslog.d/haproxy.conf??????????????#?配置日志文件,寫入如下內(nèi)容 if?($programname?==?'haproxy'?and?$syslogseverity-text?==?'info')?then?-/var/log/haproxy/haproxy-info.log &?~? if?($programname?==?'haproxy'?and?$syslogseverity-text?==?'notice')?then?-/var/log/haproxy/haproxy-notice.log &?~? [root@haproxy1?/]#?systemctl?restart?rsyslog.service?????????????????#?重啟日志服務(wù)
配置 keepalived :
[root@haproxy1?/]#?vim?/etc/keepalived/keepalived.conf? !?Configuration?File?for?keepalived global_defs?{ ???notification_email?{ ?????acassen@firewall.loc ?????failover@firewall.loc ?????sysadmin@firewall.loc ???} ???notification_email_from?Alexandre.Cassen@firewall.loc ???smtp_server?192.168.200.1 ???smtp_connect_timeout?30 ???router_id?LVS1??????????#?主從調(diào)度器名稱區(qū)分開 } vrrp_instance?VI_1?{ ????state?MASTER ????interface?ens33??????????????#?修改網(wǎng)卡名稱 ????virtual_router_id?51 ????priority?100 ????advert_int?1 ????authentication?{ ????????auth_type?PASS ????????auth_pass?1111 ????} ????virtual_ipaddress?{? ????????192.168.1.100??????????#?填寫漂移地址 ????} } [root@haproxy1?/]#?systemctl?restart?keepalived?????????????#?重啟服務(wù)使配置生效
2、配置從服務(wù)器:
[root@haproxy2?/]#??yum?-y?install?pcre-devel?bzip2-devel??keepalived [root@haproxy2?media]#?tar?zxf?haproxy-1.5.19.tar.gz?-C?/usr/src/ [root@haproxy2?media]#?cd?/usr/src/haproxy-1.5.19/ [root@haproxy2?haproxy-1.5.19]#?make?TARGET=linux26?&&?make?install [root@haproxy2?haproxy-1.5.19]#?mkdir?/etc/haproxy [root@haproxy2?haproxy-1.5.19]#?scp?root@192.168.1.10:/etc/haproxy/haproxy.cfg?/etc/haproxy/??????????#?圖個方便直接復制 root@192.168.1.10's?password:? haproxy.cfg??????????????????????????????????????????100%??570?????0.6KB/s???00:00???? [root@haproxy2?/]#?cp?/usr/src/haproxy-1.5.19/examples/haproxy.init?/etc/init.d/haproxy???????????#?復制服務(wù)控制腳本 [root@haproxy2?/]#?ln?-s?/usr/local/sbin/haproxy?/usr/sbin/haproxy [root@haproxy2?/]#?chmod?+x?/etc/init.d/haproxy? [root@haproxy2?/]#?chkconfig?--add?/etc/init.d/haproxy? [root@haproxy2?/]#?/etc/init.d/haproxy?start? Starting?haproxy?(via?systemctl):??????????????????????????[??OK??] [root@haproxy2?/]#?scp?root@192.168.1.10:/etc/rsyslog.d/haproxy.conf?/etc/rsyslog.d/ root@192.168.1.10's?password:? haproxy.conf?????????????????????????????????????????100%??226?????0.2KB/s???00:00??? [root@haproxy2?/]#?systemctl?restart?rsyslog.service?????????????????#?重啟服務(wù)使配置生效 [root@haproxy2?haproxy-1.5.19]#?scp?root@192.168.1.10:/etc/keepalived/keepalived.conf?/etc/keepalived/ root@192.168.1.10's?password:? keepalived.conf??????????????????????????????????????100%?3511?????3.4KB/s???00:00???? [root@haproxy2?/]#?vim?/etc/keepalived/keepalived.conf????????????#?修改主配置文件,修改如下幾個部分 .......................?省略部分 ???router_id?LVS2???????????????#?主從調(diào)度器區(qū)分ID } vrrp_instance?VI_1?{ ????state?BACKUP???????????????????????#?狀態(tài)修改為?BACKUP ????interface?ens33????????????????? ????virtual_router_id?51 ????priority?90???????????????????????????#?優(yōu)先級調(diào)低 ????advert_int?1 ????authentication?{ ????????auth_type?PASS ????????auth_pass?1111 ????} ????virtual_ipaddress?{ ????????192.168.1.100????????????????#?漂移地址 ????} } [root@haproxy2?/]#?systemctl?restart?keepalived
兩個web節(jié)點配置(兩個配置相同):
[root@web2 /]# yum -y install httpd
[root@web2 /]# echo server2.com > /var/www/html/index.html? ? ? ? ? # 創(chuàng)建測試網(wǎng)頁
[root@web2 /]# systemctl start httpd?
[root@web2 /]# systemctl enable httpd
當然,在實際生產(chǎn)環(huán)境中網(wǎng)頁是一樣的,這里我為了驗證出效果,所以做了兩個不同的測試文件。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。