个人整理,感谢观看
一,Haproxy分析
- Haproxy是一款可提供高可用性,负载均衡,及基于TCP(四层)和HTTP(七层)应用的代理的软件
- 适用于负载大的web站点
- 运行在硬件上可支持数以万计的并发连接的请求
二,Haproxy的调度算法
最常用的三种
- RR(round robin)
最简单最常用的一种算法,即轮询调度 ; 跟LVS的rr一个意思
(个人理解,两个web服务器的话,就是两个轮着来!)
- LC (least connections)
最小连接算法,根据后端的节点连接数的大小动态分配前端请求
(个人理解 ,比如:在大学食堂里的打菜窗口,每个阿姨负责一个窗口,人多的窗口,就不考虑了,就直接回去人少的窗口打菜!!那个窗口人少,去哪里!!)
- SH(source hashing)
基于来源访问调度算法,用于一些有session会话记录在服务器端的场景,可以基于来源的ip,cookie等做集群调度
(个人理解,可以想象你去发廊,会给你指定托尼老师,就是你的造型师了,再来一个女客户,会再指定一个托尼老师给他;想象你下次再去发廊,发廊还是会安排你的托尼老师;女客户会找她的托尼老师;)
三,使用haproxy搭建web集群
防火墙已关闭,核心防护已关闭
3.1配置Haproxy 服务器 192.168.100.25
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 /dev/log local0 info
log /dev/log local1 notice
#log loghost local0 info
maxconn 10240
#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.26:80 check inter 2000 fall 3
server inst2 192.168.100.27:80 check inter 2000 fall 3 backup
##配置解释####
- 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.26:80 check inter 2000 fall 3:定义在线节点
- server inst2 192.168.100.27:80 check inter 2000 fall 3 backup:定义备份节点
3.2Haproxy日志
1.编辑
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf #文件没有的,咱们直接vi创建的
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
& ~
systemctl restart rsyslog.service
查看日志info信息,登录显示
[root@localhost log]# tail -f /var/log/haproxy/haproxy-info.log
Sep 24 07:14:32 localhost haproxy[27869]: 192.168.100.2:57847 [24/Sep/2020:07:13:42.349] webcluster webcluster/inst2 2/0/3/1/50008 200 247 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 07:16:08 localhost haproxy[27869]: 192.168.100.2:57866 [24/Sep/2020:07:15:26.955] webcluster webcluster/inst1 0/0/0/1/41874 200 781 - - CD-- 2/2/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 07:16:23 localhost haproxy[27869]: 192.168.100.2:57932 [24/Sep/2020:07:16:17.712] webcluster webcluster/inst1 0/0/0/1/5318 200 733 - - CD-- 2/2/1/0/0 0/0 "GET / HTTP/1.1"
3.3 web1 (nginx)192.168.100.26
编译安装Nginx服务器2 192.168.100.25
1、编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例 上传至/opt下
[root@localhost ~]#yum -y install gcc-c++ make pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# 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/ #路径优化
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 66738/nginx: master
3.添加 Nginx 系统服务
killall -3 nginx #停止服务为了防止命令冲突,nginx开启的话,sys命令可能会不能用
vi /lib/systemd/system/nginx
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
3、安装httpd 挂载测试页
[root@localhost nginx-1.12.2]# yum -y install nfs-utils rpcbind
[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/as1/ /usr/local/nginx/html/ nfs delfaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
3.4 web2 (nginx) 192.168.100.27
############ nginx安装流程同web1一样 ##########
1、安装httpd 挂载测试页
在这里插入代码片[root@localhost nginx-1.12.2]# yum -y install nfs-utils rpcbind
[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/as2/ /usr/local/nginx/html/ nfs delfaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
3.5 NFS存储服务器
1.安装nfs-utils rpcbind
yum -y install nfs-utils #nfs必须安装的,不然无法识别nfs格式,
yum -y install rpcbind
2.创建共享测试目录,和网页文件
mkdir /opt/as1 /opt/as2
echo 'this is as1' >/opt/as1/index.html #写些数据定义web1
echo 'this is as2' >/opt/as2/index.html #写些数据定义web2
3.添加共享目录,
vi /etc/exports #将共享目录添加在配置内,相当于发布
/opt/as1 192.168.100.0/24(rw,sync)
/opt/as2 192.168.100.0/24(rw,sync)
#重启服务,设置开机自启
systemctl restart nfs
systemctl restart rpcbind
systemctl enable nfs
systemctl enable rpcbind
showmount -e #查看当前共享的目录
Export list for localhost.localdomain:
/opt/as2 192.168.100.0/24
/opt/as1 192.168.100.0/24
3.6 验证haproxy
输入192.168.100.25 调度器Haproxy服务器地址;