Haprox 负载均衡服务器

1. Haproxy

​ Haproxy 是一个免费的负载均衡软件 ,可以运行大部分主流的linux操作系统之上。

HaProxy 功能:

​ 负载均衡: L4和L7 两种模式,支持 RR、静态 RR、 IP Hash、URL_RARAM、Hash、等负载均衡算法。

​ 健康检查: 支持TCP和HTTP两种健康检查模式

​ 会话保持: 对于未实现会话共享的应用集群。

​ 监控与统计:HAProxy提供了基于Web的统计信息页面,展现健康状态和流量数据。基于此功能,使用者可以开发监控程序来监控HAProxy的状态

2. 四层负载与七层负载

  • 四层负载:

    针对于 IP地址、端口、mac地址进行负载, 例如 : LVS

    基于传输层,传输的速度快,负载不做任何的解析,直接将客户端的请求交给后端的服务器,不安全

  • 七层负载

    代表服务: Nginx、 Haproxy

    基于应用层,负载均衡服务器回和客户端建立TCP连接诶,接收其内容,并通过URL的路径进行解析,过滤出客户端的请求资源信息(index.php),会根据与过滤出的请求提交给后端比较适合的服务器,可以拒绝空连接, 安全性比较高,但是速度慢,同样也可以支持四层

3. HaProxy 调度算法

  1. roundrobin 动态轮询, weight(权重) 不需要重启Haproxy
  1. static-rr 静态轮询, weight ,需要重启Haproxy才能生效
  1. leastconnect 最小连接
  1. source 原地址散列, 源地址通过hash加密

4. Haproxy 负载Web

实验环境:

HostIP
Haproxy1.1.1.101
web11.1.1.102
web21.1.1.103
Client1.1.1.104

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I083j7Gv-1584340972490)(C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7VjmoDcK6mV43CyWcqdjkLo5g\e8e7ac3cb5cf402d8461002fbdea05b1\clipboard.png)]

  • 工作流程:

    ​ 对于CLinet 来说, 负载均衡服务器就是 web服务器,所以访问时,访问的是 负载均衡服务器,当用户访问向 负载均衡服务器发送网页请求 ,负载均衡服务器收到用户的网页请求,会根据自己的节点信息与调度算法,调用后端的web服务器,解析用户的请求,再次通过 负载均衡那个服务器返给用户网页

1) 部署Web服务

  • Web1
[root@localhost ~]# hostname web1
[root@localhost ~]# bash
[root@web1 ~]# yum -y install httpd

[root@web1 ~]# echo "This is a web server-1" > /var/www/html/index.html
[root@web1 ~]# systemctl restart httpd
[root@web1 ~]# curl 1.1.1.102
This is a wen server-1
  • Web2

[root@localhost ~]# hostname web2
[root@localhost ~]# bash
[root@web2 ~]# yum -y install httpd

[root@web2 ~]# echo "This is a web server-2" > /var/www/html/index.html
[root@web2 ~]# systemctl restart httpd
[root@web2 ~]# curl 1.1.1.103
This is a web server-2

2) 部署 Haproxy

  • 解决依赖关系
[root@localhost ~]# yum -y install pcre-devel bzip2-devel 
  • 安装 Haproxy
[root@localhost ~]# tar zxf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# uname -r
3.10.0-862.el7.x86_64

    # 查看内核版本,是因为在编译时要指定内核版本。
[root@localhost haproxy-1.4.24]# make TARGET=linux3.10 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy
  • 优化路径
