Haproxy的安装与配置

haproxy概述:

HAProxy是一个使用C语言编写的自由及开放源代码软件,它提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,
完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。


HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、
系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。
此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作


实验环境:

server1:172.25.66.1  haproxy服务器端
server2:172.25.66.2  apache后端服务器
server3:172.25.66.3  apache后端服务器

1.haproxy的安装

安装包:

haproxy-1.7.3.tar.gz


点击此处即可下载haproxy安装包

1.下载haproxy安装包并解压

#1.下载
[root@server1 ~]# ls
haproxy-1.7.3.tar.gz
#2.解压
[root@server1 ~]# tar zxf haproxy-1.7.3.tar.gz 
[root@server1 ~]# ls
haproxy-1.7.3  haproxy-1.7.3.tar.gz
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# ls
CHANGELOG     doc       include      Makefile  scripts  tests
contrib       ebtree    LICENSE      README    src      VERDATE
CONTRIBUTING  examples  MAINTAINERS  ROADMAP   SUBVERS  VERSION


2. 制作rpm包

#前提:存在以.spec结尾的文件
[root@server1 haproxy-1.7.3]# find -name *.spec
./examples/haproxy.spec


#1.安装rpm-build工具(制作rpm包的工具)
[root@server1 ~]# yum install -y rpm-build 
#制作包编译:报错
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz 
error: Failed build dependencies:
	pcre-devel is needed by haproxy-1.7.3-1.x86_64


#2.解决依赖性
[root@server1 ~]# yum install -y pcre-devel 


[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz 

在这里插入图片描述
[root@server1 ~]# yum install -y gcc
在这里插入图片描述
#3.制作包编译;没有error报错即可
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz
3.安装

#1.发现生成了rpmbuild目录
[root@server1 ~]# ls
haproxy-1.7.3 haproxy-1.7.3.tar.gz rpmbuild
[root@server1 ~]# cd rpmbuild/RPMS/x86_64
[root@server1 x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
#2.安装; i:install v:view h:hash(哈希)
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm 2.负载均衡

1.拷贝配置文件

root@server1 ~]# cd /root/haproxy-1.7.3/examples/
#拷贝文件并重命名
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 examples]# cd /etc/haproxy
[root@server1 haproxy]# ls
haproxy.cfg
#查看参数
[root@server1 haproxy]# vim haproxy.cfg
2建立用户和组

[root@server1 haproxy]# id haproxy
id: haproxy: No such user
#1.建立组
[root@server1 haproxy]# groupadd -g 200 harproxy
#2.建立用户
[root@server1 haproxy]# useradd -u 200 -g 200 -s /sbin/nologin -M haproxy
[root@server1 haproxy]# id haproxy
uid=200(haproxy) gid=200(harproxy) groups=200(harproxy)
#发现可以切换到haproxy用户,但只是提供了一个shell而已
[root@server1 haproxy]# su - haproxy
su: warning: cannot change directory to /home/haproxy: No such file or directory
-bash-4.1$ exit
logout

3.更改配置文件

[root@server1 haproxy]# pwd
/etc/haproxy
#更改配置文件
[root@server1 haproxy]# vim haproxy.cfg

#######################
defaults
        mode            http            #默认的模式mode
        log             global          #应用全局的日志配置
        option          httplog         #启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求日志
        option          dontlognull     #启用该项,日志中将不会记录空连接
        monitor-uri     /monitoruri     #健康检查
        maxconn         80000           #每个进程可用的最大连接数
        timeout client  30s             #客户端超时

        stats uri       /admin/stats    #监控进程

        option prefer-last-server
        retries         2           #定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端服务器标记为不可用
        option redispatch           #当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉
        timeout connect 5s          #连接超时
        timeout server  5s          #服务器端超时
        
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem

        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend dynamic

backend dynamic
        balance         roundrobin    #负载均衡算法
        server          dynsrv1 172.25.66.2:80  check inter 1000    #后端1
        server          dynsrv2 172.25.66.3:80  check inter 1000    #后端2

在这里插入图片描述

    4.启动haproxy
    
    [root@server1 haproxy]# /etc/init.d/haproxy start
    Starting haproxy:                                          [  OK  ]
    [root@server1 haproxy]# netstat -tnlp
5.配置后端服务器

在server2上:

#1.安装apache
[root@server2 ~]# yum install -y httpd
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
#2.编写默认发布文件
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html 
server2
#3.开启apache
[root@server2 html]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.66.2 for ServerName
                                                           [  OK  ]



在server3上:

#1.安装apache
[root@server3 ~]# yum install -y httpd
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
#2.编写默认发布文件
[root@server3 html]# vim index.html
[root@server3 html]# cat index.html 
server3
#3.开启apache
[root@server3 html]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.66.3 for ServerName

在这里插入图片描述

3.用户认证

[root@server1 haproxy]# pwd
/etc/haproxy
#1.更改配置文件
[root@server1 haproxy]# vim haproxy.cfg 
#######################
stats auth      admin:westos       #设定认证用户和密码
stats refresh   5s                 #每5秒刷新一次

#2.重新启动
[root@server1 haproxy]# /etc/init.d/haproxy restart
Shutting down haproxy:                                     [  OK  ]
Starting haproxy:                                          [  OK  ]

