Haproxy負載均衡羣集配置

軟件包,Nginx和haproxy需要的自取

鏈接:https://pan.baidu.com/s/19oS_In0GvbqQzJU5VfPupw
提取碼:f2ed
複製這段內容後打開百度網盤手機App,操作更方便哦

一、概述

  • Haproxy是一款可提供高可用性、負載均衡、及基於TCP和HTTP應用的代理軟件
  • 適合於負載特別大的Web站點
  • 運行在當前的硬件上可支持數以萬計的併發連接請求

二、實驗目的

當高併發請求來臨時,haproxy服務器能夠調度後面的Web節點服務器以輪詢的方式處理請求

三、實驗拓撲

在這裏插入圖片描述

四、實驗環境

在這裏插入圖片描述

五、實驗配置

5.1、haproxy服務器配置

  • 上傳haproxy源碼文件到/opt目錄下,編譯安裝haproxy
[root@haproxy ~]# yum install bzip2-devel pcre-devel gcc gcc-c++ make -y
[root@haproxy ~]# cd /opt
[root@haproxy opt]# ls
haproxy-1.5.19.tar.gz   rh     ###已經上傳
[root@haproxy opt]# tar zxvf haproxy-1.5.19.tar.gz -C /opt  ##解壓
[root@haproxy opt]# cd /opt/haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# uname -a     ###查看當前系統信息
Linux haproxy 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@haproxy haproxy-1.5.19]# make TARGET=linux3100 ###執行編譯
[root@haproxy haproxy-1.5.19]# make install  ##安裝

  • 修改haproxy.cfg配置文件
[root@haproxy haproxy-1.5.19]# vi /opt/haproxy-1.5.19/examples/haproxy.cfg

在這裏插入圖片描述

  • listen字段全部刪除,新插入以下字段

在這裏插入圖片描述

  • 編輯啓動腳本
##將源碼目錄下的examples/haproxy.init複製到/etc/init.d/[root@haproxy ~]# cp /opt/haproxy-1.5.19/examples/haproxy.init  /etc/init.d/haproxy
###添加執行權限
[root@haproxy ~]# chmod +x /etc/init.d/haproxy
###添加service服務
[root@haproxy ~]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
###關閉防火牆
[root@haproxy ~]# systemctl stop firewalld 
[root@haproxy ~]# setenforce 0
###啓動服務
[root@haproxy ~]# service haproxy start
##查看服務端口
[root@haproxy ~]# netstat -antp|grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7134/haproxy  

5.2、nginx服務器1配置

  • 安裝環境依賴包
[root@nginx1 ~]# yum install -y \
pcre-devel \
gcc \
gcc-c++ \
make
[root@nginx1 ~]# yum install pcre-devel zlib-devel  make -y 
  • 創建程序性用戶

[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx
  • 上傳nginx源碼包,解壓到opt目錄
[root@nginx1 ~]# tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt
  • 編譯安裝
[root@nginx1 ~]# cd /opt/nginx-1.12.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
make && make install
  • 編輯web首頁信息
[root@nginx1 ~]# cd /usr/local/nginx/html/
[root@nginx1 ~]# echo "this is 1111 web" >test.html
  • 創建軟連接方便管理
[root@nginx1 ~]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
  • 關閉防火牆,啓動服務
[root@nginx1 ~]# systemctl stop firewalld 
[root@nginx1 ~]# setenforce 0
[root@nginx1 ~]# nginx
[root@nginx1 ~]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15677/nginx: master 

5.3、nginx服務器2配置

  • nginx2配置與nginx1相同,只需在站點目錄中插入不同內容加以區分即可
[root@nginx1 ~]# cd /usr/local/nginx/html/
[root@nginx1 ~]# echo "this is 2222 web" >test.html
  • 關閉防火牆,啓動服務
[root@nginx2 ~]# systemctl stop firewalld 
[root@nginx2 ~]# setenforce 0
[root@nginx2 ~]# nginx
[root@nginx2 ~]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15677/nginx: master 

六、驗證

  • 開啓客戶機192.168.100.40/24模擬請求,直接訪問haproxy服務器(不斷刷新web界面)。

在這裏插入圖片描述
在這裏插入圖片描述

  • 此時haproxy服務器會以輪詢的方式調度後面的nginx服務器處理請求。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章