单主keepalived
#环境:6台机器,Client:172.16.0.6 Router:172.16.0.7、192.168.37.7 RS1:192.168.37.17、192.168.37.100 RS2:192.168.37.27、192.168.37.100 ka1:192.168.37.37、192.168.37.100 ka2:192.168.37.47、192.168.37.100
#router2块网卡,桥接与nat模式
#router
nmcli connection modify eth0 ipv4.addresses 192.168.37.7/24
nmcli connection modify eth1 ipv4.addresses 172.16.0.7/24
nmcli connection up eth0
nmcli connection up eth0
#ka1
echo "192.168.37.37 ka1" >> /etc/hosts
echo "192.168.37.47 ka2" >> /etc/hosts
ssh-keygen
ssh-copy-id 192.168.37.47
yum install keepalived
cd /etc/keepalived
cp keepalived.conf{,.bak}
vim keepalived.conf #先清空
global_defs {
notification_email {
root@localhost #发给本机root邮件
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1 #发邮件的地址
smtp_connect_timeout 30
router_id ka1 #主机名
vrrp_mcast_group4 224.0.0.100 #D类地址,多播
}
vrrp_instance VI_1 { #虚拟路由器
state MASTER #在另一个结点上为BACKUP
interface eth0 #网卡接口
virtual_router_id 10 #多个节点必须相同
priority 100 #优先级,在另一个结点上要小于这个值
advert_int 1 #通告间隔1s
authentication {
auth_type PASS #预共享密钥认证
auth_pass 123456 #密码
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vim /etc/keepalived/notify.sh
#!/bin/bash
contact='root@localhost'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
scp notify.sh ka2:/etc/keepalived/
scp keepalived.conf ka2:/etc/keepalived/
systemctl start keepalived
#ks2
echo "192.168.37.37 ka1" >> /etc/hosts
echo "192.168.37.47 ka2" >> /etc/hosts
ssh-keygen
ssh-copy-id 192.168.37.37
yum install keepalived
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka2
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 10
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
systemctl start keepalived
#抓包查看
tcpdump -i eth0 -nn host 224.0.0.100
单主IPVS
#ka1
vim keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.37.100 80 { #LVS调度器
delay_loop 5 #检查后端服务器的时间间隔
lb_algo rr #调度方法rr|wrr|sh|dh|lc|wlc|sed|nq|lblc|lblcr
lb_kind DR #集群的类型NAT|DR|TUN
persistence_timeout 50 #持久连接时长
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.37.17 80 {
weight 1
HTTP_GET {
url {
path / #定义要监控的URL
status_code 200 #定义健康状态码
}
connect_timeout 1 #连接请求的超时时长
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试之前的延迟时长
}
}
real_server 192.168.37.27 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
}
yum install httpd
echo "Sorry Server 1" > /var/www/html/index.html
systemctl restart keepalived httpd
#ka2
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 10
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.37.100 80 {
delay_loop 5
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.37.17 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.27 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
}
yum install httpd
echo "Sorry Server 2" > /var/www/html/index.html
systemctl restart keepalived
#RS1、RS2
yum install httpd -y
echo `hostname` > /var/www/html/index.html
systemctl start httpd
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ip address add 192.168.37.100/32 dev lo label lo:1
#client
vim /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
NAME="eth2"
IPADDR=172.16.0.6
PREFIX=24
service network restart
while true;do curl 192.168.37.100;sleep 0.5;done
双主keepalived
#ka1
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 20
priority 60
advert_int 1
authentication {
auth_type PASS
auth_pass 654321
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:2
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
systemctl restart keepalived.service
#ka2
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 10
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 20
priority 70
advert_int 1
authentication {
auth_type PASS
auth_pass 654321
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:2
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
systemctl restart keepalived.service
#抓包查看
tcpdump -i eth0 -nn host 224.0.0.100
双主IPVS
#ka1
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 20
priority 60
advert_int 1
authentication {
auth_type PASS
auth_pass 654321
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:2
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.37.100 80 {
delay_loop 5
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.37.17 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.27 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
}
systemctl restart keepalived.service
#ka2
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.0.0.100
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 10
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 20
priority 70
advert_int 1
authentication {
auth_type PASS
auth_pass 654321
}
virtual_ipaddress {
192.168.37.100/24 dev eth0 label eth0:2
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.37.100 80 {
delay_loop 5
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.37.17 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.27 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
}
}
}
systemctl restart keepalived.service
#client
while true;do curl 192.168.37.100;sleep 0.5;done
网友评论