[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/
  • 创建Haproxy 配置文件目录
[root@localhost ~]# mkdir /etc/haproxy
  • copy haproxy 配置文件
    • 需要copy 配置文件模板
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
  • 将Haproxy 的启动脚本 复制 至 init.d 下由系统进行管理
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod a+x /etc/init.d/haproxy 
[root@localhost ~]# chmod +x /etc/init.d/haproxy 
[root@localhost ~]# sed -i '1a/chkconfig:35 23 23' /etc/init.d/haproxy 
[root@localhost ~]# chkconfig --add haproxy
  • 修改配置文件
#chroot /usr/share/haproxy     # 注释掉,因haproxy工作目录不再share下
#redispatch                    # 该选项是 客户端访问时产生的cookie的对应节点坏掉时,就会直接转向至另一台服务器上

# 将 26之后的内容全部删除,手动编写以下内容
listen webserver 0.0.0.0:80             # haproxy 监听的地址与端口
        balance roundrobin              # 指定轮训方式,  roundrobib(动态轮询)
        option httpchk GET /index.html       #  为后端的服务器去,记性健康检查
        server web_one 1.1.1.102:80 check inter 2000 rise 3 fall 3         # 监听的 节点信息,  
        # web_one 节点name,IP:port  
        #check 开启节点健康检查  
       #inter 2000        每隔  2000毫秒进行一个jiance
       # rise 3 fall      后端建立成功的次数,和失败的次数,   如果三次健康检查都失败的话则将该 web 服务区踢出负载群集
       # 还可以添加 weight(权重)  maxconn(最大连接)   backup(使当前节点为备份节点)
       server web_two 1.1.1.103:80 check inter 2000 rise 3 fall 3

  • 启动服务
    • 如果启动失败,需要检查配置文件,且端口号是否占用等情况
[root@localhost ~]# /etc/init.d/haproxy restart 
Restarting haproxy (via systemctl):                        [  确定  ]

[root@localhost ~]# netstat -anpt | grep 80
t

3) Client 验证

[root@localhost ~]# curl 1.1.1.101
This is a wen server-1
[root@localhost ~]# curl 1.1.1.101
This is a web server-2
[root@localhost ~]# curl 1.1.1.101
This is a wen server-1
[root@localhost ~]# curl 1.1.1.101
This is a web server-2

5. Haproxy 负载数据库

1) 部署 Mysql 数据库

  • Web1
root@web1 ~]# yum -y install mariadb mariadb-server
[root@web2 ~]# systemctl start mariadb
[root@web1 ~]# netstat -anpt | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      12263/mysqld    

# 创建数据库 one
[root@web1 ~]# mysql
MariaDB [(none)]> CREATE DATABASE one;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON *.* TO 'haproxy'@'1.1.1.%' IDENTIFIED BY '123.com';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| one                |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

  • Web 2
[root@web1 ~]# yum -y install mariadb mariadb-server
[root@web2 ~]# systemctl start mariadb
[root@web1 ~]# netstat -anpt | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      12263/mysqld    

# 创建 数据库  two
[root@web1 ~]# mysql
MariaDB [(none)]> CREATE DATABASE two;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON *.* TO 'haproxy'@'1.1.1.%' IDENTIFIED BY '123.com';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| two                |
+--------------------+
5 rows in set (0.01 sec)

2) 部署 Haproxy

  • 修改配置文件
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 
defaults
        log     global
        mode    tcp            # 默认使用的是 http协议,这是要更成 tcp协议 支持 mysql
        option  tcplog         # 访问日志更改为  tcplog

listen webserver 0.0.0.0:3306
        balance roundrobin
        #option httpchk GET /index.html
        server web_one 1.1.1.102:3306 check port 3306 maxconn 100
        server web_two 1.1.1.103:3306 check port 3306 maxconn 100
  • 启动服务
[root@localhost ~]# /etc/init.d/haproxy restart 
Restarting haproxy (via systemctl):                        [  确定  ]
[root@localhost ~]# netstat -anpt | grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12459/haproxy   
  1. 客户端访问验证
  • 安装 mysql 客户端工具
[root@localhost ~]# yum -y install mariadb
  • 登录数据库
# 首次登陆进行验证, 通过 show databases 命令可以看出当前在 mysql 1数据库中
[root@localhost ~]# mysql -u haproxy -p123.com -P 3306 -h 1.1.1.101
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| one                |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

MariaDB [(none)]> exit
Bye
  • 再次登录
    • 再次登陆,会发现在 mysql2的数据库中
[root@localhost ~]# mysql -u haproxy -p123.com -P 3306 -h 1.1.1.101
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| two                |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

lp. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| two |
±-------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值