永利酒店赌场项目实战3—Keepalived 实现高可用,实战keepalived

后端real_server准备

修改192.168.234.57的vip为10.0.0.200/32

  1 [root@234c27 keepalived]# ipvsadm -Ln
  2 IP Virtual Server version 1.2.1 (size=4096)
  3 Prot LocalAddress:Port Scheduler Flags
  4   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  5 TCP  10.0.0.100:80 wrr
  6   -> 192.168.234.47:80            Route   1      0          0
  7 TCP  10.0.0.200:80 wrr
  8   -> 192.168.234.57:80            Route   1      0          0

永利酒店赌场 1

现在宕掉一个lvs

  1 [root@234c27 keepalived]# systemctl stop keepalived.service
  2 [root@234c27 keepalived]# ipvsadm -Ln
  3 IP Virtual Server version 1.2.1 (size=4096)
  4 Prot LocalAddress:Port Scheduler Flags
  5   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  6 

永利酒店赌场 2

依然提供服务

  1 [root@234c37 ~]# ipvsadm -Ln
  2 IP Virtual Server version 1.2.1 (size=4096)
  3 Prot LocalAddress:Port Scheduler Flags
  4   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  5 TCP  10.0.0.100:80 wrr
  6   -> 192.168.234.47:80            Route   1      0          21
  7 TCP  10.0.0.200:80 wrr
  8   -> 192.168.234.57:80            Route   1      0          39

后一个实现基于前一个的基础上修改来的

keepalived工作原理

4、在real server 上

(1) 开启事前准备好的web服务

systemctl start nginx

systemctl start mariadb

systemctl start php-fpm

 

(2)因为是DR模式,需在rs上设置

① 配置VIP到本地回环网卡lo上,并只广播自己

ifconfig lo:0 172.17.100.100 broadcast 172.17.100.100 netmask
255.255.255.255 up

ifconfig lo:1 172.17.100.101 broadcast 172.17.100.101 netmask
255.255.255.255 up

配置本地回环网卡路由

route add -host 172.17.100.100 lo:0

route add -host 172.17.100.101 lo:1

 

② 使RS “闭嘴”

echo “1” > /proc/sys/net/ipv4/conf/lo/arp_ignore

echo “2” > /proc/sys/net/ipv4/conf/lo/arp_announce

忽略ARP广播

echo “1” > /proc/sys/net/ipv4/conf/all/arp_ignore

echo “2” > /proc/sys/net/ipv4/conf/all/arp_announce

注意:关闭arp应答

1:仅在请求的目标IP配置在本地主机的接收到请求报文的接口上时,才给予响应

2:必须避免将接口信息向非本网络进行通告

 

③ 想永久生效,可以写到配置文件中

vim /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

 

修改主机:192.168.234.27的keepalived配置文件

[root@234c27 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 kpone
   vrrp _mcast_group4 234.10.10.10
}

vrrp_instance VI_1 {
    state MASTER
    interface ens37
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.100/24
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens37
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        10.0.0.200/24
    }
}
virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP
    #sorry_server 127.0.0.1:80
    real_server 192.168.234.47 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
virtual_server 10.0.0.200 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP
    #sorry_server 127.0.0.1:80
    real_server 192.168.234.57 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

永利酒店赌场 3

实验四:实现keeaplived 故障通知机制

单主模型IPVS示例

  [root@234c27 keepalived]# ipvsadm -Ln
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
   -> RemoteAddress:Port      Forward Weight ActiveConn InActConn
  TCP 10.0.0.100:80 wrr
   -> 192.168.234.47:80      Route  1   0     0
  TCP 10.0.0.200:80 wrr
  -> 192.168.234.57:80      Route  1   0     0

5、测试

(1)lvs负载均衡作用是否开启

客户端访问 公网172.17.100.100只能访问80

永利酒店赌场 4

公网172.17.100.101只能访问443

