本文共 7430 字,大约阅读时间需要 24 分钟。
Keepalived起初是专门针对LVS设计的一款强大的辅助工具,主要是用来提供故障切换和健康检查功能——判断LVS负载调度器、节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机回复后将其重新加入群集
主机 | 操作系统 | IP地址 |
---|---|---|
DR1主调度服务器 | CentOS 7.0 x86_64 | 192.168.100.156 |
DR2备调度服务器 | CentOS 7.0 x86_64 | 192.168.100.157 |
web1节点服务器 | CentOS 7.0 x86_64 | 192.168.100.133 |
web2节点服务器 | CentOS 7.0 x86_64 | 192.168.100.134 |
VIP虚拟IP地址 | -------- | 192.168.100.10 |
Win 7 客户端 | Windows 7 | 192.168.100.50 |
1.安装keepalived、ipvsadm软件包 yum install keepalived ipvsadm -y2.修改/proc响应参数 vi /etc/sysctl.confnet.ipv4.ip_forward=1 #开启路由转发功能net.ipv4.conf.all.send_redirects = 0 #proc响应关闭重定向功能net.ipv4.conf.default.send_redirects = 0net.ipv4.conf.ens33.send_redirects = 03.立即生效 sysctl -p4.创建虚拟VIP cd /etc/sysconfig/network-scripts/ cp ifcfg-ens33 ifcfg-ens33:05.编辑虚拟网卡 vim ifcfg-ens33:0DEVICE=ens33:0ONBOOT=yesIPADDR=192.168.100.10NETMASK=255.255.255.06.启用虚拟网卡ifup ifcfg-ens33:07.制作dr.sh脚本文件 vim /etc/init.d/dr.sh#!/bin/bashGW=192.168.100.1 #网关地址VIP=192.168.100.10 #VIP虚拟地址RIP1=192.168.100.133 #web1节点服务器IPRIP2=192.168.100.134 #web2节点服务器IPcase "$1" instart)/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm #保存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 -gecho "ipvsadm starting --------------------[ok]";;stop)/sbin/ipvsadm -Csystemctl stop ipvsadmifconfig ens33:0 downroute del $VIPecho "ipvsamd stoped----------------------[ok]";;status)if [ ! -e /var/lock/subsys/ipvsadm ];thenecho "ipvsadm stoped---------------"exit 1elseecho "ipvsamd Runing ---------[ok]"fi;;*)echo "Usage: $0 {start|stop|status}"exit 1esacexit 0
8.给dr.sh脚本赋执行权限,启动脚本chmod +x dr.shservice dr.sh start9.查看验证lvs节点状态 ipvsadm -ln10.修改keepalived配置文件如下vim /etc/keepalived/keepalived.confglobal_defs {...smtp_server 127.0.0.1 #指向本地router_id LVS_02 #指定服务器名称... }vrrp_instance VI_1 {state BACKUP #主份服务器是Masterinterface ens33virtual_router_id 10 #主备组号相同priority 99 #优先级备服务器小于主服务器... auth_pass abc123 #验证密码,主备相同...virtual_ipaddress {192.168.100.10 #虚拟VIP地址}...virtual_server 192.168.100.10 80 { #虚拟服务器VIP、端口...lb_kind DR...real_server 192.168.100.133 80 { #真实web1节点服务器IP、端口weight 1TCP_CHECK {connect_port 80 connect_timeout 3nb_get_retry 3delay_before_retry 3} } real_server 192.168.100.134 80 { #真实web2节点服务器IP、端口weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3} } }11.启动keepalived服务 systemctl start keepalived
1.安装keepalived、ipvsadm软件包 yum install keepalived ipvsadm -y2.修改/proc响应参数 vi /etc/sysctl.confnet.ipv4.ip_forward=1 #开启路由转发功能net.ipv4.conf.all.send_redirects = 0 #proc响应关闭重定向功能net.ipv4.conf.default.send_redirects = 0net.ipv4.conf.ens33.send_redirects = 03.立即生效 sysctl -p4.制作dr.sh脚本文件 vim /etc/init.d/dr.sh#!/bin/bashGW=192.168.100.1 #网关地址VIP=192.168.100.10 #VIP虚拟地址RIP1=192.168.100.133 #web1节点服务器IPRIP2=192.168.100.134 #web2节点服务器IPcase "$1" instart)/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm #保存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 -gecho "ipvsadm starting --------------------[ok]";;stop)/sbin/ipvsadm -Csystemctl stop ipvsadmifconfig ens33:0 downroute del $VIPecho "ipvsamd stoped----------------------[ok]";;status)if [ ! -e /var/lock/subsys/ipvsadm ];thenecho "ipvsadm stoped---------------"exit 1elseecho "ipvsamd Runing ---------[ok]"fi;;*)echo "Usage: $0 {start|stop|status}"exit 1esacexit 0
5.给dr.sh脚本赋执行权限,启动脚本chmod +x dr.shservice dr.sh start6.查看验证lvs节点状态 ipvsadm -ln7.修改keepalived配置文件如下vim /etc/keepalived/keepalived.confglobal_defs {...smtp_server 127.0.0.1 #指向本地router_id LVS_02 #指定服务器名称... }vrrp_instance VI_1 {state MASTER #备份服务器是BACKUPinterface ens33virtual_router_id 10 #主备组号相同priority 100 #优先级备服务器小于主服务器... auth_pass abc123 #验证密码,主备相同...virtual_ipaddress {192.168.100.10 #虚拟VIP地址}...virtual_server 192.168.100.10 80 { #虚拟服务器VIP、端口...lb_kind DR...real_server 192.168.100.133 80 { #真实web1节点服务器IP、端口weight 1TCP_CHECK {connect_port 80 connect_timeout 3nb_get_retry 3delay_before_retry 3} } real_server 192.168.100.134 80 { #真实web2节点服务器IP、端口weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3} } }8.启动keepalived服务 systemctl start keepalived
1.安装httpd软件包yum install -y httpd2.启动httpd服务systemctl start httpd.service 3.新建网站首页文件echo "this is benet
" > /var/www/html/index.html4.新建web.sh脚本至服务管理目录,便于service进行管理vim /etc/init.d/web.sh#!/bin/bashVIP=192.168.100.10 #VIP虚拟IP地址case "$1" instart)ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP/sbin/route add -host $VIP dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p >/dev/null 2>&1echo "RealServer Start OK ";;stop)ifconfig lo:0 downroute del $VIP /dev/null 2>&1echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announceecho "RealServer Stopd";;*)echo "Usage: $0 {start|stop}"exit 1esacexit 05.给web.sh脚本赋执行权限chmod +x web.sh6.启动web.sh服务service web.sh start7.关闭防火墙及selinuxsystemctl stop firewalld.service setenforce 0
8.测试
1.安装httpd软件包yum install -y httpd2.启动httpd服务systemctl start httpd.service 3.新建网站首页文件echo "this is accp web
" > /var/www/html/index.html4.新建web.sh脚本至服务管理目录,便于service进行管理vim /etc/init.d/web.sh#!/bin/bashVIP=192.168.100.10 #VIP虚拟IP地址case "$1" instart)ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP/sbin/route add -host $VIP dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p >/dev/null 2>&1echo "RealServer Start OK ";;stop)ifconfig lo:0 downroute del $VIP /dev/null 2>&1echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announceecho "RealServer Stopd";;*)echo "Usage: $0 {start|stop}"exit 1esacexit 05.给web.sh脚本赋执行权限chmod +x web.sh6.启动web.sh服务service web.sh start7.关闭防火墙及selinuxsystemctl stop firewalld.service setenforce 0
8.测试
转载于:https://blog.51cto.com/13620950/2156927