在这里插入图片描述

6.访问控制

#1.设定黑名单
[root@server1 haproxy]# pwd
/etc/haproxy
[root@server1 haproxy]# vim haproxy.cfg 
#######################

acl blacklist src 172.25.66.254     #acl访问控制;设定黑名单:拒绝在blacklist列表中的主机访问
        
http-request deny if blacklist

在这里插入图片描述

测试:

#1.server2主机访问正常
[root@server2 ~]# curl 172.25.66.1
server2
[root@server2 ~]# curl 172.25.66.1
server3


#2.server3主机访问正常
[root@server3 ~]# curl 172.25.66.1server2
[root@server3 ~]# curl 172.25.66.1
server3



#3.物理机访问失败,因为它在黑名单中
[root@foundation66 ~]# curl 172.25.66.1

在这里插入图片描述

改进:

利用重定向将访问错误界面友好化


(1).更改haproxy配置文件

#1.更改配置文件
[root@server1 haproxy]# pwd
/etc/haproxy
[root@server1 haproxy]# vim haproxy.cfg 
#######################
errorloc 403 http://172.25.66.1:8080/index.html        #将403访问错误页面调转本机的index.html发布页面上(方式1)

在这里插入图片描述
(2).配置apache

#1.安装apache
[root@server1 haproxy]# yum install -y httpd
#2.更改端口;因为haproxy服务已经占用了80端口
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
#################
Listen 8080
在这里插入图片描述
#3.编写默认发布文件
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls
[root@server1 html]# vim index.html
[root@server1 html]# cat index.html
您已经被拉黑了…
在这里插入图片描述

#4.重启apache
[root@server1 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.25.66.1 for ServerName
[ OK ]
7.动静分离

server2: 172.25.66.2 静态页面(apache)
server3: 172.25.66.3 动态页面(php)

1.配置php动态页面

#1.安装php
[root@server3 ~]# yum install -y php
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
index.html
#2.编写php默认发布首页
[root@server3 html]# vim index.php
####################
<?php
phpinfo()
?>

#3.重启apache
[root@server3 html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.25.66.3 for ServerName

2.更改配置文件

[root@server1 haproxy]# pwd
/etc/haproxy
#1.更改配置文件
[root@server1 haproxy]# vim haproxy.cfg 
#######################
use_backend      dynamic  if { path_end.php }    #当访问以.php结尾的直接跳转到server3结点上
default_backend static      #默认访问的是server2结点
        
backend static  
        balance         roundrobin
        server          dynsrv1 172.25.66.2:80  check inter 1000

backend dynamic
        balance         roundrobin
        server          dynsrv2 172.25.66.3:80  check inter 1000

在这里插入图片描述
在网页上输入:172.25.66.1/index.php , 发现以.php结尾的访问的是server3结点(动态php界面)
在这里插入图片描述
在网页上输入:172.25.66.1 ,发现默认访问的是server2结点(apache静态界面)
在这里插入图片描述
8.读写分离

server2: 172.25.66.2 写
server3: 172.25.66.3 读

1.更改配置文件

#1.更改配置文件:写在server2结点上
[root@server1 ~]# cd /etc/haproxy/
[root@server1 haproxy]# vim haproxy.cfg
在这里插入图片描述
#2.重新加载
[root@server1 haproxy]# /etc/init.d/haproxy reload

2.下载上传工具upload

[root@foundation66 Desktop]# scp -r upload/ root@172.25.66.2:/var/www/html

[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
index.html upload

3.更改权限

[root@server2 html]# cd upload/
[root@server2 upload]# ls
index.php upload_file.php
[root@server2 upload]# ll
total 8
-rwxr-xr-x 1 root root 257 Feb 19 23:51 index.php
-rwxr-xr-x 1 root root 927 Feb 19 23:51 upload_file.php
#1.更改文件权限
[root@server2 upload]# chmod 644 *
[root@server2 upload]# ll
total 8
-rw-r–r-- 1 root root 257 Feb 19 23:51 index.php
-rw-r–r-- 1 root root 927 Feb 19 23:51 upload_file.php
#2.移动文件到apache默认发布目录
[root@server2 upload]# mv * …
[root@server2 upload]# cd …
[root@server2 html]# ls
index.html index.php upload upload_file.php
[root@server2 html]# ll -d upload
drwxrwxr-x 2 root root 4096 Feb 19 23:53 upload
#3.更改目录权限
[root@server2 html]# chmod 777 upload
[root@server2 html]# ll -d upload
drwxrwxrwx 2 root root 4096 Feb 19 23:53 upload

4.安装php

[root@server2 html]# yum install -y php

5.重启apache

[root@server2 html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.25.66.2 for ServerName
[ OK ]

6.配置server3结点

#1.发送测试文件
[root@server2 html]# scp -rp upload upload_file.php index.php root@172.25.66.3:/var/www/html/
[root@server3 html]# pwd
/var/www/html
[root@server3 html]# ls
index.html index.php upload upload_file.php

#2.重启apache
[root@server3 html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.25.66.3 for ServerName
[ OK ]

7.更改上传图片大小限制

更改上传图片大小限制:是为了防止造成由于图片过大而上传失败的情形

[root@server2 html]# pwd
/var/www/html
[root@server2 html]# vim upload_file.php

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值