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

相关推荐

  • 20160804正则表达式作业

    正则表达式表示18位身份证号 正则表达式表示手机号 正则表达式表示邮箱 Ø  截取出ifconfig命令结果中所有的本机的ipv4地址   Ø  查看分区中使用率最大的百分比 Ø  查看UID最大的用户名、UID以及shell类型 Ø  查看/tmp权限,并且以数字方式显示 思路:先找到如何才能查看到/tmp…

    Linux干货 2016-08-08
  • linux计划任务

    任务计划的分类: 一次性的任务计划:只执行一次 周期性的任务计划:每隔一定的周期去做相同的事情 设置任务计划功能的工具:at和crontab at:专门处理一次性的计划任务 crontab:根据定义的周期信息,循环做一些事情 batch:系统自行选择空闲时间去执行此处指定的任务 At命令: 查看atd状态和启动atd服务及其配置文件路径: centos6:s…

    2017-09-09
  • 学习宣言

    新的一天开始了, 从今天起,正式开始Linux的系统学习, 对于基础薄弱的我来说,是一个新的挑战,而我接受这个挑战。 在今后的日子里,一定会拼搏奋进,更上一层楼。 积土而为山,积水而为海, 定会一天比一天强,努力吧。

    Linux干货 2016-10-24
  • N25-第4周作业(用户权限,grep用法)

    复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有访问权限.      [root@bogon ~]# chmod -R o=—,g=— /home/tuser1/ [root@bogon ~]# chmod -R o=,g= /home/tuser1/ 编辑/etc/…

    Linux干货 2016-12-26
  • Linux管理目录和文件命令

     管理目录命令: pwd:显示当前目录 Cd:   改变当前目录 ls:    查看目录下的内容 rmdir: 删除目录 管理文件命令: cat: 查看文件内容 cp: 复制文件内容 grep:在文件中查找某字符 touch:创建文件 rm&nbsp…

    2016-03-05
  • 第三周:文本处理工具wc,cut,sort,uniq,tr,tee命令练习和用户及组相关命令练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可; [root@app1 tmp]# who | cut -d" " -f1| uniq root dts centos 2、取出最后登录到当前系统的用户的相关信息; [r…

    Linux干货 2016-09-27