一、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
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
下载ipvsadm 和 keepalived
配置keepalived:
keepalived不配置指向后端服务器,由haproxy指向。keepalived只做主备切换
vim /etc/keepalived
备机scp远程复制keepalived改一下
id要改主备不能一致,BACKUP改,优先级改
先启动ipvsadm 在 启动keepalived
由于这里keepalived没有配置数据指向,而是直接访问后端真实服务器,ipvsadm -ln没有策略
vim /etc/sysctl.conf
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:
后端RS:
后端RS nginx配置:
后端RS启动nginx 改静态页面index.html
关闭nginx的keepalive_timeout
重启nginx
用客户机curl访问
模拟故障:把主的haproxy服务停止
ip addr
vip到备用机上去了
客户机访问:
故障恢复:先启动主的keepalived再启动主的haproxy
ip addr vip回到主上
访问:
二、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
lvs-DR主备配置
20.0.0.21
20.0.0.22
安装keepalived
更改keepalived配置文件:
vim /etc/keepalived/keepalived.conf.bak
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
备用调度器远程复制,略微修改
scp root@20.0.0.21:/etc/keepalived/keepalived.conf /etc/keepalived/
设置后端nginx:
配置静态界面:
创建虚拟ip:
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
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里把连接保持关了
配置指向动态页面tomcat:
重启nginx
客户机访问20.0.0.100检测静态页面:
配置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
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
浏览器检验:
创建动态页面:
先创建test目录 再创建jsp文件
更改server.xml:
148自带host删除
最后几行host更改:(复制进去要看有没有自动换行什么的)
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
重启Tomcat
客户机访问:
模拟故障:将主的keepalived停了
vip跑到备上
访问
故障恢复:将主的keepalived启动
vip回到主上
访问