永利酒店赌场 5

也可以详细测试

① 在rs1 上设置一个测试一面

vim /data/web/test.html

real server 1

 

② 在rs2 上设置一个测试一面

vim /data/web/test.html

real server 2

 


网页访问
发现有real server 1也有real server 2

永利酒店赌场 6

 

(2)测试keepalived的双主方式

① 使keepalive 的任意一个宕机

service keepalived stop

 

会发现服务能照常访问,另一个机器80、443都能访问,且宕机的VIP漂移到了另一个服务器上

永利酒店赌场 7

  

查看keepalived

[root@234c37 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
…………
[root@234c37 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
//暂无ipvsadm

安装httpd服务 写好网页文件

实验一:实现keepalived主从方式高可用基于LVS-DR模式的应用实战:

实验原理:

主从:一主一从,主的在工作,从的在休息;主的宕机了,VIP漂移到从上,由从提供服务

启动服务

永利酒店赌场 8

现在宕掉一个lvs

实验二:实现keepalived双主方式高可用基于LVS-DR模式的应用实战:

实验原理:

互为主从:主从都在工作;其中一个宕机了,VIP漂移到另一个上,提供服务

增加ip在网卡上 修改限制arp通告及应答级别 rs1 rs2都做,网关并指向路由

ip a a 10.0.0.100/32 dev ens37

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

route add default gw 192.168.234.17

安装httpd服务 写好网页文件

永利酒店赌场 9

2、在keepalived 的配置文件调用脚本

在instance 实例段添加,注意脚本的路径

notify_backup "/etc/keepalived/notify.sh backup"
notify_master "/etc/keepalived/notify.sh master"
notify_fault "/etc/keepalived/notify.sh fault"

例:

永利酒店赌场 10

  

启动服务

[root@234c27 keepalived]# systemctl start keepalived.service
[root@234c27 keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-08-31 20:30:02 CST; 12s ago
  Process: 9657 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 9658 (keepalived)
………………
[root@234c27 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.100:80 wrr
  -> 192.168.234.47:80            Route   1      0          0
  -> 192.168.234.57:80            Route   2      0          0
//启动服务lvs vs已配置好

让10.0.0.100的ip优先分配至192.168.234.47 192.168.234.57备用

4、在real server 上

(1) 开启事前准备好的web服务

systemctl start nginx

systemctl start mariadb

systemctl start php-fpm

 

(2)因为是DR模式,需在rs上设置

① 配置VIP到本地回环网卡lo上,并只广播自己

ifconfig lo:0 172.17.100.100 broadcast 172.17.100.100 netmask
255.255.255.255 up

配置本地回环网卡路由

route add -host 172.17.100.100 lo:0

 

② 使RS “闭嘴”

echo “1” > /proc/sys/net/ipv4/conf/lo/arp_ignore

echo “2” > /proc/sys/net/ipv4/conf/lo/arp_announce

忽略ARP广播

echo “1” > /proc/sys/net/ipv4/conf/all/arp_ignore

echo “2” > /proc/sys/net/ipv4/conf/all/arp_announce

注意:关闭arp应答

1:仅在请求的目标IP配置在本地主机的接收到请求报文的接口上时,才给予响应

2:必须避免将接口信息向非本网络进行通告

 

③ 想永久生效,可以写到配置文件中

vim /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

  

多主模型IPVS示例

永利酒店赌场 11

后一个实现基于前一个的基础上修改来的

2、在lvs-server-master 主上

修改keepalived主(lvs-server-master)配置文件实现 virtual_instance 实例

(1)vim /etc/keepalived/keepalived.conf 修改三段

① 全局段,故障通知邮件配置
global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id keepalived_lvs
}

② 配置虚拟路由器的实例段,VI_1是自定义的实例名称,可以有多个实例段
vrrp_instance VI_1 {     #VI_1是自定义的实例名称
    state MASTER        #初始状态,MASTER|BACKUP
    interface eth1      #通告选举所用端口
    virtual_router_id 51  #虚拟路由的ID号(一般不可大于255)
    priority 100       #优先级信息 #备节点必须更低
    advert_int 1       #VRRP通告间隔,秒
    authentication {
        auth_type PASS    #认证机制
        auth_pass along   #密码(尽量使用随机)
    } 
    virtual_ipaddress {
        172.17.100.100    #vip
    }
}

③ 设置一个virtual server段
virtual_server 172.17.100.100 80 {   #设置一个virtual server:
    delay_loop 6   # service polling的delay时间,即服务轮询的时间间隔
    lb_algo wrr    #LVS调度算法:rr|wrr|lc|wlc|lblc|sh|dh
    lb_kind DR    #LVS集群模式:NAT|DR|TUN
    nat_mask 255.255.255.255  
    persistence_timeout 600  #会话保持时间(持久连接,秒),即以用户在600秒内被分配到同一个后端realserver
    protocol TCP    #健康检查用的是TCP还是UDP

④ real server设置段
    real_server 172.17.1.7 80 { #后端真实节点主机的权重等设置
        weight 1  #给每台的权重,rr无效
        HTTP_GET {  #http服务
            url {
              path /
            }
            connect_timeout 3    #连接超时时间
            nb_get_retry 3      #重连次数
            delay_before_retry 3 #重连间隔
        }
    }
    real_server 172.17.22.22 80 {
        weight 2
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

(2)开启keepalived 服务

service keepalived start

能看到网卡别名 和 负载均衡策略已经设置好了

ipvsadm -Ln

永利酒店赌场 12

 

(3)因为是主从方式,所以从上的配置和主只有一点差别;所以可以把这个配置文件拷过去

scp /etc/keepalived/keepalived.conf @172.17.11.11:

  

配置keepalive

高可用的ipvs集群示例:修改keepalived配置文件

增加ip在网卡上 修改限制arp通告及应答级别
rs1 rs2都做,网关并指向路由

方案一:检测是否存在down文件,来实现主从的调整

1、在实例段上边定义一个脚本

vim keepalived.conf

vrrp_script chk_down {    #定义一个脚本,脚本名称为chk_down
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"   #检查这个down文件,若存在返回值为1,keepalived会停止;不存在返回值为0,服务正常运行;这里的exit和bash脚本里的return很相似
 interval 2   #每2秒检查一次
}

2、在instance 实例段可以直接调用这个脚本

track_script {
    chk_down
}

 

3、检测

在主上,创建一个/etc/keepalived/down
文件,主的keepalived服务立刻停止,VIP漂到从上,从接上服务;

down文件一旦删除,主的keepalived服务会立即启动,若优先级高或优先级低但设置的抢占,VIP会重漂回来,接上服务。

 

修改主机:192.168.234.37的keepalived配置文件

[root@234c37 ~]# 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 kptwo
   vrrp _mcast_group4 234.10.10.10
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens37
    virtual_router_id 50
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.100/24
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens37
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        10.0.0.200/24
    }
}
virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP
    #sorry_server 127.0.0.1:80
    real_server 192.168.234.47 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
virtual_server 10.0.0.200 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP
    #sorry_server 127.0.0.1:80
    real_server 192.168.234.57 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

让10.0.0.100的ip优先分配至192.168.234.47 192.168.234.57备用

让10.0.0.200的ip优先分配至192.168.234.57 192.168.234.47备用

配置keepalive

实验三:实现keepalived主从方式高可用基于LVS-NAT模式的应用实战:

实验原理:

主从:一主一从,主的在工作,从的在休息;主的宕机了,VIP和DIP都漂移到从上,由从提供服务,因为DIP需被rs作为网关,所以也需漂移

后端real_server准备

 [root@234c37 ~]# ipvsadm -Ln
 IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port      Forward Weight ActiveConn InActConn
  TCP 10.0.0.100:80 wrr
   -> 192.168.234.47:80      Route  1   0     21
  TCP 10.0.0.200:80 wrr
   -> 192.168.234.57:80      Route  1   0     39

方案二:检测nginx服务是否开启,来实现调整主从

1、在实例段上边定义一个脚本

vrrp_script chk_nginx {
     script "killall -0 nginx" #killall -0 检测这个进程是否还活着,不存在就减权重
     interval 2 #每2秒检查一次
     fall 2 #失败2次就打上ko的标记
     rise 2 #成功2次就打上ok的标记
     weight -4 #权重,优先级-4,若为ko
}

 

2、在instance 实例段可以直接调用这个脚本

track_script {
    chk_nginx
}

 

3、检测

若主的nginx服务没有开启,则每2秒-4的权重,当优先级小于从,VIP漂到从上,从接上服务;

若主的nginx服务开启,重读配置文件,优先级恢复,VIP回到主上,主恢复服务;

  

实现高可用,实战keepalived
实现基于Keepalived高可用集群网站架构
环境:随着业务的发展,网站的访问量越来越大,网站…

修改主机:192.168.234.27的keepalived配置文件

  1 [root@234c27 ~]# vim /etc/keepalived/keepalived.conf
  2 ! Configuration File for keepalived
  3 
  4 global_defs {
  5 notification_email {
  6 root@localhost  //接受邮件地址
  7 }
  8 notification_email_from keepalived@localhost  //发送邮件地址
  9 smtp_server 127.0.0.1  //发送邮件服务器IP
 10 smtp_connect_timeout 30  //邮件连接超时时长
 11 router_id kptwo  //路由id
 12 vrrp _mcast_group4 234.10.10.10  //指定vrrp协议的多播地址
 13 }
 14 
 15 vrrp_instance VI_1 {  //vrrp协议的
 16 state MASTER  //lvs的MASTER服务器
 17 interface ens37  //
 18 virtual_router_id 50  //虚拟路由
 19 priority 100  //权重为100.越大越先
 20 advert_int 1  //发送组博包的间隔
 21 authentication {  //验证
 22 auth_type PASS  //方式为pass( 明文)
 23 auth_pass 1111  //密码
 24 }
 25 virtual_ipaddress { //keepalived虚拟ip
 26 10.0.0.100/24
 27 }
 28 }
 29 virtual_server 10.0.0.100 80 {
 30     delay_loop 6  //检查后端服务器的时间间隔
 31     lb_algo wrr  //定义调度方法
 32     lb_kind DR  //集群的类型
 33     #persistence_timeout 50  //持久连接时长
 34     protocol TCP  //服务协议,仅支持TCP
 35     real_server 192.168.234.47 80 {  //后端real_server服务器地址
 36         weight 1 //权重
 37         HTTP_GET {  //应用层检测
 38             url {
 39               path /  //定义要监控的URL
 40               status_code 200  //判断上述检测机制为健康状态的响应码
 41             }
 42             connect_timeout 3  //连接请求的超时时长
 43             nb_get_retry 3  //重试次数
 44             delay_before_retry 3  //重试之前的延迟时长
 45         }
 46     }
 47     real_server 192.168.234.57 80 {
 48         weight 2
 49         HTTP_GET {
 50             url {
 51                 path /
 52                 status_code 200
 53             }
 54             connect_timeout 3
 55             nb_get_retry 3
 56             delay_before_retry 3
 57         }
 58     }
 59 }

多主模型IPVS示例

1、编写好脚本

脚本主要内容:检测到主从发生变化,或错误,给谁发邮件;邮件内容是:在什么时间,谁发生了什么变化

vim /etc/keepalived/notify.sh

#!/bin/bash
# Author: www.magedu.com
contact='[email protected]'
notify() {
        mailsubject="$(hostname) to be $1: vip floating"
        mailbody="$(date +'%F %H:%M:%S'): vrrp transition, $(hostname) changed to be $1"
        echo $mailbody | mail -s "$mailsubject" $contact
}
case $1 in
master) 
        notify master
        exit 0
;;
backup)
        notify backup
        exit 0
;;
fault)
        notify fault
        exit 0
