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的发展史

    一台真正意义上的计算机都是由硬件与软件组成的。而根据冯诺依曼结构计算机由控制器、运算器、存储器、输入设备、输出设备五大部分组成。 硬件 控制器:(Controler) 控制程序的执行 运算器:(ALU,Arithmetic Logic Unit) 完成数据的加工处理 储存器:(Menory) 记忆程序和数据&…

    Linux干货 2016-10-26
  • 帮助和文件管理信息

    history命令: -c 清空命令历史 -d offset 删除历史中指定的第offset个命令 n 显示最近的n条历史 -a 追加本次会话新执行的命令历史列表至历史文件 -n 读历史文件中未读过的行到历史列表 -r 读历史文件附加到历史列表 -w 保存历史列表到指定的历史文件 -p 展开历史参数成多行,但不存在历史列表中 -s 展开历史参数马一行。附加在…

    Linux干货 2017-04-11
  • Linux进程管理-初级

    Linux进程管理-初级 背景: 在学习完Linux进程管理后,发现这一块的知识点比较多,很多都是自己以前没有接触过的,而且这部分知识对今后的工作有很大帮助,在这里做个学习的总结,供以后复习。 进程介绍: 什么是进程 进程(Process):运行中的程序的一个副本,是被载入内存的一个指令集合,进程有进程ID(Process ID,PID),用来标记每个进程,…

    2017-08-26
  • Linux用户和组的主要配置文件及其相关命令

    Linux用户和组的主要配置文件: /etc/passwd:用户及其属性信息 /etc/shadow用户密码及其相关属性 /etc/group组及其属性信息 etc/gshadow组密码及其相关属性 /etc/passwd 1.用户名: 2.密码位:x pwconv (默认) 将密码映射到了/etc/shadow pwunconv 将密码保存到/etc/pa…

    2017-07-22
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 答:who | cut -f 1 -d " " | sort -u 2、取出最后登录到当前系统的用户的相关信息。 答:who | tail -n 1 3、取出当前系统上被用户当作其默认shell的最多的那个shell。 答:cut -f7 -d: …

    Linux干货 2016-11-25
  • 第十九周作业

    1.描述tomcat的架构 tomcat服务器是一种Servlet/jsp容器,更实质性的说是Servlet容器,因为jsp最终还是被编译成servlet来执行的。而对于servlet来说,其最长见的用途是扩展java web服务器功能,为来自web客户的请求提供服务。它完全运行在java虚拟机上。由于它的运行在服务器端,因此他的运行不依赖于浏览器。 tom…

    2017-07-11