负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)

一、haproxy+keepalived

haproxy 2台

20.0.0.21

20.0.0.22

nginx 2台

20.0.0.23

20.0.0.24

客户机 1台

20.0.0.30

这里没有haproxy不是集群的概念,他只是代理服务器。

访问他直接可以直接访问后端服务器

关闭防火墙

安装haproxy和环境:

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

tar zxvf haproxy-1.5.19.tar.gz

cd haproxy-1.5.19/

make TARGET=linux2628 ARCH=x86_64

make install

Haproxy服务器配置:

创建haproxy目录,复制haproxy.cfg文件到目录下

mkdir /etc/haproxy

cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/

vim /etc/haproxy/haproxy.cfg

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第1张图片

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第2张图片

 timeout http-request 10s

 timeout queue 1m

 timeout connect 10s

 timeout client 1m

 timeout server 1m

 timeout http-keep-alive 10s

 timeout check 10s

listen test 0.0.0.0:80

        option httpchk GET /index.html

        balance roundrobin

        server inst1 20.0.0.23:80 check inter 2000 fall 3 weight 3

        server inst2 20.0.0.24:80 check inter 2000 fall 3 weight 7

haproxy系统服务的添加 :

cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy

chmod 777 /etc/init.d/haproxy

chkconfig --add /etc/init.d/haproxy

cd /etc/init.d

ln -s /usr/local/sbin/haproxy /usr/sbin

软连接一定要是绝对路径

systemctl restart haproxy

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第3张图片

下载ipvsadm 和 keepalived

配置keepalived:

keepalived不配置指向后端服务器,由haproxy指向。keepalived只做主备切换

vim /etc/keepalived

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第4张图片

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第5张图片

备机scp远程复制keepalived改一下

id要改主备不能一致,BACKUP改,优先级改

先启动ipvsadm 在 启动keepalived

由于这里keepalived没有配置数据指向,而是直接访问后端真实服务器,ipvsadm -ln没有策略

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第6张图片

vim /etc/sysctl.conf

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第7张图片

net.ipv4.ip_forward = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.conf.ens33.send_redirects = 0

编写脚本检测haproxy控制keepalived:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第8张图片

后端RS:

后端RS nginx配置:

后端RS启动nginx 改静态页面index.html

关闭nginx的keepalive_timeout

重启nginx

用客户机curl访问

模拟故障:把主的haproxy服务停止

ip addr

vip到备用机上去了

客户机访问:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第9张图片

故障恢复:先启动主的keepalived再启动主的haproxy

ip addr vip回到主上

访问:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第10张图片

二、LVS+DR模式+keepalived+nginx+tomcat

主:20.0.0.21

备:20.0.0.22

静态nginx 1:20.0.0.23

静态nginx 2:20.0.0.24

动态tomcat1:20.0.0.25

动态tomcat2:20.0.0.26

客户端 1

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第11张图片

lvs-DR主备配置

20.0.0.21

20.0.0.22

安装keepalived

更改keepalived配置文件:

vim /etc/keepalived/keepalived.conf.bak

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第12张图片

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第13张图片

ip addr查看vip

systemctl -p

net.ipv4.ip_forward = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.conf.ens33.send_redirects = 0

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第14张图片

备用调度器远程复制,略微修改

scp [email protected]:/etc/keepalived/keepalived.conf /etc/keepalived/

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第15张图片

设置后端nginx:

配置静态界面:

创建虚拟ip:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第16张图片

DEVICE=lo:0

IPADDR=20.0.0.100

NETMASK=255.255.255.255

ONBOOT=yes

重启网卡,加入路由

ifup ifcfg-lo:0

route add -host 20.0.0.100 dev lo:0

vim /etc/sysctl.conf

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第17张图片

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

去nginx里把连接保持关了

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第18张图片

配置指向动态页面tomcat:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第19张图片

重启nginx

客户机访问20.0.0.100检测静态页面:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第20张图片

配置tomcat:

解压安装Tomcat:

rpm -ivh jdk-8u201-linux-x64.rpm

指定java脚本:

vim /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64

export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第21张图片

source /etc/profile.d/java.sh

检查java版本:

java -version

安装Tomcat:解压即可

tar -xf apache-tomcat-9.0.16.tar.gz

将软件剪切到local目录下面去

mv apache-tomcat-9.0.16 /usr/local/tomcat

进入/usr/local/tomcat

进入bin目录启动Tomcat

浏览器检验:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第22张图片

创建动态页面:

先创建test目录 再创建jsp文件

更改server.xml:

148自带host删除

最后几行host更改:(复制进去要看有没有自动换行什么的)

        

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第23张图片

重启Tomcat

客户机访问:

负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)_第24张图片

模拟故障:将主的keepalived停了

vip跑到备上

访问

故障恢复:将主的keepalived启动

vip回到主上

访问

你可能感兴趣的:(web,负载均衡,lvs,nginx)