https://pan.baidu.com/s/1Sq2aJ35zrW2Xn7Th9j7oOA //軟件百度網盤連接
在80.100虛擬機上
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
yum install lrz* -y //安裝上傳軟件
tar xf haproxy-1.5.15.tar.gz -C /opt/ //解壓壓縮包到/opt/
cd /opt/haproxy-1.5.15/ //進入/opt/haproxy-1.5.15/
yum install -y \pcre-devel \bzip2-devel \gcc \gcc-c++ \make
uname -r
make TARGET=linux26 PREFIX=/usr/local/haproxy //標識64為系統
make install PREFIX=/usr/local/haproxy
mkdir /etc/haproxy //創建目錄
useradd -s /sbin/nologin -M haproxy //創建用戶
id haproxy //查看用戶信息
cd examples/ //進入examples/
cp haproxy.cfg /etc/haproxy/ //拷貝到/etc/haproxy/
cd /etc/haproxy/ //進入cd /etc/haproxy/
vi haproxy.cfg //進入vi haproxy.cfg文件
把里面全刪了添加#this config needs haproxy-1.1.28 or haproxy-1.2.1globallog 127.0.0.1 local0 log 127.0.0.1 local1 notice#log loghost local0 info //定義haproxy 日志級別maxconn 4096 //可以接收的最大并發連接數#chroot /usr/share/haproxy //工作目錄uid 1001 //運行的程序用戶 gid 1001 //運行的程序用戶組daemon //創建1個進程進入deamon模式運行,以后臺形式運行harpoxy#debug#quietdefaultslog global //定義日志,采用全局定義mode http //所處理的類別,tcp是四層,http是七層,health只會返回OK,若是混合模式則mode不需要設置option httplog //每次請求完畢后主動關閉http通道option dontlognull //不記錄健康檢查的日志信息retries 3redispatchmaxconn 2000contimeout 5000clitimeout 50000srvtimeout 50000listen admin_stats //為haproxy訪問狀態監控頁面配置,取名為admin_statsbind 0.0.0.0:8089 //監聽端口stats enable //啟用監聽端口mode http //http的7層模式log global // 繼承global中log的定義stats uri /stats //監控頁面的url訪問路徑,即http://ip/stats訪問監控頁面stats realm Haproxy\ Statistics //監控頁面的密碼框提示信息stats auth admin:admin //監控頁面的用戶和密碼admin,可以設置多個用戶名#stats hide-version //隱藏統計頁面上HAProxy的版本信息stats admin if TRUE //當通過認證才可管理stats refresh 30s //頁面自動刷新時間30s:wq //保存退出
cd -
cp haproxy.init /etc/init.d/haproxy //拷貝到/etc/init.d/haproxy
ll /etc/init.d/haproxy //查看這個文件有哪些權限
chmod +x /etc/init.d/haproxy //給這個文件x權限
ll /etc/init.d/haproxy //查看這個文件有哪些權限
chkconfig --add haproxy
在瀏覽器地址欄192.168.80.100:8089/stats
vi /etc/haproxy/haproxy.cfg#最后一行添加listen webcluster //定義webcluster服務器組。bind 0.0.0.0:80 //定義haproxy前端部分監聽的端口。mode http //http的7層模式option httpchk GET /index.html //心跳檢測log global //繼承global中log的定義maxconn 3000 //server進程可接受的最大并發連接數balance roundrobin //負載均衡的方式:輪詢server web01 192.168.80.102:80 check inter 2000 fall 5server web02 192.168.80.103:80 check inter 2000 fall 5//后端服務器 web1 和 web2 ,IP 地址分別為 192.168.80.102 和 192.168.80.103
check:對當前server做健康狀態檢測

在瀏覽器地址欄192.168.80.100:8089/stats
在80.102虛擬機上
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
yum install httpd -y //安裝httpd服務
vi /etc/httpd/conf/httpd.conf //進入這個配置文件
找到ServerName www.example.com:80把#去掉
:wq //保存退出
cd /var/www/html/ //進入這個目錄
echo "<h1>server aa</h1>" > index.html //在當前目錄中創建index.html在里面添加server aa
systemctl start httpd //重啟服務
在瀏覽器地址欄192.168.80.102
在80.103虛擬機上
systemctl stop firewalld //關閉防火墻
setenforce 0 //關閉監控
yum install httpd -y //安裝httpd服務
vi /etc/httpd/conf/httpd.conf //進入這個配置文件
找到ServerName www.example.com:80把#去掉
:wq //保存退出
cd /var/www/html/ //進入這個目錄
echo "<h1>server bb</h1>" > index.html //在當前目錄中創建index.html在里面添加server bb
systemctl start httpd //重啟服務
在瀏覽器地址欄192.168.80.103
轉載于:https://blog.51cto.com/14158288/2339647