Haproxy搭建Web集群

1.Haproxy介绍:

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。Haproxy是目前比较流行的一种集群调度工具。

2.Haproxy支持多种调度算法:

RR(Round Robin)轮询调度,最简单最常用的一种算法。

LC(Least Connections)最小连接数算法,根据后端的节点连接数大小动态分配前端请求。目前用的比较多的算法。

SH(Source Hashing)基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度。

3.Haproxy 配置中文件通常分为三部分内容,分别如下:

  1. global:  设置全局配置参数,属于进程的配置,通常是和操作系统相关。
  2. defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件。
  3. Listen :frontend和backend的组合体。

4.使用haproxy搭建web集群

4.1 配置Haproxy 服务器  192.168.100.41 

1.编译安装 Haproxy
上传 haproxy-1.4.24.tar.gz 到/opt目录下
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost opt]#uname -r         #查看linux内核版本信息,要注意操作系统版本信息
3.10.0-957.el7.x86_64
[root@localhost haproxy-1.4.24]# make TARGET=linux31       # 根据内核信息编译
[root@localhost haproxy-1.4.24]# make install

2.配置Haproxy 服务
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    #log loghost local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    uid 99
    gid 99
    daemon
    #debug
    #quiet

defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    #redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

listen webcluster 0.0.0.0:80
    option httpchk GET /index.html
    balance roundrobin
    server inst1 192.168.100.42:80 check inter 2000 fall 3
    server inst2 192.168.100.43:80 check inter 2000 fall 3

###配置文件解释###

  • log /dev/log local0 info #配置日志记录 ,日志设备,info信息
  • log /dev/log local1 notice
  • maxconn 4096 #最大连接数
  • uid 99:用户uid gid 99:组用户gid # nobaby用户
  • listen配置项目一般为配置应用模块参数
  • listen appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用
  • option httpchk /index.html:检查服务器的index.html文件
  • option persist :强制将请求发送到已经down掉的服务器
  • balance roundrobin:负载均衡调度算法使用轮询算法
  • server inst1 192.168.100.42:80 check inter 2000 fall 3:定义在线节点
  • server inst2 192.168.100.43:80 check inter 2000 fall 3 backup:定义备份节点

4.2 Haproxy日志

[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf

if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~

[root@localhost haproxy-1.4.24]# systemctl restart rsyslog.service

[root@localhost haproxy-1.4.24]# tail -f /var/log/haproxy/haproxy-info.log
Sep 27 00:08:33 localhost haproxy[13864]: 192.168.32.1:57172 [27/Sep/2018:00:07:43.232] webcluster webcluster/inst1 1/0/1/2/50006 404 726 - - cD-- 2/2/1/0/0 0/0 "GET /favicon.ico HTTP/1.1"
Sep 27 00:08:40 localhost haproxy[13864]: 192.168.32.1:57223 [27/Sep/2018:00:07:47.477] webcluster webcluster/inst2 0/0/1/2/52806 200 750 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"

4.3 编译安装Nginx服务器1  192.168.100.42

1.编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例 上传至/opt下
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]#
./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx


[root@localhost nginx-1.12.2]# make && make install

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    #路径优化
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx 
lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

2.启动、 停止 Nginx

killall -1 nginx             ####安全重启
killall -3 nginx            ###停止服务

如果出现: -bash: killall: command not found

yum -y install psmisc

[root@localhost ~]# nginx ####启动

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

tcp    0          0 0.0.0.0:80              0.0.0.0:*              LISTEN            7180/nginx: master

3.添加 Nginx 系统服务

[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx ####描述
After=network.target ####描述服务类别
[Service]
Type=forking ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service

[root@localhost ~]# killall -3 nginx ###停止服务
[root@localhost ~]# systemctl start nginx.service

4.安装httpd 挂载测试页

[root@localhost ~]# showmount -e 192.168.100.44 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/51xit (everyone)
/opt/52xit (everyone)

[root@localhost ~]# mount 192.168.100.44:/opt/51xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.44:/opt/51xit/ /usr/local/nginx/html/ nfs rw,tcp,intr 0 1 ###开机自动挂载,注意格式对齐

[root@localhost nginx-1.12.2]# systemctl restart nginx

4.4  编译安装Nginx服务器2  192.168.100.43

###nginx安装流程同1一样###

1.安装httpd 挂载测试页

[root@localhost ~]# showmount -e 192.168.100.44 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.44:
/opt/51xit (everyone)
/opt/52xit (everyone)

[root@localhost ~]# mount 192.168.100.44:/opt/52xit/ /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.44:/opt/52xit/ /usr/local/nginx/html/ nfs rw,tcp,intr 0 1 ###开机自动挂载,注意格式对齐

[root@localhost ~]# systemctl restart nginx

4.5 调试存储服务器  192.168.100.44

1.安装nfs-utils rpcbind

yum -y install nfs-utils   ### nfs必须安装的,不然无法识别nfs格式,

yum -y install rpcbind

2.创建共享测试目录,和网页文件

mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html   ###写数据定义服务器1
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html   ###写数据定义服务器2

 

3.添加共享目录

vi /etc/exports     ####将共享目录添加在配置内发布
/opt/51xit 192.168.100.0/24 (rw,sync) 
/opt/52xit 192.168.100.0/24 (rw,sync)

systemctl restart nfs  ###重启服务
systemctl restart rpcbind
systemctl enable nfs   ###设置开机自启
systemctl enable rpcbind

#####测试网站#####
192.168.100.41 切换 会发现不同的网站页面 说明已经实现了负载均衡

道阻且长,行则将至!加油! --不是冷漠
原文地址:https://www.cnblogs.com/bushilengmo/p/13726292.html