首页 > 代码库 > Keepalived Httpd的简单高可用搭建及设定sorry_server

Keepalived Httpd的简单高可用搭建及设定sorry_server


    环境:

    K1:192.168.11.26

    K2:192.168.11.28

    R1:192.168.11.21

    R2:192.168.11.30

    VIP:192.168.11.99


一、K1配置

[root@K1 ~]# cd /etc/keepalived/                    
[root@K1 keepalived]# cat keepalived.conf    #查看配置文件
! Configuration File for keepalived

global_defs {                                #定义全局信息
   notification_email {
	root@localhost
   }
   notification_email_from xxx@163.com        #定义发送者邮箱,这里随意填
   smtp_server 127.0.0.1
   smtp_connect_timeout 30                    #连接超时30秒
   router_id LVS_DEVEL                        #定义id,我们使用默认就好
  # vrrp_mcast_group4 224.0.100.18            #定义组播地址,建议最好使用
}

vrrp_instance VI_1 {                        #定义虚拟路由地址
    state MASTER                            #我们K1为MASTER
    interface eth0                          #在那口网卡,Centos 7 是enoxxxx
    virtual_router_id 51                    #虚拟路由ID,必须一样主从
    priority 100                            #优先级
    advert_int 1                            #默认1秒就好,
    authentication {                        #认真
        auth_type PASS                      #keepalived只支持俩中认证,这里使用第二种简单密码认证,也就是预认证
        auth_pass 6Nb6zjWB                  #使用openssh rant -base64 6 获取随机密码,最长支持八位所以也就是后面使用6的原因
    }
    virtual_ipaddress {                     #定义虚拟IP地址
	192.168.11.17
    }
	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.11.17 80 {            #定义keepalived集群VIP地址
    delay_loop 6                             #检查周期6秒
    lb_algo rr                               #调度算法
    lb_kind DR                               #调度模型
    protocol TCP                             #只支持TCP,所以想想就好
    sorry_server 127.0.0.1 80                #用于集群挂掉了,提示的友好界面,比如提示:服务器正在维护等等友好信息...

    real_server 192.168.11.30 80 {           #定义R1
        weight 1                             #权重,我们是rr调度,权重可以忽略
        HTTP_GET {                           #这里我们使用的HTTP 80端口
            url {                            #定义要请求的url地址
              path /                         #请求地址为/ ,也就是/indexx.html或者index.php等等
              status_code 200                #返回状态码为200表示OK
            }
            connect_timeout 3                #连接超时时长,3秒
            nb_get_retry 3                   #重试3次
            delay_before_retry 3             #每次重试的时间间隔3秒
        }                                
    }
   real_server 192.168.11.21 80 {            #定义R2
        weight 1                             #权重
        HTTP_GET {                           #这里我们使用的HTTP 80端口
            url {                            #定义要请求的url地址
              path /                         #请求地址为/ ,也就是/indexx.html或者index.php等等
              status_code 200                #返回状态码为200表示OK
            }
            connect_timeout 3                #连接超时时长,3秒
            nb_get_retry 3                   #重试3次
            delay_before_retry 3             #每次重试的时间间隔3秒
        }
    }

}

二、K2配置

[root@K2 keepalived]# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from xxx@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
  # vrrp_mcast_group4 224.0.100.18
}

vrrp_instance VI_1 {
    state BACKUP                                            #这里不同,是BACKUP
    interface eth0
    virtual_router_id 51
    priority 98                                             #这里不同,优先级是98,下面其他都一样
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.17
    }
	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.11.17 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80

    real_server 192.168.11.30 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.11.21 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}


三、R2配置

[root@R2 ~]# cat skp.sh                                                     #查看脚本内容
#!/bin/bash
#
vip=192.168.11.17
case $1 in
start)
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
ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
route add -host $vip dev lo:0
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 del $vip
;;
esac
[root@R2 ~]# sh skp.sh start                                                #执行脚本,记得传参进去start
[root@R2 ~]# ip add sh                                                      #查看,配置成功:inet 192.168.11.17/32 brd 192.168.11.17 scope global lo:0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 192.168.11.17/32 brd 192.168.11.17 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16780032: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:c8:1b:d5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.30/24 brd 192.168.11.255 scope global eno16780032
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec8:1bd5/64 scope link 
       valid_lft forever preferred_lft forever
[root@R2 ~]# yum install httpd -y                                           #安装httpd服务
[root@R2 ~]# cat /var/www/html/index.html                                   #标记index.html内容,以做区别,在生产机千万要别这样区别,不然用户会迷茫的!~
<h1>===30===</h1>
[root@R2 ~]# systemctl start httpd                                          #启动httpd服务,这台是Centos 7 启动方式可能有所不同

四、R1配置

[root@R1 ~]# yum install httpd -y                                           #安装httpd服务
[root@R1 ~]# sh skp.sh start                                                #执行脚本,记得传参进去
[root@R1 ~]# ip add sh                                                      #查看,配置成功:inet 192.168.11.17/32 brd 192.168.11.17 scope global lo:0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet 192.168.11.17/32 brd 192.168.11.17 scope global lo:0
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:4e:ce:b9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.21/24 brd 192.168.11.255 scope global eth0
    inet6 fe80::20c:29ff:fe4e:ceb9/64 scope link 
       valid_lft forever preferred_lft forever
