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
下一篇 2017-06-29

相关推荐

  • 第一周linux学习总结

    1、描述计算机的组成及其功能
    2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别
    3、描述Linux的哲学思想,并按照自己的理解对其进行解释性描述
    4、说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,并配合相应的示例来阐述
    5、如何在Linux系统上获取命令的帮助信息,请详细列出,并描述man文档的章节是如何划分的
    6、请罗列Linux发行版的基础目录名称命名法则及功用规定

    Linux干货 2017-12-04
  • 第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     文件管理类命令:cp、mv、rm     (1)cp:复制命令      单源复制(源文件为文本文件)    &n…

    Linux干货 2017-01-18
  • vim编辑器及练习题

    命令用法: vim [OPTION]… FILE…  +#: 打开文件后,直接让光标处于第#行的行首                   &nbs…

    Linux干货 2016-08-11
  • Linux基础之文件管理和bash特性

    部分文件管理命令,bash的一些基础特性,部分glob文件匹配示例

    Linux干货 2018-01-16
  • ip命令详解

      Linux中的ip命令功能强大,可以完成接口配置、路由管理等任务。   格式:ip [ OPTIONS ] OBJECT { COMMAND | help }   下面使用ip命令来完成一些常用的操作:     1、查看接口状态       ip link show [设备名…

    Linux干货 2016-01-14
  • iptables

    一、机制 部队大院、高档私人住宅区都是在一个被保护起来的范围内,要想进入只能从各个大门进入,在进入的时候还在门口出示证件、或者是指纹识别、或者是保安身份认证等,只有通过了这些检查才能进入到大院、住宅区内部。 Linux系统也是这么一个大院,netfilter就是这个大院的围墙,prerouting、input、output、forward、postrouti…

    Linux干货 2017-01-13