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

相关推荐

  • 磁盘分区

    磁盘分区 一、分区方式两种 MBR分区不能超过2个T按柱面分区主引导记录存放在,0磁道0扇区 512bytes前446字节存放bootloader (软件程序)64字节:分区表16个字节表示一个有效的分区 故只能分4个主分区。大于4个需要创建扩展分区,并在其中创建逻辑分区。2字节 55AABPT分区GPT:GUID(Globals Unique Identi…

    Linux干货 2016-08-29
  • TCP/IP五层模型

    1、简述osi七层模型和TCP/IP五层模型 网络的七层模型是由OSI和CITT一起制定的开放系统互连参考模型: TCP/IP 五层模型可以分为:应用层,传输层,网络层,数据链路层,物理层。 2、简述ip route家族命令 语法:ip [ OPTIONS ] OBJECT { COMMAND | help } OBJECT={link ,addr,rout…

    2018-01-05
  • 九.Linux博客-2016年8月10日脚本、sed、vim

    格式说明: 操作 概念 命令 说明及举例 九 脚本sed、vim sed 文本编辑工具 行编辑器,每次取出一行在内存里处理,处理完成后屏幕打印。完成后再取出一行放到内存里处理,覆盖原来内存中的行,循环。。 sed -n '3d' f1 把文件f1中的第三行删掉并不在屏幕上显示 sed -n…

    Linux干货 2016-08-24
  • SQL优化大全

    1. 优化SQL步骤 1. 通过 show status和应用特点了解各种 SQL的执行频率        通过 SHOW STATUS 可以提供服务器状态信息,也可以使用 mysqladmin extende d-status 命令获得。 SHOW STATUS 可以根据需要显示 session 级别的统计结果和 g…

    Linux干货 2015-04-13
  • Linux基础知识(五)

    1、显示当前系统上root、fedora或user1用户的默认shell [root@server01 ~]# cat /etc/passwd | grep –color=auto -E "^root|fedora|user1" | cut -d : -f 1,7 2、找出/etc/rc.d/init.d/functions文…

    Linux干货 2016-10-25
  • 马哥教育网络班20期第2周课程练习

    答: 1、 cp,mv,rm,cat(tac,more,less,tail),touch 其中常用的有: cp(复制):                   -i:交互式              -…

    Linux干货 2016-06-23