haproxy

HAproxy代理

haproxy简介

haproxy是一款提供高可用性,负载均衡以及tcp四层,http七层应用的代理软件,支持虚拟主机,并且有着免费,部署快速简单等诸多优点

haproxy管理

haproxy安装

yum -y install haproxy

haproxy主配置文件的结构

配置文件路径为/etc/haproxy/haproxy.conf
配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen。

主配置文件的配置

vi /etc/haproxy/haproxy.cfg

global 		# 全局配置
    log 127.0.0.1 local3 info 	# 定义全局性的日志
    chroot /var/lib/haproxy 	# 修改haproxy的工作目录
    maxconn 4000 		# haproxy接受的最大并发数
    daemon 				# 用守护进程的方式运行haproxy
    user haproxy 		# 运行haproxy的用户
    group haproxy 		# 运行haproxy的组

defaults 		# 用于提供默认参数的配置        !!!代理配置
    log global 	# 日志使用global的全局定义
    mode http 		# 模式http
    option httplog 	# 记录http请求
    retries 3	 	# 重试3次
    #option forwardfor 	#如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
    option httpclose 	#每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
    #option redispatch 	#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持
    timeout connect 5000 	# 转发后端的超时时间
    timeout client 50000 	# 客户端超时时间
    timeout server 50000 	# 等待服务器响应的超时时间

listen stats 		# 监听统计状态
    mode http
    bind 0.0.0.0:8080 	# 绑定地址端口
    stats enable 		# 启用统计报告
    stats uri /stats 	# 定义访问统计页面路径
    stats realm Haproxy\ Statistics 
    stats auth admin:admin 	# 定义认证用户账号密码

listen nginxserver 	# 监听的服务名称
    bind *:80 		# 绑定的地址和端口
    mode http 		# 定义运行的模式
    default_backend nginx 	# 设定代理对应的后端

backend nginx 		# 代理后端服务器的配置
    balance roundrobin 		# 定义轮训算法
    option httpchk GET /index.html 		# 设定检查方式
    server nginx1 192.168.0.104:80 check inter 2000 rise 3 fall 3 weight 30 	# 定义轮训后端以及代理健康检查
    server nginx2 192.168.0.102:80 check inter 2000 rise 3 fall 3 weight 30

acl配置

listen nginxserver
    bind *:80
    mode http
    default_backend nginx
    acl deny_src src -i 192.168.0.101
    tcp-request content reject if deny_src

主配置文件的参数注释

###########全局配置#########
global
  log 127.0.0.1 local0 		#[日志输出配置,所有日志都记录在本机,通过local0输出]
  log 127.0.0.1 local1 notice 	#定义haproxy 日志级别[error warringinfo debug]
  daemon 		#以后台形式运行harpoxy
  nbproc 1 		#设置进程数量
  maxconn 4096 		#默认最大连接数,需考虑ulimit-n限制
  #user haproxy 	#运行haproxy的用户
  #group haproxy 	#运行haproxy的用户所在的组
  #pidfile /var/run/haproxy.pid 	#haproxy 进程PID文件
  #ulimit-n 819200 					#ulimit 的数量限制
  #chroot /usr/share/haproxy 		#chroot运行路径
  #debug 			#haproxy 调试级别,建议只在开启单进程的时候调试
  #quiet

########默认配置############
defaults
  log global
  mode http 			#默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
  option httplog 		#日志类别,采用httplog
  option dontlognull 	#不记录健康检查日志信息
  retries 2 			#两次连接失败就认为是服务器不可用,也可以通过后面设置
  #option forwardfor 	#如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
  option httpclose 		#每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
  #option redispatch 	#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持
  option abortonclose 	#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
  maxconn 4096 			#默认的最大连接数
  timeout connect 5000ms 	#连接超时
  timeout client 30000ms 	#客户端超时
  timeout server 30000ms 	#服务器超时
  #timeout check 2000 		#心跳检测超时
  #timeout http-keep-alive10s 	#默认持久连接超时时间
  #timeout http-request 10s 	#默认http请求超时时间
  #timeout queue 1m 		#默认队列超时时间
  balance roundrobin 		#设置默认负载均衡方式,轮询方式
  #balance source 			#设置默认负载均衡方式,类似于nginx的ip_hash
  #balnace leastconn 		#设置默认负载均衡方式,最小连接数