;;
*)
        echo "Usage: $(basename $0) {master|backup|fault}"
        exit 1
;;
esac

脚本加权限 chmod +x /etc/keepalived/notify.sh

 

配置keepalive

高可用的ipvs集群示例:修改keepalived配置文件

1.把rs服务都停掉。然后在lvs上安装apache或者nginx服务

实验五:实现keepaplived自定义脚本检测功能

原理:在keepalived的配置文件中能直接定义脚本,且能在instance
实例段直接调用生效

 

修改主机:192.168.234.37的keepalived配置文件

[root@234c37 ~]# 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 kptwo
   vrrp _mcast_group4 234.10.10.10
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens37
    virtual_router_id 50
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.100/24
    }
}
virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP
    sorry_server 127.0.0.1:80
    real_server 192.168.234.47 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.234.57 80 {
        weight 2
        HTTP_GET {
            url {
              path /
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

2.将keepalived配置文件中的

5、测试

(1)lvs负载均衡作用是否开启

客户端访问

永利酒店赌场 13

也可以详细测试

① 在rs1 上设置一个测试一面

vim /data/web/test.html

real server 1

 

② 在rs2 上设置一个测试一面

vim /data/web/test.html

real server 2

 

③ 网页访问 发现有real server 1也有real
server 2

永利酒店赌场 14

 

 

(2)测试keepalived的主从方式

① 使keepalive 的主宕机

service keepalived stop

 

会发现服务能照常访问,但是VIP 和DIP 都漂移到了从上

从多了网卡别名,且地址是VIP

永利酒店赌场 15

 

③ 使keepalive
的主重新开启服务,因为主的优先级高,所以VIP和DIP又重新漂移到了主上

永利酒店赌场 16

 

假设要实现sorry_server

1.把rs服务都停掉。然后在lvs上安装apache或者nginx服务

2.将keepalived配置文件中的

  1 virtual_server 10.0.0.200 80 {
  2     delay_loop 6
  3     lb_algo wrr
  4     lb_kind DR
  5     #persistence_timeout 50
  6     protocol TCP
  7     #sorry_server 127.0.0.1:80  //这一行来修改 写出服务出错之后的页面
  8     real_server 192.168.234.57 80 {
  9         weight 1
 10         HTTP_GET {
 11             url {
 12               path /
 13               status_code 200
 14             }
 15             connect_timeout 3
 16             nb_get_retry 3
 17             delay_before_retry 3
 18         }
 19     }
 20 }
[root@234c37 ~]# 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 kptwo
  vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
  state BACKUP
  interface ens37
  virtual_router_id 50
  priority 80
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    10.0.0.100/24
  }
}
vrrp_instance VI_2 {
  state MASTER
  interface ens37
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 2222
  }
  virtual_ipaddress {
    10.0.0.200/24
  }
}
virtual_server 10.0.0.100 80 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  #persistence_timeout 50
  protocol TCP
  #sorry_server 127.0.0.1:80
  real_server 192.168.234.47 80 {
    weight 1
    HTTP_GET {
      url {
       path /
       status_code 200
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}
virtual_server 10.0.0.200 80 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  #persistence_timeout 50
  protocol TCP
  #sorry_server 127.0.0.1:80
  real_server 192.168.234.57 80 {
    weight 1
    HTTP_GET {
      url {
       path /
       status_code 200
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}

4、在real server 上

(1) 开启事前准备好的web服务

systemctl start nginx

systemctl start mariadb

systemctl start php-fpm

 

(2)因为是NAT模式,需在rs上设置

只需把网关指向DIP

route add default gw 192.168.30.100

 

永利酒店赌场 17

您可能感兴趣的文章:

  • LVS+Keepalived构建高可用负载均衡(测试篇)
  • LVS+Keepalived构建高可用负载均衡配置方法(配置篇)
  • Linux下的高可用性方案研究

5、测试

(1)lvs负载均衡作用是否开启

客户端访问

永利酒店赌场 18

也可以详细测试

① 在rs1 上设置一个测试一面

vim /data/web/test.html

real server 1

 

② 在rs2 上设置一个测试一面

vim /data/web/test.html

real server 2

 

③ 网页访问 发现有real server 1也有real
server 2

永利酒店赌场 19

 

(2)测试keepalived的主从方式

① 使keepalive 的主宕机

service keepalived stop

 

会发现服务能照常访问,但是VIP 漂移到了从上

从多了网卡别名,且地址是VIP

永利酒店赌场 20

 

③ 使keepalive
的主重新开启服务,因为主的优先级高,所以VIP又重新漂移到了主上

永利酒店赌场 21

 

  [root@234c27 keepalived]# systemctl stop keepalived.service
  [root@234c27 keepalived]# ipvsadm -Ln
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port      Forward Weight ActiveConn InActConn

2、在lvs-server1 上,基本同上,就是加了一个实例段

修改keepalived主(lvs-server-master)配置文件实现 virtual_instance 实例

(1)vim /etc/keepalived/keepalived.conf

① 主的设置 VI_1

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }
    virtual_ipaddress {
        172.17.100.100
    }
}

virtual_server 172.17.100.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.255
    persistence_timeout 600
    protocol TCP

    real_server 172.17.1.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.17.22.22 80 {
        weight 1
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

② 从的设置 VI_2

vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 52
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }
    virtual_ipaddress {
        172.17.100.101
    }
}

virtual_server 172.17.100.101 443 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.255
    persistence_timeout 600
    protocol TCP

    real_server 172.17.1.7 443 {
        weight 1
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.17.22.22 443 {
        weight 1
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

(2)开启keepalived 服务

service keepalived start

能看到网卡别名 和 负载均衡策略已经设置好了

永利酒店赌场 22

ipvsadm -Ln

永利酒店赌场 23

 

(3)因为是主从方式,所以从上的配置和主只有一点差别;所以可以把这个配置文件拷过去

scp /etc/keepalived/keepalived.conf @172.17.11.11:

 

[root@234c37 ~]# 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 kptwo
 vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
 state BACKUP
 interface ens37
 virtual_router_id 50
 priority 80
 advert_int 1
 authentication {
  auth_type PASS
  auth_pass 1111
 }
 virtual_ipaddress {
  10.0.0.100/24
 }
}
virtual_server 10.0.0.100 80 {
 delay_loop 6
 lb_algo wrr
 lb_kind DR
 #persistence_timeout 50
 protocol TCP
 sorry_server 127.0.0.1:80
 real_server 192.168.234.47 80 {
  weight 1
  HTTP_GET {
   url {
    path /
    status_code 200
   }
   connect_timeout 3
   nb_get_retry 3
   delay_before_retry 3
  }
 }
 real_server 192.168.234.57 80 {
  weight 2
  HTTP_GET {
   url {
    path /
    status_code 200
   }
   connect_timeout 3
   nb_get_retry 3
   delay_before_retry 3
  }
 }
}

3、在lvs-server2 上,基本同1,就是把实例的主从调换一下

(1)vim /etc/keepalived/keepalived.conf

① vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }
    virtual_ipaddress {
        172.17.100.100
    }
}
② vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }
    virtual_ipaddress {
        172.17.100.101
    }
}

 

