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

相关推荐

  • 文本编辑三剑客之gawk

     awk 本章内容: awk介绍 awk基本用法 awk变量 awk格式化 awk操作符 awk条件判断 awk循环 awk数组 awk函数 调用系统命令 1.awk介绍 ★ Linux 文本处理工具三剑客:grep、sed、awk。其中grep是一种文本过滤工具;sed是文本行编辑器,而awk是一种报表生成器,就是对文件进行格式化处理的…

    Linux干货 2016-09-24
  • Web服务,http协议。

    一·web   1。web是一种网站服务器,C/S架构,比较流行的Apache,Nginx,IIS。客户能够去服务器请求资        资源。   2.使用http协议,服务器html或者xml格式文件,客户浏览器访问资源(url)。   3.过程包括服务器套接字监听直到和客户套接字建立…

    Linux干货 2015-07-04
  • IP地址之IPv6

    一、IPv6概述   目前使用的IP地址都是IPv4版本,之所以开发出IPv6版本主要是基于以下原因:     1、IPv4地址空间不足,分配不合理;     2、IPv4头部太过于复杂;     3、使用IPv4使得路由器及主机配置复杂;     4、对于IPv4重新…

    Linux干货 2016-01-13
  • 利用heartbeat构建高可用http

    一、实验准备:  1)实验环境:          2)同步时间;确保可以使用主机名通信;确保可以使用ssh秘钥方式进行彼此登录;由于是两台设备,需要仲裁;  确保可以使用主机名通信 web1修改hosts文件如下: 172.16.2.12 web1.linux.com…

    Linux干货 2015-07-08
  • 常用RAID级别介绍

    RAID是什么         磁盘阵列(Redundant Arrays of Independent Disks,RAID),磁盘阵列是将多个价格便宜的磁盘按照一定的组合方式组成具有高容量的磁盘组,按照不同的组合方式可以达到不同的效果,如:可以提升磁盘的存取效率,可提高磁盘的…

    Linux干货 2016-02-14
  • CentOS7的虚拟机安装

    刚学习linux下CentOS7的操作,熟悉一下CentOS7的安装,所以写一下CentOS7的安装教程,如果那里有写的不对的话希望得到指正。 我是在VMware下装的CentOS7 第一步配置硬件 先创建一个虚拟机 这里我选择的典型模式,新手嘛,新手难度的创建就好,高手难度的等新手难度熟练了再去碰好了,当然这是我的建议… 下一步然后光盘选择稍后…

    2017-07-15