Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具备不少,如LVS和Nginx。相比较而言,LVS性能最好,可是搭建相对复杂;Nginx的upstream模块支持群集功能,可是相对群集节点健康检查功能不强,性能没有Haproxy好,更多的是应用在企业内网环境中。
搭建LVS群集能够参考博文:Centos 7搭建LVS+Keepalived高可用Web服务群集html
Nginx群集能够参考博文:基于centos 7安装Tomcat服务及其负载均衡linux
关于Haproxy搭建Web群集原理概述参考博文:Haproxy搭建Web群集概述nginx
部署环境以下:web
准备工做redis
1)调通网络,防火墙放行相关流量(我这里直接将防火墙关闭了);算法
2)准备系统映像,配置本地yum(自行配置);apache
3)下载haproxy源码包,能够从我提供的网盘连接下载使用:https://pan.baidu.com/s/1I8JCUhejz0VSe8Q4lhzUpQ
提取码:th9xvim
4)web网站使用apache、Nginx、Tomcat搭建均可以,只要能够访问就行,我这里部署两台Nginx和一台Apache,web网站搭建能够参考:
APache网站服务配置访问控制和构建虚拟主机centos
关于Nginx详细配置及说明能够参考:Centos 7部署Nginx网站服务
[root@centos01 ~]# yum -y install prce-devel zlib-devel <!--安装依赖软件包--> [root@centos01 ~]# useradd -M -s /sbin/nologin nginx <!--建立管理Nginx帐户--> [root@centos01 ~]# umount /mnt/ <!--卸载操做系统光盘--> [root@centos01 ~]# mount /dev/cdrom /mnt/ <!--切换Linux光盘--> mount: /dev/sr0 写保护,将以只读方式挂载 [root@centos01 ~]# scp /mnt/nginx-1.6.0.tar.gz root@192.168.100.20:/root <!--复制Nginx包到第二台Nginx服务器--> The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts. root@192.168.100.20's password: <!--输入密码--> nginx-1.6.0.tar.gz 100% 784KB 68.2MB/s 00:00 [root@centos01 ~]# scp /mnt/haproxy-1.4.24.tar.gz root@192.168.100.30:/root <!--复制haproxy软件包到100.30服务器--> The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes <!--输入yes--> Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts. root@192.168.100.30's password: <!--输入密码--> haproxy-1.4.24.tar.gz 100% 817KB 31.1MB/s 00:00 00:00 [root@centos01 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/ <!--解压缩nginx包--> [root@centos01 ~]# cd /usr/src/nginx-1.6.0/ <!--进入nginx目录--> [root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module <!--配置nginx--> [root@centos01 nginx-1.6.0]# make && make install <!--编辑及安装nginx--> [root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--优化执行命令--> [root@centos01 ~]# echo "192.168.100.10:nginx" > /usr/local/nginx/html/index.html <!--建立nginx网站主页,写入测试数据--> [root@centos01 ~]# nginx <!--启动nginx服务--> [root@centos01 ~]# netstat -anptu | grep nginx <!--监听Nginx服务是否启动--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3685/ngin: master
[root@centos02 ~]# yum -y install pcre-devel zlib-devel <!--安装依赖软件包--> [root@centos02 ~]# ls <!--查看Nginx包是否成功从第一台Nginx网站复制成功--> anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz [root@centos02 ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/ <!--解压缩nginx软件包--> [root@centos02 ~]# useradd -M -s /sbin/nologin nginx <!--建立管理nginx帐户--> [root@centos02 ~]# cd /usr/src/nginx-1.6.0/ <!--进入Nginx目录--> [root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module <!--配置nginx--> [root@centos02 nginx-1.6.0]# make && make install <!--编译及安装--> [root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--优化执行路径--> [root@centos02 ~]# echo "192.168.100.20:nginx" > /usr/local/nginx/html/index.html <!--建立nginx网站主页,写入测试数据 --> [root@centos02 ~]# nginx <!--启动nginx服务--> [root@centos02 ~]# netstat -anptu | grep nginx <!--监听Nginx服务是否启动--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6059/ngin: master
<!--安装haproxy--> [root@centos03 ~]# yum -y install pcre-devel bzip2-devel <!--安装依赖程序--> [root@centos03 ~]# ls anaconda-ks.cfg haproxy-1.4.24.tar.gz initial-setup-ks.cfg [root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/ <!--解压缩haproxy压缩包--> [root@centos03 ~]# cd /usr/src/haproxy-1.4.24/ <!---进入haproxy目录-> [root@centos03 haproxy-1.4.24]# make TARGET=linux26<!--编译haproxy支持64位系统--> [root@centos03 haproxy-1.4.24]# make install <!--安装haproxy--> <!--接下来开始生成haproxy配置文件--> [root@centos03 ~]# mkdir /etc/haproxy <!--建立保存haproxy配置文件目录--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/ <!--生成配置文件--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy <!--建立haproxy服务控制脚本--> [root@centos03 ~]# chmod +x /etc/init.d/haproxy <!--添加执行权限--> [root@centos03 ~]# chkconfig --add haproxy <!--添加为系统服务--> [root@centos03 ~]# chkconfig --level 35 haproxy on <!--设置开机自动启动--> [root@centos03 ~]# cp /usr/src/haproxy-1.4.24/haproxy /usr/sbin/<!--优化程序执行命令--> [root@centos03 ~]# mkdir -p /usr/share/haproxy <!--建立服务运行的临时目录--> <!--接下来开始配置haproxy群集--> [root@centos03 ~]# vim /etc/haproxy/haproxy.cfg <!--修改haproxy主配置文件--> # this config needs haproxy-1.1.28 or haproxy-1.2.1 gid 99 <!--用户gid--> daemon #debug #quiet defaults log global <!--定义日志为global配置中的日志定义--> mode http <!--模式为http--> option httplog <!--采用http日志格式记录日志--> option dontlognull retries 3 <!--检查节点服务器失败次数,连续达到三次失败,则认为节点不可用--> redispatch <!--当服务器负载很高时,自动结束当前队列处理比较久的链接--> maxconn 2000 <!--最大链接数--> contimeout 10 <!--链接超时时间--> clitimeout 10 <!--客户端超时时间--> srvtimeout 10 <!--服务器超时时间--> listen nginx 192.168.100.30:80 <!--定义一个nginx的应用--> balance roundrobin <!--负载均衡调度算法使用轮询算法--> server web01 192.168.100.10:80 check inter 2000 fall 3 <!--定义在线节点--> server web02 192.168.100.20:80 check inter 2000 fall 3 <!--定义备份节点--> [root@centos03 ~]# /etc/init.d/haproxy start <!--启动haproxy服务--> Starting haproxy (via systemctl): [ 肯定 ]
至此Haproxy群集部署完成,如今可使用客户端访问Haproxy服务器的IP地址,第一次访问到的页面是第一台Nginx服务器,第二次访问到的页面会是第二台Nginx服务器
我这里就直接配置了,关于DNS详细配置及配置项解释及DNS工做原理概述请参考博文:CentOS7简单搭建DNS服务
[root@centos03 ~]# yum -y install bind bind-chroot bind-utils <!--安装依赖软件--> [root@centos03 ~]# echo "" > /etc/named.conf <!--清空主配置文件--> [root@centos03 ~]# vim /etc/named.conf <!--编辑DNS主配置文件--> options { listen-on port 53 { 192.168.100.0/24; }; directory "/var/named"; }; zone bdqn.com IN { type master; file "bdqn.com.zone"; }; [root@centos03 ~]# named-checkconf -z /etc/named.conf<!--检查DNS主配置文件是否错误--> [root@centos03 ~]# vim /var/named/bdqn.com.zone<!--编辑bdqn.com正向解析区域配置文件--> $TTL 86400 @ SOA bdqn.com. root.bdqn.com.( 2019112201 1H 15M 1W 1D ) @ NS centos03.bdqn.com. centos03 A 192.168.100.30 www A 192.168.100.30 [root@centos03 ~]# named-checkzone bdqn.com /var/named/bdqn.com.zone <!--检查正向解析区域配置文件是否错误--> zone bdqn.com/IN: loaded serial 2019112201 OK [root@centos03 ~]# chmod +x /var/named/bdqn.com.zone <!--正向解析区域配置文件添加执行权限--> [root@centos03 ~]# chown named:named /var/named/bdqn.com.zone <!--修改属组属组--> [root@centos03 ~]# systemctl start named <!--启动服务--> [root@centos03 ~]# systemctl enable named <!--设置服务开机自动启动-->
至此DNS已经配置完成,客户端添加DNS地址,打开浏览器使用域名访问便可
关于Apache网站详细配置及工做原理概述参考博文:CentOS 7.4搭建Apache网站服务
[root@centos04 ~]# tar zxvf /mnt/httpd-2.2.17.tar.gz -C /usr/src/ <!--解压缩httpd压缩包--> [root@centos04 ~]# cd /usr/src/httpd-2.2.17/ [root@centos04 httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so –enable-rewrite --enable-charset-lite --enable-cgi <!--配置apache--> [root@centos04 httpd-2.2.17]# make && make install <!--编辑及安装--> [root@centos04 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ <!--优化执行路径--> [root@centos04 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd<!--建立apache服务--> [root@centos04 ~]# chmod +x /etc/init.d/httpd <!--添加执行权限--> [root@centos04 ~]# vim /etc/init.d/httpd <!--修改apache服务--> #!/bin/sh #chkconfig 35 80 20 #description:apache server [root@centos04 ~]# chkconfig --add httpd <!--添加系统服务--> [root@centos05 ~]# echo "192.168.100.40:apache" > /usr/local/httpd/htdocs/index.html <!--建立网站主页,写入测试数据--> [root@centos04 ~]# apachectl -t <!--检查apache服务配置文件是否错误--> [root@centos04 ~]# systemctl start httpd <!--启动apache服务--> [root@centos04 ~]# systemctl enable httpd <!--设置开机自动启动--> [root@centos04 ~]# netstat -anptu | grep httpd <!--监听Apache服务是否启动--> tcp6 0 0 :::80 :::* LISTEN 53801/httpd [root@centos04 ~]# systemctl is-enabled httpd.service <!--查看apache服务开机自动启动状态--> enabled
listen nginx 192.168.100.30:80 balance roundrobin server web01 192.168.100.10:80 check inter 2000 fall 3 <!--定义在线节点--> server web02 192.168.100.20:80 check inter 2000 fall 3 <!--定义备份节点--> server apache01 192.168.100.40:80 check inter 2000 fall 3 weight 1 <!--定义备份节点(Haproxy主配置文件中添加此行便可)--> [root@centos03 ~]# /etc/init.d/haproxy restart <!--重启动haproxy服务--> Restarting haproxy (via systemctl): [ 肯定 ]
至此基于Apache网站群集已经部署完成,如今可使用客户端访问
[root@centos03 ~]# tail -f /var/log/haproxy/haproxy-info.log <!--查看haproxy访问日志-->
—————— 本文至此结束,感谢阅读 ——————