(2)开启keepalived 服务

service keepalived start

能看到网卡别名 和 负载均衡策略已经设置好了,显示结果会等段时间再显示

永利酒店赌场 24

ipvsadm -Ln,显示结果会等段时间再显示

永利酒店赌场 25

 

启动服务

项目实战3—Keepalived 实现高可用,实战keepalived

永利酒店赌场 26

实现基于Keepalived高可用集群网站架构

  环境:随着业务的发展,网站的访问量越来越大,网站访问量已经从原来的1000QPS,变为3000QPS,目前业务已经通过集群LVS架构可做到随时拓展,后端节点已经通过集群技术保障了可用性,但对于前端负载均衡器来说,是个比较大的安全隐患,因为当前端负载均衡器出现故障时,整个集群就处于瘫痪状态,因此,负载均衡器的可用性也显得至关重要,那么怎么来解决负载均衡器的可用性问题呢?

 总项目流程图,详见

实验前准备

① 两台服务器都使用yum 方式安装keepalived 服务

yum -y install keepalived

② iptables -F && setenforing 清空防火墙策略,关闭selinux

 

LVS是Linux Virtual
Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR),十种调度算法。

2、在lvs-server-master 主上

(1)vim keepalived.conf

global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id keepalived_lvs
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }
    virtual_ipaddress {
       172.17.100.100
192.168.30.100
    }
}

