Lvs
高可用:
在Centos7和Centos6系统中都可以使用keepalived作为高可用
高可用是一样的;
负载均衡:
Centos6 建议使用piranha,
Centos7 使用keepalived
LVS-NAT数据包走向
LVS-DR数据包走向
在centos6环境部署LVS集群
准备工作
PIP 172.16.0.111
SIP 172.16.0.112
RIP1 172.16.0.121
RIP2 172.16.0.122
VIP 172.16.0.110
配置hosts
[root@6web1 ~]# tail -4 /etc/hosts
172.16.0.121 6web1.up.com 6web1
172.16.0.122 6web2.up.com 6web2
172.16.0.111 6PIP.up.com 6PIP
172.16.0.112 6SIP.up.com 6SIP
1. 配置RIP
[root@6web1 ~]# yum install -y httpd
[root@6web2 ~]# yum install -y httpd
[root@6web1 ~]# echo "172.16.0.121-RIP1" > /var/www/html/index.html
[root@6web1 ~]# /etc/init.d/httpd start
[root@6web1 ~]# chkconfig httpd on
[root@6web2 ~]# echo "172.16.0.122-RIP2" > /var/www/html/index.html
[root@6web2 ~]# /etc/init.d/httpd start
[root@6web2 ~]# chkconfig httpd on
服务开机自启动
绑定VIP和配置ARP防火墙:
[root@6web1 ~]# lftp 172.16.0.99
lftp 172.16.0.99:~> cd scripts/
lftp 172.16.0.99:/scripts> get RIP-arptables.sh
[root@6web1 ~]# vim RIP-arptables.sh
#!/bin/bash
VIP=172.16.0.110
RIP=172.16.0.121
[root@6web1 ~]# chmod +x RIP-arptables.sh
[root@6web1 ~]# scp RIP-arptables.sh 172.16.0.122:/root/
[root@6web2 ~]# vim RIP-arptables.sh
#!/bin/bash
VIP=172.16.0.110
RIP=172.16.0.122
[root@6web1 ~]# yum install -y arptables_jf
[root@6web2 ~]# yum install -y arptables_jf
执行脚本
[root@6web1 ~]# ./RIP-arptables.sh
将脚本设置开机自启,否则vip的虚拟网卡只是临时启动
[root@6web1 ~]# echo "/root/RIP-arptables.sh" >> /etc/rc.local
执行脚本
[root@6web2 ~]# ./RIP-arptables.sh
将脚本设置开机自启,否则vip的虚拟网卡只是临时启动
[root@6web2 ~]# echo "/root/RIP-arptables.sh" >> /etc/rc.local
[root@6web1 ~]# ifconfig eth0:1
[root@6web2 ~]# ifconfig eth0:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:6B:CE:3A
inet addr:172.16.0.110 Bcast:172.16.0.110 Mask:255.255.255.255
[root@6web1 ~]# arptables -L
[root@6web2 ~]# arptables -L
2. 配置PIP
[root@6PIP ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@6PIP ~]# sysctl -p
下载"食人鱼"工具
[root@6PIP ~]# yum install -y piranha
设置"食人鱼"登录密码
[root@6PIP ~]# piranha-passwd
New Password:
Verify:
Adding password for user piranha
启动"食人鱼"页面配置服务 GUI服务
[root@6PIP ~]# /etc/init.d/piranha-gui start
浏览器访问:http://172.16.0.111:3636/
用户名:piranha
密码:自定义的
页面配置
当配置name选项时,不能有特殊字符
heartbeat 心跳时间6秒
ASSUME 18秒判断
Re-entry Time: 由15 改为 重试时间也会缩短
Service timeout 由6 改为2 切换时间会缩短,否则切换时间会太长
探测脚本
写一个探测RIP是否可以访问的脚本:
[root@6PIP ~]# yum install -y elinks
[root@6PIP ~]# vim testlink
#!/bin/bash
# 探测RIP是否可以访问
/usr/bin/links -dump 1 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "ok"
else
echo "fail"
fi
测试脚本,访问rip测试
[root@6PIP ~]# chmod +x testlink
[root@6PIP ~]# ./testlink 172.16.0.121
ok
[root@6PIP ~]# ./testlink 172.16.0.122
ok
设置脚本硬连接到bin
[root@6PIP ~]# ln testlink /bin/
将testlink拷贝给sip的bin目录下,因为sip主机也需要
[root@6PIP ~]# scp testlink 172.16.0.112:/bin/
在sip下载脚本需要的工具elinks
[root@6SIP ~]# yum install -y elinks
在sip测试脚本可用性
[root@6SIP ~]# testlink 172.16.0.121
ok
[root@6SIP ~]# testlink 172.16.0.122
ok
页面信息的配置文件路径
[root@6PIP ~]# cd /etc/sysconfig/ha/
[root@6PIP /etc/sysconfig/ha]# ls
conf logs lvs.cf modules web
[root@6PIP /etc/sysconfig/ha]# vim lvs.cf
3. 配置SIP
在sip上先安装“食人鱼”工具
[root@6SIP ~]# yum install -y piranha
将页面配置文件传给sip到对应目录
[root@6PIP /etc/sysconfig/ha]# scp lvs.cf 172.16.0.112:/etc/sysconfig/ha/
pip启动"食人鱼"基本服务
[root@6PIP ~]# /etc/init.d/pulse start
[root@6PIP ~]# chkconfig pulse on
sip启动"食人鱼"基本服务
[root@6SIP ~]# /etc/init.d/pulse start
[root@6SIP ~]# chkconfig pulse on
用ifconfig查看vip地址,可能在pip,也可能在sip
[root@6PIP ~]# ifconfig eth0:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:D4:E3:CD
inet addr:172.16.0.110 Bcast:172.16.255.255 Mask:255.255.0.0
测试:
-
测试RIP
-
测试RIP 和SIP
测试脚本
直接在$1后边添加:8080端口号即可
#!/bin/bash
# 探测RIP-tomcat是否可以访问
/usr/bin/links -dump 1 $1:8080 &> /dev/null
if [ $? -eq 0 ]
then
echo "ok"
else
echo "fail"
fi
故障参考
单机服务节点偶尔访问不了,或者只能访问单机节点
检查pip,sip节点的ipconfig,查看vip在哪一段,vip在哪一端时ipvsadm -L -n就可以查到设备信息。
检查各自需要启动的服务是否设置了开机自启动设置,比如pulse,piranha-gui(页面服务),服务端节点需要自动启动的脚本如arptables.sh是否添加到/etc/rc.local文件中。
服务类,nginx,tomcat,mysql,httpd
是否添加在/etc/rc.local文件中。
Centos6 /etc/init.d/$service start/restart/stop
设置开机自启动
/chkconfig $service on
Centos7 /etc/init.d/$service start/restart/stop
设置开机自启动
systemctl enable $service
搭建集中存储的时候,启动nfs服务时提示Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
解决办法就是重新启动rpcbind服务即可:
service rpcbind restart
(rpmbind负责端口资源分配,想要重新启动nfs,比如让rpcbind重新加载一遍即可)
service nfs restart