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

相关推荐

  • MySQL 多实例详解

    目录 一、基本概念 1、MySQL多实例        就是在一台机器上面开启多个不同的端口,运行多个MySQL服务进程。这些MySQL多实例公用一套安装程序,使用不同的(也可以是相同的)配置文件,启动程序,数据文件。在提供服务时候,多实例MySQL在逻辑上看来是各自独立的,多个实例的自身是根据配置…

    Linux干货 2015-09-23
  • N26_第四周

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@node1 skel]# cp -r /etc/skel/home/tuser1 [root@node1 skel]# chmod go-wrx/home/tuser1/   2、编辑/etc/grou…

    Linux干货 2017-05-26
  • Linux磁盘管理(分区,格式化,文件系统、VFS、mount、fstab)

    Linux系统中数据存储在磁盘中,磁盘在冯诺依曼体系中属于输入输出外部存储对象,保存着重要的企业数据,对Linux系统中的磁盘管理尤其需要熟练掌握,由于企业目前使用的主流存储设备还是硬盘,所以这里只叙述硬盘的组织结构; 1.分区:    (1)低级格式化:       硬盘在出厂的时候厂家会做一次分区格式化,…

    Linux干货 2016-09-02
  • 文本处理工具

    1, head  默认显示前十行 -n  +行号 显示前n行 -行号 显示前n行 -c 字节数 显示前n个字节 tail 默认显示后十行 -n +行号 显示后n行 – 行号 显示后n行 -f 动态显示 cut -d 指定分隔符 -f 选取第几列 –output-delimiter 指定输出符 相关的实际操作: a,…

    2017-07-29
  • find命令归纳

    首先,find可以实现以下方式查找 文件名:-name -iname -regex 文件类型:-type TYPE:(f,d,l,s,b,c,p) 文件大小:-size [+|-]#UNIT #UNIT(k,M,G)范围(由小到大):[0,#-1](#-1,#](+#,oo) 时间戳:-{a|m|c}time -{a|m|c}min  范围(现在到…

    Linux干货 2016-04-19
  • 最近了解到的命令

    ssh:secure  shell shell  人机交互的语言查询当前系统使用了哪个shell(接口程序) echo $SHELL 查询当前系统已安装的所有shell cat /etc/shells Shell提示符 whoami 显示当前的登录用户名 hostname 显示当前的主机名      pwd …

    Linux干货 2017-07-13