virtual_server 172.17.100.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind NAT
    nat_mask 255.255.255.255
    persistence_timeout 100
    protocol TCP

    real_server 192.168.30.107 80 {
        weight 1
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.30.7 80 {
        weight 2
        HTTP_GET {
            url {
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

(2)因为是NAT模式,所以需开启路由转发功能

vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

 

sysctl -p 读一些,使参数生效

 

(3)开启keepalived 服务

service keepalived start

能看到网卡别名 和 负载均衡策略已经设置好了

永利酒店赌场 27

ipvsadm -Ln

永利酒店赌场 28

 

(4)因为是主从方式,所以从上的配置和主只有一点差别;所以可以把这个配置文件拷过去

scp /etc/keepalived/keepalived.conf @172.17.11.11:

 

3、在lvs-server-backup 从上

(1)只需改②实例段,其他都不要变,保证一模一样

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }

 

 

(2)因为是NAT模式,所以需开启路由转发功能

① vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

② sysctl -p 读一些,使参数生效

 

(3)开启keepalived 服务

service keepalived start

负载均衡策略已经设置好了,注意:主director没有宕机,从上就不会有VIP

ipvsadm -Ln 可能过一会才会显示

永利酒店赌场 29

 

后端real_server准备

3、在lvs-server-backup 从上

(1)只需改②实例段,其他都不要变,保证一模一样

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass along
    }

 

(2)开启keepalived 服务

service keepalived start

负载均衡策略已经设置好了,注意:主director没有宕机,从上就不会有VIP

ipvsadm -Ln 可能过一会才会显示

永利酒店赌场 30

 

依然提供服务

1、环境准备:

两台centos系统做DR、一主一从,两台实现过基于LNMP的电子商务网站

机器名称

lvs-server-master

lvs-server-backup

rs01

rs02

  

修改主机:192.168.234.27的keepalived配置文件

1、实验环境,基本同上

机器名称

lvs-server-1

lvs-server2

rs01

rs02

 

永利酒店赌场 31

1、环境准备

机器名称

vs-server-master

lvs-server-backup

rs01

rs02

注意:要确保rs和DIP在一个网段,且不和VIP在一个网段

 

本文重点给大家介绍Linux利用keepalived实现lvs的高可用性的操作方法,感兴趣的朋友参考下吧。

配置keepalive

image

virtual_server 10.0.0.200 80 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  #persistence_timeout 50
  protocol TCP
  #sorry_server 127.0.0.1:80 //这一行来修改 写出服务出错之后的页面
  real_server 192.168.234.57 80 {
    weight 1
    HTTP_GET {
      url {
       path /
       status_code 200
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}

查看keepalived

单主模型IPVS示例

让10.0.0.200的ip优先分配至192.168.234.57 192.168.234.47备用

永利酒店赌场 32

 LVS简介

ip a a 10.0.0.100/32 dev ens37
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
route add default gw 192.168.234.17

永利酒店赌场 33

修改主机:192.168.234.27的keepalived配置文件

假设要实现sorry_server

lvs+keepalived是什么?

修改主机:192.168.234.37的keepalived配置文件

[root@234c27 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 kpone
  vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
  state MASTER
  interface ens37
  virtual_router_id 50
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    10.0.0.100/24
  }
}
vrrp_instance VI_2 {
  state BACKUP
  interface ens37
  virtual_router_id 51
  priority 80
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 2222
  }
  virtual_ipaddress {
    10.0.0.200/24
  }
}
virtual_server 10.0.0.100 80 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  #persistence_timeout 50
  protocol TCP
  #sorry_server 127.0.0.1:80
  real_server 192.168.234.47 80 {
    weight 1
    HTTP_GET {
      url {
       path /
       status_code 200
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}
virtual_server 10.0.0.200 80 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  #persistence_timeout 50
  protocol TCP
  #sorry_server 127.0.0.1:80
  real_server 192.168.234.57 80 {
    weight 1
    HTTP_GET {
      url {
       path /
       status_code 200
      }
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
    }
  }
}
[root@234c27 ~]# 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 //发送邮件服务器IP
smtp_connect_timeout 30 //邮件连接超时时长
router_id kptwo //路由id
vrrp _mcast_group4 234.10.10.10 //指定vrrp协议的多播地址
}
vrrp_instance VI_1 { //vrrp协议的
state MASTER //lvs的MASTER服务器
interface ens37 //
virtual_router_id 50 //虚拟路由
priority 100 //权重为100.越大越先
advert_int 1 //发送组博包的间隔
authentication { //验证
auth_type PASS //方式为pass( 明文)
auth_pass 1111 //密码
}
virtual_ipaddress { //keepalived虚拟ip
10.0.0.100/24
}
}
virtual_server 10.0.0.100 80 {
 delay_loop 6 //检查后端服务器的时间间隔
 lb_algo wrr //定义调度方法
 lb_kind DR //集群的类型
 #persistence_timeout 50 //持久连接时长
 protocol TCP //服务协议,仅支持TCP
 real_server 192.168.234.47 80 { //后端real_server服务器地址
  weight 1 //权重
  HTTP_GET { //应用层检测
   url {
    path / //定义要监控的URL
    status_code 200 //判断上述检测机制为健康状态的响应码
   }
   connect_timeout 3 //连接请求的超时时长
   nb_get_retry 3 //重试次数
   delay_before_retry 3 //重试之前的延迟时长
  }
 }
 real_server 192.168.234.57 80 {
  weight 2
  HTTP_GET {
   url {
    path /
    status_code 200
   }
   connect_timeout 3
   nb_get_retry 3
   delay_before_retry 3
  }
 }
}

启动服务

keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy
Protocol,即虚拟路由冗余协议。
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。

修改主机:192.168.234.37的keepalived配置文件

后端real_server准备

修改192.168.234.57的vip为10.0.0.200/32

[root@234c27 keepalived]# systemctl start keepalived.service
[root@234c27 keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
 Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
 Active: active (running) since Fri 2018-08-31 20:30:02 CST; 12s ago
 Process: 9657 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 9658 (keepalived)
………………
[root@234c27 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port   Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 wrr
 -> 192.168.234.47:80   Route 1  0   0
 -> 192.168.234.57:80   Route 2  0   0
//启动服务lvs vs已配置好

高可用的ipvs集群示例:修改keepalived配置文件

[root@234c37 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
 Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
 Active: inactive (dead)
…………
[root@234c37 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port   Forward Weight ActiveConn InActConn
//暂无ipvsadm

高可用的ipvs集群示例:修改keepalived配置文件

网站地图xml地图