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.178linux.com/78827

(0)
sraybansrayban
上一篇 2017-06-29 12:03
下一篇 2017-06-29 20:04

相关推荐

  • rpm及yum

    库文件 查看二进制程序所依赖的库文件: ldd /PATH/TO/BINARY_FILE [root@localhost ~]# ldd /bin/bash 管理及查看本机装载的库文件: ldconfig /sbin/ldconfig -p:  显示本机已经缓存的所有可用库文件 名及文件路径映射关系 配置文件:/etc/ld.so.conf, /e…

    Linux干货 2016-08-29
  • Linux网络管理之网络配置管理和子网划分

    配置网卡信息 获取网卡的信息 dmesg | grep -i eth 获取网卡芯片信息 lspci | grep -i eth 查询IP信息 ifconfig显示IP信息    -a:显示包括未激活状态的网卡信息 重启网卡服务(每次配置完ip需要手动重启此服务) CentOS6: servcie network restart CentO…

    Linux干货 2016-09-07
  • Linux RPM 命令参数使用详解

    rpm 执行安装包: 二进制包(Binary)以及源代码包(Source)两种。二进制包可以直接安装在计算机中,而源代码包将会由 RPM自动编译、安装。源代码包经常以src.rpm作为后缀名。 -ivh # 安装显示安装进度–install–verbose–hash。 使用方法如下:   &n…

    2017-04-18
  • HipHop PHP实战(详解web运行模式)

    Note: These code examples assume the HipHop compiler is fully built. 1 . Setting Up Your Environment (构建环境) To get started, you need to configure two environment variables. cd…

    Linux干货 2015-04-10
  • 马哥教育网络班21期+第10周课程练习

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)     1)开机后会进行POST(开机加电自检),加载BIOS,之后会根据BIOS上的boot  sequence(引导加载次序)找到第一个有引导程序的设备,找到MBR,bootloader(引导加载器)就安装在MBR内。M…

    Linux干货 2016-09-19
  • Linux系统终端类型

    引言 终端(Terminal)也称终端设备,是计算机网络中处于网络最外围的设备,主要用于用户信息的输入以及处理结果的输出等。在早期计算机系统中,由于计算机主机昂贵,因此一个主机一般会配置多个终端,这些终端本身不具备计算能力,仅仅承担信息输入输出的工作,运算和处理均由主机来完成,在个人计算机时代,个人计算机可以运行称为终端仿真器的程序来模仿一个终端的工作。 &…

    Linux干货 2016-10-19