标签:sbin 火墙 abc dex ipad algo 防火 虚拟服务器 yum仓库
Keepalived 的设计目标是搭建高可用的 LVS 负载均衡群集,可以调用 ipvsadm 工具来创建虚拟服务器、管理服务器池,而不仅仅用作双机热备。使用 Keepalived 搭建 LVS 群集更加简便易用,主要优势体现在:对 LVS 负载调度器实现热备切换,提高可用性;对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入。
本次我们以 DR 模式的 LVS 群集为基础,增加一台从负载调度器,使用 Keepalived 来实现主、从调度器的热备,从而构建兼有负载均衡、高可用两种能力的 LVS 网站群集平台。
后续上传
DR1主调度服务器:192.160.100.200
DR2备调度服务器:192.168.100.201
web1节点服务器:192.168.100.202
web2节点服务器:192.168.100.203
虚拟 VIP 服务器:192.168.100.100
nfs服务器:192.168.100.233
Win10客户端:192.168.100.10
检查是否安装nfs以及rpcbind软件包
rpm -q nfs-utils rpcbind
如果没有安装则用yum仓库进行安装
yum -y install nfs-utils rpcbind
开启服务
systemctl start rpcbind
systemctl start nfs
创建挂载点,给文件目录设置权限(实际应用中,我们还需创建raid卷并挂载到目录中以便使用)
mkdir -p /opt/web1 /opt/web2
chmod 777 /opt/web1 /opt/web2
编辑配置文件vim /etc/exports
/opt/web1 192.168.100.0/24(rw,sync) ##在此网段下的用户可以读写、同步目录中的文件
/opt/web2 192.168.100.0/24(rw,sync)
发布共享
exportfs -rv
此时我们可以对客户端进行查看
showmount -e 192.168.100.233
setenforce 0
systemctl disable firewalld
systemctl stop firewalld
安装Apache
yum -y install httpd
挂载 nfs 到Apache主页存放
mount.nfs 192.168.100.206:/opt/web1 /var/www/html
创建默认首页文件
echo "<h1>This is web1 page.</h1>" > /var/www/html/index.html
创建虚拟网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0 ##设置参数如下
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
cd /etc/init.d/
vim web.sh ##添加参数如下
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
chmod +x web.sh ##给予执行权限
service web.sh start
ifup lo:0 ##启动网卡
setenforce 0
systemctl disable firewalld
systemctl stop firewalld
systemctl enable httpd
systemctl start httpd
安装Apache
yum -y install httpd
挂载 nfs 到Apache主页存放
mount.nfs 192.168.100.233:/opt/web1 /var/www/html
创建默认首页文件
echo "<h1>This is web2 page.</h1>" > /var/www/html/index.html
创建虚拟网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0 ##设置参数如下
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
cd /etc/init.d/
vim web.sh ##添加参数如下
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
chmod +x web.sh ##给予执行权限
service web.sh start
ifup lo:0 ##启动网卡
setenforce 0
systemctl disable firewalld
systemctl stop firewalld
systemctl enable httpd
systemctl start httpd
安装 ipvsadm 、keepalived 软件包
yum -y install ipvsadm keepalived
调整 proc 响应参数
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
#proc响应关闭重定向功能
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
配置 VIP 承载接口
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33:0 ##参数如下
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ifup ens33:0 ##启用虚拟IP
cd /etc/init.d/
vim dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.100
RIP1=192.168.100.202
RIP2=192.168.100.203
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
chmod +x dr.sh
service dr.sh start
cd /etc/keepalived/
vim keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 #指向本地
router_id LVS_01 #指定名称,备份服务器不同名称
...
}vrrp_instance VI_1 {
state MASTER #备份服务器是BACKUP
interface ens33 #承接的端口
virtual_router_id 10 #主从调度器组号相同
...
auth_pass abc123 #验证密码
priority 100 #优先级,备份要小于主调度器
...
virtual_ipaddress {
192.168.100.100
}
...
virtual_server 192.168.100.100 80 {
delay_loop 6
lb_algo rr ##轮询算法
lb_kind DR ##DR模式
persistence_timeout 50
protocol TCPreal_server 192.168.100.202 80 { weight 1 ##权重为1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.100.203 80 { weight 1 TCP_CHECK { connect_port 80 #添加端口 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }
}
systemctl start keepalived
在通过客户机的浏览器中,能够通过群集的 VIP 地址(192.168.100.100)来正常访问 Web1 & Web2 页面。当主、从调度器有任一失效时,我们仍可以正常访问 Web ,且只要节点服务器存在两台及以上,我们仍可以实现负载均衡。
标签:sbin 火墙 abc dex ipad algo 防火 虚拟服务器 yum仓库
原文地址:http://blog.51cto.com/13625676/2141090