Centos7中搭建haproxy实现代理服务

haproxy提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理,支持虚拟主机;
haproxy特别适用于那些负载特别大的web站点,这些站点通常又需要会话保持或七层处理。haproxy运行在时下的硬件上,完全可以支持数以万计的并发连接,提供更加稳定高效的代理服务。

实验环境:haproxy代理服务器IP 192.168.60.136
                节点服务器1IP 192.168.60.135
                节点服务器2IP  192.168.60.143

Centos7中搭建haproxy实现代理服务

手工编译nginx的过程不在赘述,请看我另一篇博客详解
手工编译nginx博客地址:http://blog.51cto.com/13760226/2158459
haproxy https://pan.baidu.com/s/1o6266kAIwcaVxjKdCNZNQw 密码:vxxz
nginx https://pan.baidu.com/s/13G9Mc3uX5dgYMYh4RflcWQ 密码:vtkv

下面我们开始搭建haproxy
1、关闭防火墙,安装编译环境

Centos7中搭建haproxy实现代理服务
2、挂载宿主机,进行解压
Centos7中搭建haproxy实现代理服务
Centos7中搭建haproxy实现代理服务

3、cd /opt/haproxy-1.5.19
    make TARGET=linux26

4、安装

Centos7中搭建haproxy实现代理服务

5、创建目录,复制配置文件

Centos7中搭建haproxy实现代理服务

6、对配置文件进行修改

vim haproxy.cfg /修改配置文件
chroot /usr/share/haproxy /删除,不禁锢家目录
redispatch /删除再发送功能
Centos7中搭建haproxy实现代理服务

7、复制启动脚本以及haproxy命令脚本并启动

Centos7中搭建haproxy实现代理服务

8、nginx站点添加首页内容

Centos7中搭建haproxy实现代理服务
Centos7中搭建haproxy实现代理服务

9、测试

Centos7中搭建haproxy实现代理服务

end:代码随笔记

1、代理服务器地址 192.168.60.135
节点服务器地址 192.168.60.136
节点服务器地址2 192.168.60.143
节点服务器使用nginx
2、代理服务器使用haporxy实现代理功能
=========================节点服务器======================

扫描二维码关注公众号,回复: 2892575 查看本文章

systemctl stop firewalld.service

systemctl disable firewalld.service

setenforce 0

yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y

useradd -M -s /sbin/nologin nginx

mkdir /opt/abc

mount.cifs //192.168.10.3/linuxbage /opt/abc

cd /opt/abc/rhel7/Y2C7

tar zxf nginx-1.12.0.tar -C /opt

cd /opt/nginx-1.12.0.

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

vim /etc/init.d/nginx

#!/bin/bash
chkconfig: - 99 20
description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig --level 35 nginx on
chkconfig --list nginx
service nginx start
service nginx stop

cd /usr/local/nginx/html

echo "this is web1" >index.html

第二台节点服务器一样

==========================haporxy=====================
yum install -y pcre-devel bzip2-devel gcc gcc-c++

systemctl stop firewalld.service

systemctl disable firewalld.service

setenfoce 0

mount.cifs //192.168.10.3/linuxbage /opt/abc

cd /opt/abc/rhel7/Y2C7

tar zxf haproxy-1.5.19.tar -C /opt

cd /opt/haproxy-1.5.19.

make TARGET=linux26

make install

mkdir /etc/haproxy //创建一个目录给放haproxy配置文件

cp examples/haproxy.cfg /etc/haproxy/ //复制配置文件

cd /etc/haproxy/

vim haproxy.cfg /修改配置文件
chroot /usr/share/haproxy /删除,不禁锢家目录
redispatch /删除再发送功能

删除所有listen项目

listen webcluster 0.0.0.0:80
option httpchk GET /index.html //访问的页面
balance roundrobin
server inst1 192.168.100.26:80 check inter 2000 fall 3
server inst2 192.168.100.27:80 check inter 2000 fall 3

cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy /复制启动脚本到init

chmod +x /etc/init.d/haproxy

chkconfig --add /etc/init.d/haproxy

ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy //建立软连接命令脚本能被系统识别

service haproxy start

使用代理服务器的IP进行访问

猜你喜欢

转载自blog.51cto.com/13760226/2162583
今日推荐