[root@R1 ~]# cat /var/www/html/index.html                                   #标记index.html内容,以做区别,在生产机千万要别这样区别,不然用户会迷茫的!~
<h1>===21===</h1>
[root@R1 ~]# service httpd start                                            #启动服务,这是Centos 6 很熟悉吧 ^-^


五、在K2启动,这台是初始是BACKUP优先级是98

[root@K2 keepalived]# service keepalived start                #启动keepalived,提示OK启动成功
Starting keepalived:                                       [  OK  ]
[root@K2 keepalived]# ipvsadm -Ln                      #查看是否成功,发现是OK的
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.11.17:80 rr
  -> 192.168.11.21:80             Route   1      0          0         
  -> 192.168.11.30:80             Route   1      0          0 
[root@K2 ~]# tail -f /var/log/messages                   #这是在启动前,在另外的一个session执行的命令,有兴趣可以看看哦,后面就示例啦                   
Oct 21 22:58:34 redis_master Keepalived[22646]: Starting Keepalived v1.2.13 (03/19,2015)
Oct 21 22:58:34 redis_master Keepalived[22647]: Starting Healthcheck child process, pid=22648
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Netlink reflector reports IP 192.168.11.28 added
Oct 21 22:58:34 redis_master Keepalived[22647]: Starting VRRP child process, pid=22649
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Netlink reflector reports IP fe80::20c:29ff:fe2d:bab0 added
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Registering Kernel netlink reflector
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Registering Kernel netlink command channel
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Netlink reflector reports IP 192.168.11.28 added
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Netlink reflector reports IP fe80::20c:29ff:fe2d:bab0 added
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Registering Kernel netlink reflector
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Registering Kernel netlink command channel
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Registering gratuitous ARP shared channel
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Configuration is using : 63319 Bytes
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: Using LinkWatch kernel netlink reflector...
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Configuration is using : 17292 Bytes
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 21 22:58:34 redis_master Keepalived_vrrp[22649]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Using LinkWatch kernel netlink reflector...
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Activating healthchecker for service [192.168.11.30]:80
Oct 21 22:58:34 redis_master Keepalived_healthcheckers[22648]: Activating healthchecker for service [192.168.11.21]:80
Oct 21 22:58:38 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 21 22:58:39 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 21 22:58:39 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 21 22:58:39 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17
Oct 21 22:58:39 redis_master Keepalived_healthcheckers[22648]: Netlink reflector reports IP 192.168.11.17 added
Oct 21 22:58:44 redis_master Keepalived_vrrp[22649]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17

[root@localhost ~]# curl http://192.168.11.17                #使用其他的机制访问,OK也是我们所设定的rr调度算法
<h1>===21===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===30===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===21===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===30===</h1>

六、启动K1,由于是MASTER 优先级是100 所以VIP地址资源会被抢占到本机

[root@K1 keepalived]# ip add sh                              #我们发现,VIP地址被抢过来了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
       valid_lft forever preferred_lft forever
[root@K1 keepalived]# ipvsadm -Ln                            #服务也是正常的,记得K2使用ipvsadm -Ln 看到的也是一样的
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.11.17:80 rr
  -> 192.168.11.21:80             Route   1      0          0         
  -> 192.168.11.30:80             Route   1      0          0  
[root@localhost ~]# curl http://192.168.11.17                #使用其他的机制访问,OK也是我们所设定的rr调度算法
<h1>===21===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===30===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===21===</h1>
[root@localhost ~]# curl http://192.168.11.17
<h1>===30===</h1>

七、停掉R1/R2服务,看看是否会是我们所希望的那样提示sorry_server的友好界面

[root@K1 keepalived]# cat /var/www/html/index.html      #K1的友好界面提示,为了实验区分,特地+K1

<h1>===localhost--K1==</h1>

[root@K2 keepalived]# cat /var/www/html/index.html      #K2的友好界面提示,为了实验区分,特地+K2

<h1>===localhost--K2===</h1>

[root@R2 ~]# systemctl stop httpd               #停掉R2的httpd服务

[root@K1 keepalived]# ipvsadm -Ln               #K1上查看,192.168.11.30:80的被自动去掉了

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.11.17:80 rr

  -> 192.168.11.21:80             Route   1      0          0 

[root@K2 keepalived]# ipvsadm -Ln              #K2上查看,192.168.11.30:80的被自动去掉了,更K1是一样的

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.11.17:80 rr

  -> 192.168.11.21:80             Route   1      0          0 

[root@R1 ~]# service httpd stop               #停掉R1的httpd服务

Stopping httpd:                                            [  OK  ]

[root@K1 keepalived]# ipvsadm -Ln              #使用了我们的定义的sorry_server

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.11.17:80 rr

  -> 127.0.0.1:80                 Local   1      0          0   

[root@localhost ~]# curl http://192.168.11.17        #如我们所要的结果

<h1>===localhost--K1==</h1>

[root@K1 keepalived]# service keepalived stop        #停掉K1的keepalived

停止 keepalived:                                          [确定]

[root@localhost ~]# curl http://192.168.11.17       #如我们所要的结果,VIP跑去了K2,至此keepalived搭建的简单httpd高可用就搭建OK啦 ^-^

<h1>===localhost--K2===</h1>

 


本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1864430

Keepalived Httpd的简单高可用搭建及设定sorry_server