########统计页面配置########
listen stats
  bind 0.0.0.0:1080 		#设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称
  mode http 				#http的7层模式
  option httplog 			#采用http日志格式
  #log 127.0.0.1 local0 err #错误日志记录
  maxconn 10 				#默认的最大连接数
  stats refresh 30s 		#统计页面自动刷新时间
  stats uri /stats 			#统计页面url
  stats realm XingCloud\ Haproxy 	#统计页面密码框上提示文本
  stats auth admin:admin 			#设置监控页面的用户和密码:admin,可以设置多个用户名
  stats auth Frank:Frank 			#设置监控页面的用户和密码:Frank
  stats hide-version 				#隐藏统计页面上HAProxy的版本信息
  stats admin if TRUE 				#设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本)

########设置haproxy 错误页面#####
#errorfile 403 /home/haproxy/haproxy/errorfiles/403.http
#errorfile 500 /home/haproxy/haproxy/errorfiles/500.http
#errorfile 502 /home/haproxy/haproxy/errorfiles/502.http
#errorfile 503 /home/haproxy/haproxy/errorfiles/503.http
#errorfile 504 /home/haproxy/haproxy/errorfiles/504.http

########frontend前端配置##############
frontend main
  bind *:80 						 #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
  acl web hdr(host) -i www.abc.com   #acl后面是规则名称,-i为忽略大小写,后面跟的是要访问的域名,如果访问www.abc.com这个域名,就触发web规则,。
  acl img hdr(host) -i img.abc.com   #如果访问img.abc.com这个域名,就触发img规则。
  use_backend webserver if web   	 #如果上面定义的web规则被触发,即访问www.abc.com,就将请求分发到webserver这个作用域。
  use_backend imgserver if img  	 #如果上面定义的img规则被触发,即访问img.abc.com,就将请求分发到imgserver这个作用域。
  default_backend dynamic 			#不满足则响应backend的默认页面

########backend后端配置##############
backend webserver #webserver作用域
  mode http
  balance roundrobin #balance roundrobin 负载轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
  option httpchk /index.html HTTP/1.0 	#健康检查, 检测文件,如果分发到后台index.html访问不到就不再分发给它
  server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3
  server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3
  #cookie 1表示serverid为1,check inter 1500 是检测心跳频率 
  #rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

backend imgserver
  mode http
  option httpchk /index.php
  balance roundrobin 
  server img01 192.168.137.101:80 check inter 2000 fall 3
  server img02 192.168.137.102:80 check inter 2000 fall 3

backend dynamic 
  balance roundrobin 
  server test1 192.168.1.23:80 check maxconn 2000 
  server test2 192.168.1.24:80 check maxconn 2000


listen tcptest 
  bind 0.0.0.0:5222 
  mode tcp 
  option tcplog 	#采用tcp日志格式 
  balance source 
  #log 127.0.0.1 local0 debug 
  server s1 192.168.100.204:7222 weight 1 
  server s2 192.168.100.208:7222 weight 1

haproxy+keepalived+nginx配置

准备工作

nginx server1 192.168.2.191
nginx server2 192.168.2.190
haproxy 1+keepalived(主) 192.168.2.194
haproxy 2+keepalived(备) 192.168.2.195

配置nginx server1

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/1111
echo "server 11111" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

http://192.168.2.191

配置nginx server2

yum -y install epel-release
yum -y install nginx

systemctl start nginx
systemctl enable nginx

mv /usr/share/nginx/html/index.html /usr/share/nginx/html/2222
echo "server 22222" > /usr/share/nginx/html/index.html

systemctl stop firewalld
setenforce 0

http://192.168.2.190

haproxy 1+keepalived(主)

1、安装

yum -y install haproxy

2、修改haproxy主配置文件

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak    #备份主配置文件

vi /etc/haproxy/haproxy.cfg
global		# 全局配置
    log 127.0.0.1 local3 info		# 定义全局性的日志
    chroot /var/lib/haproxy			# 修改haproxy的工作目录
    maxconn 4000			# haproxy接受的最大并发数
    daemon					# 用守护进程的方式运行haproxy
    user haproxy			# 运行haproxy的用户
    group haproxy			# 运行haproxy的组

defaults	# 用于提供默认参数的配置
    log global		# 日志使用global的全局定义
    mode http		# 模式http
    option httplog	# 记录http请求
    retries 3		# 重试3次
    timeout connect 5000		# 转发后端的超时时间
    timeout client 50000		# 客户端超时时间
    timeout server 50000		# 等待服务器响应的超时时间

