haproxy 實戰之haproxy實現httpd負載均衡

haproxy 實戰之haproxy實現httpd負載均衡

實驗目的
haproxy + httpd實現負載均衡

1.準備機器,做好時間同步,域名主機名解析

192.168.42.151 [node2 haproxy]
192.168.42.152 [node3 httpd]
192.168.42.153 [node4 httpd]

2.node3,node4上安裝httpd

node3上操作:
(1)安裝httpd

yum install httpd -y

(2)創建應用目錄

mkdir -p /application/test
chown -R apache.apache /application/test

(3)添加測試頁

echo "this is test1 page." > /application/test/index.html

(4)定義discuz虛擬主機

vim /etc/httpd/conf.d/test.conf

<VirtualHost *:80>
        ServerName www.test.com
        DocumentRoot "/application/test"
        <Directory "/application/test">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/www.test.com_access_log" combined
</VirtualHost>

(5)添加hosts解析

vim /etc/hosts
192.168.42.152  www.test.com

(6)檢查httpd語法并啟動httpd

httpd -t
systemctl start httpd

(7)測試一把

[root@node3 ~]# curl www.test.com
this is test1 page.

node4 操作同上:
需要改下測試頁的內容,和hosts解析的IP地址
(1)修改測試頁

echo "this is test2 page." > /application/test/index.html

(2)添加hosts解析

vim /etc/hosts
192.168.42.153  www.test.com

(3)測試一把

[root@node4 ~]# curl www.test.com
this is test2 page.

3.安裝haproxy
node1:
(1).haproxy的安裝

yum install haproxy -y

(2).配置haproxy
配置文件如下:

[root@node2 haproxy]# cat haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  web
    bind *:80
    default_backend             websrvs

backend websrvs
    balance     roundrobin
    server      web1 192.168.42.152:80 check
    server      web2 192.168.42.153:80 check

(3)啟動haproxy

systemctl start haproxy
[root@node2 haproxy]# ss -tnl
State      Recv-Q Send-Q      Local Address:Port     Peer Address:Port              
LISTEN     0      3000                    *:80                  *:*                  
LISTEN     0      128                     *:22                  *:*                  
LISTEN     0      100             127.0.0.1:25                  *:*                  
LISTEN     0      128                    :::22                 :::*                  
LISTEN     0      100                   ::1:25                 :::*

(4)添加域名解析

vim /etc/hosts
192.168.42.151 www.test.com

(5).測試一把

[root@node2 haproxy]# for i in {1..10};do curl www.test.com ; done;
this is test1 page.
this is test2 page.
this is test1 page.
this is test2 page.
this is test1 page.
this is test2 page.
this is test1 page.
this is test2 page.
this is test1 page.
this is test2 page.

至此我們就能看到我們訪問的能夠負載均衡了

原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/78827

(0)
sraybansrayban
上一篇 2017-06-29
下一篇 2017-06-29

相關推薦

  • NFS 實現wordpress同步

    NFS 0x00 NFS概述 0x01 NFS實現原理 0x02 NFS 相關配置及命令 0x03 NFS實踐作業(一) 實驗要求: 實驗環境: 實驗步驟: 0x04 NFS實踐作業(二) 實驗要求: 實驗環境: 實驗步驟: 0x05 常見問題 0x00 NFS概述 網絡文件系統(英語:Network File System,縮寫為NFS)是一種分布式文件系…

    2017-04-30
  • bash功能特性四 文件名通配符

    文件名通配(globbing)     通配符在bash中是一個非常有用的功能,它可以使我們更加方便的查找符合特定條件的文件。     文件通配符的包括以下幾種:          *:任意長度的任意…

    Linux干貨 2015-04-21
  • NoSQL—mongodb常見使用和入門

    NoSQL介紹: NoSQL數據管理系統是目前非常流行的一種非關系性、分布式、不支持ACID設計規范式的數據庫;NoSQL簡單的數據模型、元數據和數據分離、弱一致性、高吞吐量、高水平擴展能力和低端硬件集群使其流行的主要原因,而mongodb就是NoSQL數據庫一種非常流行的實現方式。   常見的NoSQL數據存儲模型 列式模型 文檔類型 應用場景:…

    2015-09-01
  • 第二周作業

    # 第二周作業 ##1.文件管理類命令 ###cp   復制 * 單元復制 如果目標文件不存在,會自動創建 如果已經存在,會覆蓋 * 多源復制 目標必須是目錄,分別復制每個文件至目標目錄中,并保持原名 > -i: 交互提醒 > -f: 強制覆蓋,不交互 > -r: 遞歸復制目錄 > -d: 如果復制的是符號鏈接,不找源文件,…

    Linux干貨 2016-12-09
  • Week 1–Linux基礎2

    四. Linux系統命令使用格式:  但首先,在linux中我們要知道linux系統中基本的命令格式如下: 命令字 【命令選項】 【命令參數】   Command 【option】 【arguments】 在這里主要介紹這幾個命令的使用格式(Ifconfig,echo, tty, startx, export, pwd, history, shut…

    Linux干貨 2016-12-05
欧美性久久久久