专为LVS和HA设计的一款健康检查工具
支持故障自动切换(Failover)
支持节点健康状态检查(Health Checking)
官方网站:http://www.keepalived.org
Keepalived配置目录位于/etc/keepalived/
keepalived.conf是主配置文件
常用配置选项
常用配置选项
Keepalived备份服务器的配置与master的配置有三个选项不同
其他选项与master相同
【1】全局配置、热备配置
【2】Web服务器池配置
【1】从调度器的配置与主调度器基本相同
【1】DR调度服务器 192.168.100.11 keepalived ipvsadm
【3】DR调度服务器 192.168.100.33 keepalived ipvsadm
【5】节点服务器 192.168.100.55 httpd
【6】节点服务器 192.168.100.66 httpd
【2】win 10客户端 192.168.100.20
漂移地址:192.168.100.10
——————————————DR调度服务器——————————————————
【1】
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install keepalived ipvsadm -y
[root@localhost ~]# 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
[root@localhost ~]# sysctl -p ###即时生效
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0 ###修改VIP地址
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# vim ifcfg-ens33 ###修改本地网卡地址
IPADDR=192.168.100.11
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
##########本地的虚拟机改为仅主机模式
#### 配置LVS-DR模式
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10
RIP1=192.168.100.55
RIP2=192.168.100.66
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 "ipvsadm stoped -------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit
else
echo "ipvsadm Runnig --------[ok]"
fi
;;
*)
echo "Usage: $0 [start|stop|status]"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x dr.sh
[root@localhost init.d]# service network restart
[root@localhost init.d]# ifconfig ###有ens33和ens33:0
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# service dr.sh start
【3】
[root@localhost ~]# yum install keepalived ipvsadm -y
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.ens33.send_redirects=0
[root@localhost ~]# sysctl -p
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0 ###ens33本地IP地址,ens33:0 VIP
[root@localhost network-scripts]# vim ifcfg-ens33:0 ###修改VIP地址
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# vim ifcfg-ens33 ###修改本地网卡地址
IPADDR=192.168.100.33
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
##########本地的虚拟机改为仅主机模式
#### 配置LVS-DR模式
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10
RIP1=192.168.100.55
RIP2=192.168.100.66
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 "ipvsadm stoped -------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit
else
echo "ipvsadm Runnig --------[ok]"
fi
;;
*)
echo "Usage: $0 [start|stop|status]"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x dr.sh
[root@localhost init.d]# service network restart
[root@localhost init.d]# ifconfig
ens33: 192.168.100.33
ens33:0: 192.168.100.10
[root@localhost init.d]# service dr.sh start
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# systemctl stop firewalld.service
———————————节点服务器——————————————
###先把两个节点网卡改为仅主机模式
【5】
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# vim ifcfg-ens33
IPADDR=192.168.100.55
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
———本地yum源安装httpd-----也可以直接NAT模式yum install httpd -y
[root@localhost network-scripts]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mkdir bak
[root@localhost yum.repos.d]# mv CentOS-* bak/
[root@localhost yum.repos.d]# vim abc.repo
[abc]
name=test
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@localhost yum.repos.d]# mount /dev/sr0 /mnt
[root@localhost yum.repos.d]# df -Th
[root@localhost yum.repos.d]# yum install httpd -y
———————————————————————————
[root@localhost yum.repos.d]# cd /etc/init.d/
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.10
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
[root@localhost init.d]# chmod +x web.sh
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# service network restart
[root@localhost init.d]# ifconfig ###有ens33和lo:0
[root@localhost init.d]# service web.sh start
[root@localhost init.d]# systemctl start httpd
[root@localhost init.d]# cd /var/www/html
[root@localhost html]# vim index.html
this is aaa web
【6】
[root@localhost ~]# yum install httpd -y
###先把两个节点网卡改为仅主机模式
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@localhost network-scripts]# vim ifcfg-ens33
IPADDR=192.168.100.66
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
[root@localhost yum.repos.d]# cd /etc/init.d/
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.10
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
[root@localhost init.d]# service network restart
[root@localhost init.d]# ifconfig
[root@localhost init.d]# systemctl start httpd.service
[root@localhost init.d]# systemctl stop firewalld.service
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# cd /var/www/html/
[root@localhost html]# vim index.html
this is bbb web
#####keepalived部署(在调度服务器上设置)
[root@localhost init.d]# cd /etc/keepalived/ ###进入keepalived配置文件站点目录
[root@localhost keepalived]# vim keepalived.conf
10 smtp_server 127.0.0.1 ###邮件服务指向本地
...
12 router_id LVS_01 ###指定名称,备份服务器不同名称
...
20 state MASTER ###备份服务器是BACKUP
21 interface ens33
22 virtual_router_id 51 ###组号相同
23 priority 100 ###优先级备份小于主
...
29 virtual_ipaddress {
30 192.168.100.10
31 }
...
34 virtual_server 192.168.100.10 80 {
...
37 lb_kind DR ###LVS模式
...
41 real_server 192.168.100.55 80 {
42 weight 1
43 TCP_CHECK {
44 connect_port 80 ###添加端口
45 connect_timeout 3
46 nb_get_retry 3
47 delay_before_retry 3
48 }
49 }
50 real_server 192.168.100.66 80 {
###9yy ,p粘贴
51 weight 1
52 TCP_CHECK {
53 connect_port 80
54 connect_timeout 3
55 nb_get_retry 3
56 delay_before_retry 3
57 }
58 }
59 }
###下面的virtual_server全部删除
[root@localhost keepalived]# systemctl start keepalived.service
###把本地的配置文件复制到第二台服务器
[root@localhost keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/
【3】
[root@localhost init.d]# vim /etc/keepalived/keepalived.conf
router_id LVS_02 ###router_id不相同02,角色身份为BACKUP,优先级90
state BACKUP
priority 90
[root@localhost keepalived]# systemctl start keepalived.service
####验证结果
###Win10 设为仅主机模式,网络ip改为100网段:192.168.100.20
网页搜索192.168.100.10
刷新