nginx基于haproxy构建负载均衡集群

准备一台haproxy(192.168.200.115),两台nginx

 

一、安装nginx(192.168.200.111/112)

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@localhost ~]# yum -y install gcc gcc-c++ make
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar -xf nginx-1.15.9.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.15.9/
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# nginx
[root@localhost ~]# netstat -lnpt | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13144/nginx: master

写一个测试文件

第一台
[root@localhost ~]# echo "aaaaa" > /usr/local/nginx/html/index.html
第二台
[root@localhost ~]# echo "bbbbbb" > /usr/local/nginx/html/index.html 

 

二、安装配置Haproxy(192.168.200.115)

1、安装Haproxy依赖包及源码编译安装

1 [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel [root@localhost ~]# tar -xf haproxy-1.4.24.tar.gz -C /usr/src/
2 [root@localhost ~]# cd /usr/src/haproxy-1.4.24/
3 [root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install

2、建立haproxy的配置目录及文件

[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy

3、haproxy 配置文件修改

8 [root@localhost ~]# vim /etc/haproxy/haproxy.cfg
 9 # this config needs haproxy-1.1.28 or haproxy-1.2.1
10 
11 global
12 #   log 127.0.0.1   local0
13 #   log 127.0.0.1   local1 notice
14     log /dev/log    local0 info
15     log /dev/log    local0 notice
16     maxconn 4096
17     uid 99
18     gid 99
19     daemon
20 
21 defaults
22     log global
23     mode    http
24     option  httplog
25     retries 3
26     maxconn 4096
27     contimeout  5000   //连接超时时间
28     clitimeout  50000  //客户端超时时间
29     srvtimeout  50000  //服务器超时时间
30 
31 listen  webcluster 0.0.0.0:80
32     option  httpchk GET /index.html
33     balance roundrobin
34     server  inst1 192.168.200.111:80 check inter 2000 fall 3
35     server  inst1 192.168.200.112:80 check inter 2000 fall 3
36 
37 listen admin_stats
38     bind 0.0.0.0:8000
39     mode http
40     option httplog
41     maxconn 100
42     stats refresh 1s
43     stats uri /stats
44     stats realm Crushlinux\ Haproxy
45         stats auth admin:admin
46     stats hide-version
47 

4、启动服务自启动脚本

[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy 
[root@localhost ~]# /etc/init.d/haproxy start

 

三、测试

[root@localhost ~]# while :
> do
> curl 192.168.200.115
> sleep 1
> done
aaaaa
bbbbbb
aaaaa
bbbbbb
aaaaa
bbbbbb
。。。。。。。。。。

 

posted @ 2019-10-08 17:13  三岁半的胖啊  阅读(303)  评论(0编辑  收藏  举报