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

相关推荐

  • 第二周部分练习

    (1)用户管理: 1.创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为”Gentoo Distribution” 使用id命令查看用户信息 使用cat命令查看配置文件/etc/passwd 2.创建下面的用户、组和组成员关系 名字为admins的组 用户natasha,使用admins作为附…

    2017-07-23
  • 引导加载程序grub的使用详解

    引导加载程序grub的使用详解 为运行于虚拟机上的CentOS 6添加一个新硬件,提供两个主分区 为硬盘建两个主分区;并为其安装grub 为硬盘的第一个主分区提供内核和ramdisk文件;为第二个分区提供rootfs 为rootfs提供bash、ls、cat程序以及所依赖的库文件 为grub提供配置文件 将新的硬盘设置为第一启动项并能够正常启动目标主机 1.…

    Linux干货 2017-09-04
  • Linux shell脚本编程练习题

    《书籍上面的练习题》 问题:谁在霸占磁盘资源?     如果您负责的Linux服务器上有许多用户,则经常需要解决的一个问题就是谁在使用所有磁盘空间。这是个老掉牙的问题有时比其他问题更难以弄清。     不幸的是,虽然跟踪用户磁盘空间使用情况非常重要,但却没有一个Linux命令可以提供此信息。因此需要…

    Linux干货 2016-06-09
  • bash的命令执行状态返回值及命令行展开详解

    命令执行状态返回值及命令行展开是bash的2个基本特性,其详细特点及应用如下:   一、命令执行的状态结果:bash通过状态返回值来输出此结果                 成功:0             …

    Linux干货 2016-11-06
  • 查找 -数据结构

    几种查找算法:顺序查找,折半查找,分块查找,散列表 一、顺序查找的基本思想:  从表的一端开始,向另一端逐个按给定值kx 与关键码进行比较,若找到,查找成功,并给出数据元素在表中的位置;若整个表检测完,仍未找到与kx 相同的关键码,则查找失败,给出失败信息。 说白了就是,从头到尾,一个一个地比,找着相同的就成功,找不到就失败。很明显的缺点就是查找效…

    Linux干货 2015-07-28
  • 【linux】正则表达式之grep、egrep、元字符

    正则表达式:     又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。是一类字符所书写的模式,其中许多字符(元字符)不表示其字面意义,而是表达控制或通配等功能。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符…

    Linux干货 2015-04-01