HAproxy搭建Web集群

HAproxy搭建Web集群

一、基本环境

1. IP地址配置

主机IP地址系统
haproxy192.168.119.191/24CentOS 7.8
web01192.168.119.192/24CentOS 7.8
web02192.168.119.193/24CentOS 7.8

2. 主机名设置

# haproxy
$ hostnamectl set-hostname haproxy

# web01
$ hostnamectl set-hostname web01

# web02
$ hostnamectl set-hostname web02

3. 关闭Selinux

# 临时
$ setenforce 0

# 永久
$ sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

二、Web服务器

1. 添加Nginx源

$ vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2. 安装Nginx

$ yum install nginx -y

3. 添加Web页面

$ echo "<h1>This is web01 page!</h1>" > /usr/share/nginx/html/index.html
$ echo "<h1>This is web02 page!</h1>" > /usr/share/nginx/html/index.html

4. 启动服务

$ systemctl start nginx
$ systemctl enable nginx

5. 防火墙

$ firewall-cmd --add-service=http --permanent
$ firewall-cmd --reload

三、HAproxy

1. 安装haproxy

$ yum install haproxy -y

2. 配置rsyslog服务来接收haproxy的日志

# rsyslog服务
$ vim /etc/rsyslog.conf
	# Provides TCP syslog reception							# 开启UDP日志接收,使用514端口
	$ModLoad imtcp
	$InputTCPServerRun 514
	
# 添加日志规则
local2.*                                                /var/log/haproxy.log

# 重启服务
$ systemctl restart rsyslog

3. 配置haproxy

$ vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1:514  local2 info

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #最大连接数,根据应用实际情况进行调整,推荐使用10240
    user        haproxy
    group       haproxy
    daemon   #以后台形式运行haproxy


defaults
    mode                    http   #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
    log                     global
    option                  httplog
    option                  dontlognull
    timeout connect         10s
    timeout client          1m
    timeout server          1m

frontend  http_front
    bind  192.168.119.191:80
    stats uri /haproxy?stats
    default_backend http_back
backend http_back
    balance     roundrobin
    server web01 192.168.119.192:80  check
    server web02 192.168.119.193:80  check
    
# 重启服务
$ systemctl restart haproxy
$ systemctl enable haproxy

4. 防火墙

$ firewall-cmd --add-port=80/tcp --permanent
$ firewall-cmd --reload

5. 测试

$ curl 192.168.119.191
<h1>This is web01 page!</h1>
$ curl 192.168.119.191
<h1>This is web02 page!</h1>

6. stats页面

四、基于域名的负载均衡

1. 配置haproxy

# 主配置文件
$ vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1:514  local2 info

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #最大连接数,根据应用实际情况进行调整,推荐使用10240
    user        haproxy
    group       haproxy
    daemon   #以后台形式运行haproxy


defaults
    mode                    http   #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
    log                     global
    option                  httplog
    option                  dontlognull
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    
frontend http_front
    bind 192.168.119.191:80
    stats uri /haproxy?stats
    stats hide-version
    default_backend web01
    acl is_web02 hdr_end(host) www.web02.com
    use_backend web02 if is_web02
    
backend web01
    balance     roundrobin
    option forwardfor header X-REAL-IP
    option httpchk GET /index.html
    server      web01 192.168.119.192:80 check inter 2000 rise 3 fall 3 weight 1

backend web02
    balance     roundrobin
    option forwardfor header X-REAL-IP
    option httpchk GET /index.html
    server      web02 192.168.119.193:80 check inter 2000 rise 3 fall 3 weight 1
    
# 重启服务
$ systemctl restart haproxy

2. 在Windows中添加域名解析

# hosts文件路径
C:\Windows\System32\drivers\etc\hosts
	192.168.119.191 www.web01.com
	192.168.119.191 www.web02.com

3. 测试

五、调度算法采用IP哈希

1. 配置haproxy

$ vim /etc/haproxy/haproxy.cfg
backend web01
    balance     source

backend web02
    balance     source
    
# 重启服务
$ systemctl restart haproxy

2. 测试

$ curl 192.168.119.191
<h1>This is web01 page!</h1>
$ curl 192.168.119.191
<h1>This is web01 page!</h1>
$ curl 192.168.119.191
<h1>This is web01 page!</h1>
$ curl 192.168.119.191
<h1>This is web01 page!</h1>

六、配置stats页面认证

1. 配置haproxy

$ vim /etc/haproxy/haproxy.cfg
frontend http_front
    bind 192.168.119.191:80
    stats uri /haproxy?stats
    stats hide-version
    stats realm "Welcome to the haproxy load balancer status page of Legolas"					# 认证标题
    stats auth admin:admin123								# 认证用户和密码
    stats admin if TRUE								# 开启页面管理haproxy
    
# 重启服务
$ systemctl restart haproxy

2. 认证界面测试

3. 状态监测界面测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值