listen stats		 # 监听统计状态
    mode http		
    bind 0.0.0.0:8080		 # 绑定地址端口
    stats enable			 # 启用统计报告
    stats uri /stats		 # 定义访问统计页面路径
    stats realm Haproxy\ Statistics	
    stats auth admin:admin		# 定义认证用户账号密码

listen nginxserver		# 监听的服务名称
    bind 192.168.2.100:80			# 绑定的地址和端口
    mode http			# 定义运行的模式
    default_backend nginx		# 设定代理对应的后端

backend nginx		# 代理后端服务器的配置
    balance roundrobin		# 定义轮训算法
    option httpchk GET /index.html		# 设定检查方式
    server nginx1 192.168.2.191:80 check inter 2000 rise 3 fall 3 weight 30 	# 定义轮训后端以及代理健康检查
    server nginx2 192.168.2.190:80 check inter 2000 rise 3 fall 3 weight 30

3、启动服务

systemctl start haproxy
systemctl enable haproxy

4、关闭防火墙

systemctl stop firewalld
setenforce 0

5、查看服务是否开启

systemctl status haproxy

yum -y install net-tools 

netstat -lntp
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8169/haproxy   

6、安装keepalived

yum -y install keepalived ipvsadm

7、修改keepalived主配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

vi /etc/keepalived/keepalived.conf
global_defs {
    router_id test		# 服务器名字
}

vrrp_instance VI_1 {
    state MASTER            # MASTER代表为主的keepalived服务
    interface ens33			# 指定接口
    virtual_router_id 51	# 指定路由ID,主备必须一样
    priority 101			# 设置优先级,主略高于备份
    advert_int 1			# 设置检查时间
    authentication {
        auth_type PASS		# 设置验证加密方式
        auth_pass 1234		# 设置验证密码
    }
    virtual_ipaddress {
        192.168.2.100
    }
}

8、启动keepalived服务

systemctl start keepalived
systemctl enalbe keepalived


9、查看keepalived服务的状态

systemctl status keepalived

ip a # 查看虚拟IP地址

ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.100:80 rr
-> 192.168.2.190:80 Route 1 0 0
-> 192.168.2.191:80 Route 1 0 0


**haproxy 2+keepalived(备)**

1、安装haproxy服务

yum install haproxy


2、修改haproxy主配置文件

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
maxconn 4000
daemon
user haproxy
group haproxy

defaults
log global
mode http
option httplog
retries 3
timeout connect 5000
timeout client 50000
timeout server 50000

listen stats
mode http
bind 0.0.0.0:8080
stats enable
stats uri /stats
stats realm Haproxy\ Statistics
stats auth admin:admin

listen nginxserver
bind 192.168.2.100:80 #这里监听keepalived中的虚拟IP地址
mode http
default_backend nginx

backend nginx
balance roundrobin
option httpchk GET /index.html
server nginx1 192.168.2.191:80 check inter 2000 rise 3 fall 3 weight 30
server nginx2 192.168.2.190:80 check inter 2000 rise 3 fall 3 weight 30


3、启动haproxy服务

systemctl start haproxy
systemctl enable haproxy


4、关闭防火墙

systemctl stop firewalld
setenforce 0


5、查看服务是否开启

systemctl status haproxy

yum -y install net-tools

netstat -lntp
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8169/haproxy


6、安装keepalived服务

yum -y install keepalived


7、修改keepalived配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak #备份主配置文件

vi /etc/keepalived/keepalived.conf
global_defs {
router_id test # 服务器名字
}

vrrp_instance VI_1 {
state BACKUP # BCKUP表为备份的keepalived服务
interface ens33 # 指定接口
virtual_router_id 51 # 指定路由ID,主备必须一样
priority 99 # 优先级小于主的优先级
advert_int 1 # 设置检查时间
authentication {
auth_type PASS # 设置验证加密方式
auth_pass 1234 # 设置验证密码
}
virtual_ipaddress {
192.168.2.100
}
}


8、启动keepalived服务

systemctl start keepalived
systemctl enalbe keepalived


9、查看keepalived服务的状态

systemctl status keepalived

ip a # 备份上是不显示虚拟IP地址,除非主keepalived服务故障,备份的就升为主

ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.100:80 rr
-> 192.168.2.190:80 Route 1 0 0
-> 192.168.2.191:80 Route 1 0 0


**测试**

http://192.168.2.100

curl 192.168.2.100
server 11111
curl 192.168.2.100
server 2222

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值