配置keepalived实现Nginx高可用(单主、双主模式)

配置keepalived实现Nginx高可用(单主、双主模式)

一、前置条件:

(1).Http作为后端web服务器:192.168.88.130:80 test.field.com

[[email protected]]# cat /var/www/html/index.html

<h1>Welcometo Http on test.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

(2).Http作为后端web服务器:192.168.88.129:80 web2.field.com

[root@web2 ~]#cat /var/www/html/index.html

<h1>Welcometo Http on web2.field.com!!</h1>

<p>If yousee this page,test ok. </p>

<p><em>Thankyou!</em></p>

(3).nginx作为负载均衡服务器:192.168.88.130:8080  test.field.com

Nginx配置:实现简单负载均衡

Fascgi配置部分可直接删除

[root@testnginx]# vi nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type application/octet-stream;

    log_format main  '$remote_addr - $remote_user[$time_local] "$request" $http_host '

                      '$status $body_bytes_sent"$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log  main;

#    proxy_cache_path  /cache/nginx/ levels=1:2keys_zone=mycache:32m;  

    fastcgi_cache_path /cache/fastcgi/levels=1:1 keys_zone=fcgicache:10m inactive=3m max_size=1g;

    upstream upservers {

        server 192.168.88.129 weight=1;

        server 192.168.88.130 weight=1;

    }

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;

}

[root@test conf.d]#vi default.conf

server {

    listen      8080;

    server_name localhost;

    add_header X-Via $server_addr;         

    add_header X-Cache$upstream_cache_status;   

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       proxy_pass http://upservers/;   

    }  

    location /field/ {

    # proxy_cache mycache;

    # proxy_cache_valid 200 1d;

    # proxy_cache_valid 301 302 10m;

    # proxy_cache_valid any 1m;

    # proxy_cache_use_stale error http_500http_502 http_503 http_504;

     proxy_pass http://upservers/;

     proxy_set_header Host      $host;

     proxy_set_header X-Real-IP $remote_addr;

     proxy_set_header        X-Forwarded-For$proxy_add_x_forwarded_for;

    } 

    location ~* \.(jpg|png|gif)$ {

    # proxy_cache mycache;

    # proxy_cache_valid 200 1d;

    # proxy_cache_valid 301 302 10m;

    # proxy_cache_valid any 1m;

    # proxy_cache_use_stale error http_500 http_502 http_503 http_504;

     proxy_pass http://upservers;

     proxy_set_header X-Real-IP $remote_addr;

    }

    #error_page 404              /404.html;

    # redirect server error pages to the staticpage /50x.html

    #

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

       root   /usr/share/nginx/html;

    }

    # proxy the PHP scripts to Apache listeningon 127.0.0.1:80

    #

    #location ~ \.php$ {

    #   proxy_pass   http://127.0.0.1;

    #}

    # pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000

    #

    location ~ \.php$ {

#       fastcgi_cache off;

#开关fastcgi_cache

       fastcgi_cache fcgicache;

#fastcgi_cache:定义使用哪个缓存空间

       fastcgi_cache_key $host$request_uri;

#fastcgi_cache_key:定义fastcgi_cache的key,该例中以请求的URI作为缓存的key,Nginx会取这个key的md5作为缓存文件,如果设置了缓存哈希目录,Nginx会从后往前取相应的位数做为目录

        fastcgi_cache_min_uses  1;

#fastcgi_cache_min_uses:URL经过多少次请求将被缓存

       fastcgi_cache_use_stale error  timeout invalid_header http_500;

#fastcgi_cache_use_stale:定义哪些情况下可以使用过期缓存

       fastcgi_cache_valid 200 10m;

#fastcgi_cache_valid:定义哪些http头要使用缓存

       fastcgi_cache_valid 302 3m;

       fastcgi_cache_valid any 1m;      

        root  /usr/share/nginx/html;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        include        fastcgi_params;

    }

    # deny access to .htaccess files, ifApache's document root

    # concurs with nginx's one

    #

    #location ~ /\.ht {

    #   deny  all;

    #}

}

(4).nginx作为负载均衡服务器:192.168.88.131:8080  www.field.com

Nginx配置:实现简单负载均衡

[root@wwwnginx]# vi nginx.conf

    upstream upservers {

        server 192.168.88.129 weight=1;

        server 192.168.88.130 weight=1;

    }

[root@wwwnginx]#

[[email protected]]# vi default.conf

    location / {

       proxy_pass http://upservers/;

    }

 

二、配置keepalived实现Nginx高可用

案例1、配置keepalived实现Nginx高可用

[root@testkeepalived]# service keepalived stop;ssh www 'service keepalived stop'

停止 keepalived:[确定]

停止 keepalived:[确定]

[root@testkeepalived]# service keepalived status;ssh www 'service keepalived status'

keepalived 已停

keepalived 已停

scp到129web服务器,实现免密ssh

[root@test ~]#scp -p .ssh/id_rsa .ssh/authorized_keys web2:/root/.ssh

root@web2'spassword:

id_rsa                                                                                                                              100% 1675     1.6KB/s   00:00   

authorized_keys                                                                                                                      100%  401    0.4KB/s   00:00   

[root@test ~]#ssh web2

Last login: SunApr 22 23:45:43 2018 from test.field.com

编辑简单通知脚本

[root@testkeepalived]# vi notify.sh

#!/bin/bash

# Author:MageEdu <[email protected]>

# description:An example of notify script

#

vip=192.168.88.80

contact='root@localhost'

notify() {

    mailsubject="`hostname` to be $1: $vipfloating"

    mailbody="`date '+%F %H:%M:%S'`: vrrptransition, `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

[root@testkeepalived]# ll

总用量 24

-rw-r--r-- 1root root 4564 4月  23 00:05 keepalived.conf

-rw-r--r-- 1root root 3562 4月  22 16:23 keepalived.conf.backup

-rw-r--r-- 1root root 4212 4月  22 23:59 keepalived.conf.backup01

-rw-r--r-- 1root root  619 4月  2300:08 notify.sh

注意修改脚本权限,否则无法下发邮件

[root@test keepalived]# chmod +xnotify.sh

[root@test keepalived]#vi keepalived.conf

! ConfigurationFile for keepalived

global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from kaadmin@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id test.field.com

   vrrp_mcast_group4 224.18.0.200

}

vrrp_scriptchk_maintanance {

        script "[[ -f /etc/keepalived/down]] && exit 1 || exit 0"

        interval 1

#interval多长时间检查一次

#如果失败返回1权重-2

        weight -2

}

vrrp_script chk_nginx {

        script "killall -0 nginx &>/dev/null"

        interval 1

#interval多长时间检查一次

#如果失败返回1权重-2

        weight -5

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 4e78bb3a

    }

    virtual_ipaddress {

        192.168.88.80/16 dev eth0 label eth0:0

    }

track_script {

#track_script 调用脚本

        chk_nginx

}

        notify_master"/etc/keepalived/notify.sh master"

        notify_backup "/etc/keepalived/notify.shbackup"

        notify_fault"/etc/keepalived/notify.sh fault"

}

[root@test keepalived]# scpnotify.sh www:/etc/keepalived/

notify.sh                                                                                                                           100%  619    0.6KB/s   00:00   

[root@test keepalived]# scpkeepalived.conf www:/etc/keepalived/

keepalived.conf

修改备机配置文件                                                                                                                    100% 4564     4.5KB/s   00:00   

[root@wwwkeepalived]# vi keepalived.conf

! ConfigurationFile for keepalived

global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from root@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

  router_id www.field.com

   vrrp_mcast_group4 224.18.0.200

}

vrrp_scriptchk_maintanance {

        script "[[ -f /etc/keepalived/down]] && exit 1 || exit 0"

        interval 1

#interval多长时间检查一次

#如果失败返回1权重-2

        weight -2

}

vrrp_scriptchk_nginx {

        script "killall -0 nginx &>/dev/null"

        interval 1

#interval多长时间检查一次

#如果失败返回1权重-2

        weight -5

}

vrrp_instance VI_1 {

    state BACKUP

   interface eth1

    virtual_router_id 51

   priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 4e78bb3a

    }

    virtual_ipaddress {

        192.168.88.80/16 dev eth1label eth1:0

    }

track_script {

#track_script 调用脚本

        chk_nginx

}

        notify_master "/etc/keepalived/notify.shmaster"

        notify_backup"/etc/keepalived/notify.sh backup"

        notify_fault"/etc/keepalived/notify.sh fault"

}

[root@testkeepalived]# service keepalived start;ssh www 'service keepalived start'

正在启动 keepalived:[确定]

正在启动 keepalived:[确定]

[root@testkeepalived]# service keepalived start;ssh www 'service keepalived start'

正在启动 keepalived:[确定]

正在启动 keepalived:[确定]

[root@testkeepalived]# mail

Heirloom Mailversion 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/root":20 messages 1 new 14 unread

......

>N 20root                  Mon Apr 2323:08  18/687   "test.field.com to be master:192.168.88.80 floating"

& 20

Message 20:

From [email protected] Mon Apr 23 23:08:19 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:08:14 +0800

To: [email protected]

Subject: test.field.com to be master: 192.168.88.80 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:08:13: vrrp transition, test.field.com changed to be master

& q

Held 20 messagesin /var/spool/mail/root

You have mail in/var/spool/mail/root

[root@testkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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:4c:04:cc brdff:ff:ff:ff:ff:ff

    inet 192.168.88.130/24 brd 192.168.88.255scope global eth0

    inet 192.168.88.80/16 scope global eth0:0

    inet6 fe80::20c:29ff:fe4c:4cc/64 scope link

       valid_lft forever preferred_lft forever

[root@test ~]#tail -f /var/log/keepalived.log

Apr 23 23:08:11test Keepalived[3853]: Starting Keepalived v1.2.13 (03/19,2015)

Apr 23 23:08:11test Keepalived_healthcheckers[3855]: Initializing ipvs 2.6

Apr 23 23:08:11test Keepalived[3854]: Starting Healthcheck child process, pid=3855

Apr 23 23:08:11test Keepalived[3854]: Starting VRRP child process, pid=3856

Apr 23 23:08:11test Keepalived_vrrp[3856]: Netlink reflector reports IP 192.168.88.130 added

Apr 23 23:08:11test Keepalived_vrrp[3856]: Netlink reflector reports IPfe80::20c:29ff:fe4c:4cc added

Apr 23 23:08:11test Keepalived_vrrp[3856]: Registering Kernel netlink reflector

Apr 23 23:08:11test Keepalived_vrrp[3856]: Registering Kernel netlink command channel

Apr 23 23:08:11test Keepalived_vrrp[3856]: Registering gratuitous ARP shared channel

Apr 23 23:08:11test Keepalived_vrrp[3856]: Opening file '/etc/keepalived/keepalived.conf'.

Apr 23 23:08:11test Keepalived_vrrp[3856]: Configuration is using : 66502 Bytes

Apr 23 23:08:11test Keepalived_vrrp[3856]: Using LinkWatch kernel netlink reflector...

Apr 23 23:08:11test Keepalived_vrrp[3856]: VRRP sockpool: [ifindex(2), proto(112), unicast(0),fd(10,11)]

Apr 23 23:08:11test Keepalived_vrrp[3856]: VRRP_Script(chk_nginx) succeeded

Apr 23 23:08:12test Keepalived_vrrp[3856]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Netlink reflector reports IP 192.168.88.130added

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Netlink reflector reports IPfe80::20c:29ff:fe4c:4cc added

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Registering Kernel netlink reflector

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Registering Kernel netlink commandchannel

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Opening file'/etc/keepalived/keepalived.conf'.

Apr 23 23:08:12test Keepalived_healthcheckers[3855]: Configuration is using : 7610 Bytes

Apr 23 23:08:13test Keepalived_healthcheckers[3855]: Using LinkWatch kernel netlinkreflector...

Apr 23 23:08:13test Keepalived_vrrp[3856]: VRRP_Instance(VI_1) Entering MASTER STATE

Apr 23 23:08:13test Keepalived_vrrp[3856]: VRRP_Instance(VI_1) setting protocol VIPs.

Apr 23 23:08:13test Keepalived_vrrp[3856]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0for 192.168.88.80

Apr 23 23:08:13test Keepalived_vrrp[3856]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:08:13test Keepalived_healthcheckers[3855]: Netlink reflector reports IP192.168.88.80 added

Apr 23 23:08:18test Keepalived_vrrp[3856]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0for 192.168.88.80

www节点:作为backup节点,不分配VIP

[root@wwwkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:e3:90:19 brdff:ff:ff:ff:ff:ff

    inet 192.168.88.131/24 brd 192.168.88.255scope global eth1

    inet6 fe80::20c:29ff:fee3:9019/64 scopelink

       valid_lft forever preferred_lft forever

[root@www ~]#tail -f /var/log/keepalived.log

Apr 23 23:08:16www Keepalived[3638]: Starting Keepalived v1.2.13 (03/19,2015)

Apr 23 23:08:16www Keepalived[3640]: Starting Healthcheck child process, pid=3641

Apr 23 23:08:16www Keepalived_healthcheckers[3641]: Initializing ipvs 2.6

Apr 23 23:08:16www Keepalived[3640]: Starting VRRP child process, pid=3642

Apr 23 23:08:16www Keepalived_vrrp[3642]: Netlink reflector reports IP 192.168.88.131 added

Apr 23 23:08:16www Keepalived_vrrp[3642]: Netlink reflector reports IPfe80::20c:29ff:fee3:9019 added

Apr 23 23:08:16www Keepalived_vrrp[3642]: Registering Kernel netlink reflector

Apr 23 23:08:16www Keepalived_vrrp[3642]: Registering Kernel netlink command channel

Apr 23 23:08:16www Keepalived_vrrp[3642]: Registering gratuitous ARP shared channel

Apr 23 23:08:16www Keepalived_vrrp[3642]: Opening file '/etc/keepalived/keepalived.conf'.

Apr 23 23:08:16www Keepalived_vrrp[3642]: Configuration is using : 66486 Bytes

Apr 23 23:08:16www Keepalived_vrrp[3642]: Using LinkWatch kernel netlink reflector...

Apr 23 23:08:16www Keepalived_vrrp[3642]: VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 23 23:08:16www Keepalived_vrrp[3642]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:08:16www Keepalived_vrrp[3642]: VRRP sockpool: [ifindex(2), proto(112), unicast(0),fd(10,11)]

Apr 23 23:08:17www Keepalived_vrrp[3642]: VRRP_Script(chk_nginx) succeeded

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Netlink reflector reports IP192.168.88.131 added

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Netlink reflector reports IPfe80::20c:29ff:fee3:9019 added

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Registering Kernel netlink reflector

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Registering Kernel netlink command channel

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Opening file'/etc/keepalived/keepalived.conf'.

Apr 23 23:08:18www Keepalived_healthcheckers[3641]: Configuration is using : 7594 Bytes

Apr 23 23:08:19www Keepalived_healthcheckers[3641]: Using LinkWatch kernel netlinkreflector...

测试1:主备机Nginx均正常

.Windows7访问http://192.168.88.80:8080/

页面在【Welcome to Http on web2.field.com!!】和【Welcome to Http on test2.field.com!!】之间跳转

.test机用curl访问http://192.168.88.80:8080/,会依次连接192.168.88.129:80和192.168.88.130:80

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]#

测试2、关闭test机nginx服务

[root@testkeepalived]# service nginx stop

停止 nginx:[确定]

[root@test keepalived]#ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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 qlen1000

    link/ether 00:0c:29:4c:04:cc brdff:ff:ff:ff:ff:ff

    inet 192.168.88.130/24 brd 192.168.88.255scope global eth0

    inet6fe80::20c:29ff:fe4c:4cc/64 scope link

       valid_lft forever preferred_lft forever

You have newmail in /var/spool/mail/root

观察主节点IP可以发现,master节点: Netlink reflector reports IP 192.168.88.80removed

[root@test ~]#tail -f /var/log/keepalived.log

Apr 23 23:32:53 test Keepalived_vrrp[3856]:VRRP_Script(chk_nginx) failed

Apr 23 23:32:55 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Received higher prio advert

Apr 23 23:32:55 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 23 23:32:55 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) removing protocol VIPs.

Apr 23 23:32:55 test Keepalived_vrrp[3856]: Opening scriptfile /etc/keepalived/notify.sh

Apr 23 23:32:55 test Keepalived_healthcheckers[3855]: Netlinkreflector reports IP 192.168.88.80 removed

www节点,主节点关闭Nginx,VIP流转到backup节点

[root@wwwkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:e3:90:19 brdff:ff:ff:ff:ff:ff

    inet 192.168.88.131/24 brd 192.168.88.255scope global eth1

    inet192.168.88.80/16 scope global eth1:0

    inet6 fe80::20c:29ff:fee3:9019/64 scopelink

       valid_lft forever preferred_lft forever

观察备节点IP可以发现,VIP流转到backup节点:Netlink reflector reports IP 192.168.88.80added      

[root@www ~]#tail -f /var/log/keepalived.log

Apr 23 23:32:55 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) forcing a new MASTER election

Apr 23 23:32:55 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) forcing a new MASTER election

Apr 23 23:32:56 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 23 23:32:57 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Entering MASTER STATE

Apr 23 23:32:57 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) setting protocol VIPs.

Apr 23 23:32:57 www Keepalived_healthcheckers[3641]: Netlinkreflector reports IP 192.168.88.80 added

Apr 23 23:32:57 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.88.80

Apr 23 23:32:57 www Keepalived_vrrp[3642]: Opening script file/etc/keepalived/notify.sh

Apr 23 23:33:02 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.88.80

测试网页访问:

.Windows7访问http://192.168.88.80:8080/

页面在【Welcome to Http on web2.field.com!!】和【Welcome to Http on test2.field.com!!】之间跳转

.test机用curl访问http://192.168.88.80:8080/,会依次连接192.168.88.129:80和192.168.88.130:80

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]#

测试3、重启nginx,主节点抢占回vip

[root@testkeepalived]# service nginx start

正在启动 nginx:[确定]

[root@testkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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 qlen1000

    link/ether 00:0c:29:4c:04:cc brdff:ff:ff:ff:ff:ff

    inet 192.168.88.130/24 brd 192.168.88.255scope global eth0

    inet 192.168.88.80/16 scope global eth0:0

    inet6 fe80::20c:29ff:fe4c:4cc/64 scope link

       valid_lft forever preferred_lft forever

You have newmail in /var/spool/mail/root

 [root@test keepalived]# mail

Heirloom Mailversion 12.4 7/29/08.  Type ? for help.

 N 22 root                  Mon Apr 23 23:37  18/687  "test.field.com to be master: 192.168.88.80 floating"

& 22

Message 22:

From [email protected] Mon Apr 23 23:37:31 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:37:31 +0800

To: [email protected]

Subject: test.field.com to be master: 192.168.88.80 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:37:31: vrrp transition, test.field.com changed to be master

& q

Held 22 messagesin /var/spool/mail/root

观察主节点IP可以发现,master抢回VIPNetlink reflector reports IP 192.168.88.80 added

[root@test ~]#tail -f /var/log/keepalived.log

Apr 23 23:37:28 test Keepalived_vrrp[3856]:VRRP_Script(chk_nginx) succeeded

Apr 23 23:37:29 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) forcing a new MASTER election

Apr 23 23:37:29 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) forcing a new MASTER election

Apr 23 23:37:30 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 23 23:37:31 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Entering MASTER STATE

Apr 23 23:37:31 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) setting protocol VIPs.

Apr 23 23:37:31 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.88.80

Apr 23 23:37:31 test Keepalived_vrrp[3856]: Opening scriptfile /etc/keepalived/notify.sh

Apr 23 23:37:31 test Keepalived_healthcheckers[3855]: Netlinkreflector reports IP 192.168.88.80 added

Apr 23 23:37:36 test Keepalived_vrrp[3856]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.88.80

www节点:master节点启动Nginx,backup节点VIP移除

[root@wwwkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:e3:90:19 brdff:ff:ff:ff:ff:ff

    inet 192.168.88.131/24 brd 192.168.88.255scope global eth1

    inet6 fe80::20c:29ff:fee3:9019/64 scopelink

       valid_lft forever preferred_lft forever

You have newmail in /var/spool/mail/root

观察backup节点可以发现,VIP移除:Netlink reflector reports IP 192.168.88.80 removed

[root@www ~]#tail -f /var/log/keepalived.log

Apr 23 23:37:29 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Received higher prio advert

Apr 23 23:37:29 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 23 23:37:29 www Keepalived_vrrp[3642]:VRRP_Instance(VI_1) removing protocol VIPs.

Apr 23 23:37:29 www Keepalived_vrrp[3642]: Opening scriptfile /etc/keepalived/notify.sh

Apr 23 23:37:29 www Keepalived_healthcheckers[3641]: Netlinkreflector reports IP 192.168.88.80 removed

 

三、keepalived实现nginx高可用的双主模型

案例2、配置keepalived实现nginx高可用的双主模型

说明:其基本实现思想为创建两个虚拟路由器,并以两个节点互为主从。

(1)、www节点,编辑keepalived.conf,添加虚拟路由实例VI_2

[root@wwwkeepalived]# vi keepalived.conf

vrrp_instance VI_2 {

    state MASTER

    interface eth1

    virtual_router_id 61

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass Te7UYb3a

    }

    virtual_ipaddress {

        192.168.88.90/16 dev eth1 label eth1:1

    }

track_script {

#track_script 调用脚本

        chk_nginx

}

        notify_master"/etc/keepalived/notify.sh master"

        notify_backup"/etc/keepalived/notify.sh backup"

        notify_fault"/etc/keepalived/notify.sh fault"

}

(2)、test节点,编辑keepalived.conf,添加虚拟路由实例VI_2

[root@testkeepalived]# vi keepalived.conf

vrrp_instance VI_2 {

    state BACKUP

    interface eth0

    virtual_router_id 61

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass Te7UYb3a

    }

    virtual_ipaddress {

        192.168.88.90/16 dev eth0 label eth0:1

    }

track_script {

#track_script 调用脚本

        chk_nginx

}

        notify_master"/etc/keepalived/notify.sh master"

        notify_backup"/etc/keepalived/notify.sh backup"

        notify_fault"/etc/keepalived/notify.sh fault"

}

[root@testkeepalived]# service keepalived restart;ssh www 'service keepalived restart'

停止 keepalived:[确定]

正在启动 keepalived:[确定]

停止 keepalived:[确定]

正在启动 keepalived:[确定]

test节点:在虚拟路由实例VI_1中作为master,占有VI_1的VIP:192.168.88.80

[root@testkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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 qlen1000

    link/ether 00:0c:29:4c:04:cc brdff:ff:ff:ff:ff:ff

    inet 192.168.88.130/24 brd 192.168.88.255scope global eth0

    inet192.168.88.80/16 scope global eth0:0

    inet6 fe80::20c:29ff:fe4c:4cc/64 scope link

       valid_lft forever preferred_lft forever

[root@testkeepalived]# mail

Heirloom Mailversion 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/root":27 messages 3 new 19 unread

 N 27 root                  Mon Apr 23 23:51  18/687  "test.field.com to be master: 192.168.88.80 floating"

& 27

Message 27:

From [email protected] Mon Apr 23 23:51:18 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:51:18 +0800

To: [email protected]

Subject: test.field.com to be master: 192.168.88.80 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:51:18: vrrp transition, test.field.com changed to be master

& q

Held 27 messagesin /var/spool/mail/root

You have mail in/var/spool/mail/root

[root@test ~]#tail -f /var/log/keepalived.log

Apr 23 23:51:16test Keepalived[11946]: Stopping Keepalived v1.2.13 (03/19,2015)

Apr 23 23:51:16test Keepalived_vrrp[11948]: VRRP_Instance(VI_1) sending 0 priority

Apr 23 23:51:16test Keepalived_vrrp[11948]: VRRP_Instance(VI_1) removing protocol VIPs.

Apr 23 23:51:16test Keepalived_healthcheckers[11947]: Netlink reflector reports IP192.168.88.80 removed

Apr 23 23:51:16test Keepalived[12478]: Starting Keepalived v1.2.13 (03/19,2015)

Apr 23 23:51:16test Keepalived[12479]: Starting Healthcheck child process, pid=12480

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Initializing ipvs 2.6

Apr 23 23:51:16test Keepalived[12479]: Starting VRRP child process, pid=12481

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Netlink reflector reports IP192.168.88.130 added

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Netlink reflector reports IPfe80::20c:29ff:fe4c:4cc added

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Registering Kernel netlink reflector

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Registering Kernel netlink commandchannel

Apr 23 23:51:16test Keepalived_vrrp[12481]: Netlink reflector reports IP 192.168.88.130 added

Apr 23 23:51:16test Keepalived_vrrp[12481]: Netlink reflector reports IPfe80::20c:29ff:fe4c:4cc added

Apr 23 23:51:16test Keepalived_vrrp[12481]: Registering Kernel netlink reflector

Apr 23 23:51:16test Keepalived_vrrp[12481]: Registering Kernel netlink command channel

Apr 23 23:51:16test Keepalived_vrrp[12481]: Registering gratuitous ARP shared channel

Apr 23 23:51:16test Keepalived_vrrp[12481]: Opening file '/etc/keepalived/keepalived.conf'.

Apr 23 23:51:16test Keepalived_vrrp[12481]: Configuration is using : 71723 Bytes

Apr 23 23:51:16test Keepalived_vrrp[12481]: Using LinkWatch kernel netlink reflector...

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Opening file'/etc/keepalived/keepalived.conf'.

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Configuration is using : 7940 Bytes

Apr 23 23:51:16test Keepalived_healthcheckers[12480]: Using LinkWatch kernel netlinkreflector...

Apr 23 23:51:16test Keepalived_vrrp[12481]: VRRP_Instance(VI_2) Entering BACKUP STATE

Apr 23 23:51:16test Keepalived_vrrp[12481]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:51:16test Keepalived_vrrp[12481]: VRRP sockpool: [ifindex(2), proto(112),unicast(0), fd(10,11)]

Apr 23 23:51:16test Keepalived_vrrp[12481]: VRRP_Script(chk_nginx) succeeded

Apr 23 23:51:17test Keepalived_vrrp[12481]: VRRP_Instance(VI_2) Transition to MASTER STATE

Apr 23 23:51:17test Keepalived_vrrp[12481]: VRRP_Instance(VI_2) Received higher prio advert

Apr 23 23:51:17test Keepalived_vrrp[12481]: VRRP_Instance(VI_2) Entering BACKUP STATE

Apr 23 23:51:17test Keepalived_vrrp[12481]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:51:17test Keepalived_vrrp[12481]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 23 23:51:18test Keepalived_vrrp[12481]: VRRP_Instance(VI_1) Entering MASTER STATE

Apr 23 23:51:18test Keepalived_vrrp[12481]: VRRP_Instance(VI_1) setting protocol VIPs.

Apr 23 23:51:18test Keepalived_healthcheckers[12480]: Netlink reflector reports IP192.168.88.80 added

Apr 23 23:51:18test Keepalived_vrrp[12481]: VRRP_Instance(VI_1) Sending gratuitous ARPs oneth0 for 192.168.88.80

Apr 23 23:51:18test Keepalived_vrrp[12481]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:51:23test Keepalived_vrrp[12481]: VRRP_Instance(VI_1) Sending gratuitous ARPs oneth0 for 192.168.88.80

www节点:在虚拟路由实例VI_2中作为master,占有VI_2的VIP:192.168.88.90

[root@wwwkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:e3:90:19 brdff:ff:ff:ff:ff:ff

    inet 192.168.88.131/24 brd 192.168.88.255scope global eth1

    inet192.168.88.90/16 scope global eth1:1

    inet6 fe80::20c:29ff:fee3:9019/64 scopelink

       valid_lft forever preferred_lft forever

You have newmail in /var/spool/mail/root

[root@wwwkeepalived]# mail

Heirloom Mailversion 12.4 7/29/08.  Type ? for help.

"/var/spool/mail/root":50 messages 4 new 42 unread

 N 50 root                  Mon Apr 23 23:51  18/680  "www.field.com to be master: 192.168.88.90 floating"

& 50

Message 50:

From [email protected] Mon Apr 23 23:51:18 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:51:18 +0800

To: [email protected]

Subject: www.field.com to be master: 192.168.88.90 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:51:18: vrrp transition, www.field.com changed to be master

& q

Held 50 messagesin /var/spool/mail/root

 [root@www ~]# tail -f /var/log/keepalived.log

Apr 23 23:51:16www Keepalived_vrrp[11512]: VRRP_Instance(VI_2) sending 0 priority

Apr 23 23:51:16www Keepalived[11510]: Stopping Keepalived v1.2.13 (03/19,2015)

Apr 23 23:51:16www Keepalived_vrrp[11512]: VRRP_Instance(VI_2) removing protocol VIPs.

Apr 23 23:51:16www Keepalived[12042]: Starting Keepalived v1.2.13 (03/19,2015)

Apr 23 23:51:16www Keepalived[12043]: Starting Healthcheck child process, pid=12044

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Initializing ipvs 2.6

Apr 23 23:51:16www Keepalived[12043]: Starting VRRP child process, pid=12045

Apr 23 23:51:16www Keepalived_vrrp[12045]: Netlink reflector reports IP 192.168.88.131 added

Apr 23 23:51:16www Keepalived_vrrp[12045]: Netlink reflector reports IPfe80::20c:29ff:fee3:9019 added

Apr 23 23:51:16www Keepalived_vrrp[12045]: Registering Kernel netlink reflector

Apr 23 23:51:16www Keepalived_vrrp[12045]: Registering Kernel netlink command channel

Apr 23 23:51:16www Keepalived_vrrp[12045]: Registering gratuitous ARP shared channel

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Netlink reflector reports IP192.168.88.131 added

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Netlink reflector reports IPfe80::20c:29ff:fee3:9019 added

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Registering Kernel netlink reflector

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Registering Kernel netlink commandchannel

Apr 23 23:51:16www Keepalived_vrrp[12045]: Opening file '/etc/keepalived/keepalived.conf'.

Apr 23 23:51:16www Keepalived_vrrp[12045]: Configuration is using : 71709 Bytes

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Opening file '/etc/keepalived/keepalived.conf'.

Apr 23 23:51:16www Keepalived_vrrp[12045]: Using LinkWatch kernel netlink reflector...

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Configuration is using : 7926 Bytes

Apr 23 23:51:16www Keepalived_healthcheckers[12044]: Using LinkWatch kernel netlinkreflector...

Apr 23 23:51:16www Keepalived_vrrp[12045]: VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 23 23:51:16www Keepalived_vrrp[12045]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:51:16www Keepalived_vrrp[12045]: VRRP sockpool: [ifindex(2), proto(112), unicast(0),fd(10,11)]

Apr 23 23:51:17www Keepalived_vrrp[12045]: VRRP_Script(chk_nginx) succeeded

Apr 23 23:51:17www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) Transition to MASTER STATE

Apr 23 23:51:17www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) Received lower prio advert,forcing new election

Apr 23 23:51:18www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) Entering MASTER STATE

Apr 23 23:51:18www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) setting protocol VIPs.

Apr 23 23:51:18www Keepalived_healthcheckers[12044]: Netlink reflector reports IP192.168.88.90 added

Apr 23 23:51:18www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth1for 192.168.88.90

Apr 23 23:51:18www Keepalived_vrrp[12045]: Opening script file /etc/keepalived/notify.sh

Apr 23 23:51:23www Keepalived_vrrp[12045]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth1for 192.168.88.90

测试1:主备机nginx均正常

.Windows7访问http://192.168.88.80:8080/和 http://192.168.88.90:8080/

页面在【Welcome to Http on web2.field.com!!】和【Welcome to Http on test2.field.com!!】之间跳转

.www机用curl访问http://192.168.88.80:8080/,会依次连接192.168.88.129:80和192.168.88.130:80

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@test keepalived]#curl http://192.168.88.90:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]# curl http://192.168.88.90:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@testkeepalived]#

测试2、一台机关闭nginx,访问两个虚拟IP地址,仍能正常访问。

[root@wwwkeepalived]# service nginx stop

停止 nginx:[确定]

[root@wwwkeepalived]# mail

>N 51root                  Mon Apr 2323:55  18/680   "www.field.com to be backup:192.168.88.90 floating"

& 51

Message 51:

From [email protected] Mon Apr 23 23:55:47 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:55:47 +0800

To: [email protected]

Subject: www.field.com to be backup: 192.168.88.90 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:55:47: vrrp transition, www.field.com changed to be backup

& q

Held 51 messagesin /var/spool/mail/root

 [root@www keepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

    link/ether 00:0c:29:e3:90:19 brdff:ff:ff:ff:ff:ff

    inet 192.168.88.131/24 brd 192.168.88.255scope global eth1

    inet6 fe80::20c:29ff:fee3:9019/64 scopelink

       valid_lft forever preferred_lft forever

 [root@www ~]# tail -f /var/log/keepalived.log

Apr 23 23:55:46 www Keepalived_vrrp[12045]:VRRP_Script(chk_nginx) failed

Apr 23 23:55:47 www Keepalived_vrrp[12045]:VRRP_Instance(VI_2) Received higher prio advert

Apr 23 23:55:47 www Keepalived_vrrp[12045]:VRRP_Instance(VI_2) Entering BACKUP STATE

Apr 23 23:55:47 www Keepalived_vrrp[12045]:VRRP_Instance(VI_2) removing protocol VIPs.

Apr 23 23:55:47 www Keepalived_vrrp[12045]: Opening scriptfile /etc/keepalived/notify.sh

Apr 23 23:55:47 www Keepalived_healthcheckers[12044]: Netlinkreflector reports IP 192.168.88.90 removed

test节点:www节点关闭Nginx,VIP192.168.88.80会流转到该主机backup节点

[root@testkeepalived]# ip addr list

1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd00: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 qlen1000

    link/ether 00:0c:29:4c:04:cc brdff:ff:ff:ff:ff:ff

    inet 192.168.88.130/24 brd 192.168.88.255scope global eth0

    inet192.168.88.80/16 scope global eth0:0

    inet192.168.88.90/16 scope global secondary eth0:1

    inet6 fe80::20c:29ff:fe4c:4cc/64 scope link

       valid_lft forever preferred_lft forever

[root@testkeepalived]# mail

>N 28root                  Mon Apr 2323:55  18/687   "test.field.com to be master:192.168.88.80 floating"

& 28

Message 28:

From [email protected] Mon Apr 23 23:55:49 2018

Return-Path: <[email protected]>

X-Original-To: root@localhost

Delivered-To: [email protected]

Date: Mon, 23 Apr 2018 23:55:49 +0800

To: [email protected]

Subject: test.field.com to be master: 192.168.88.90 floating

User-Agent: Heirloom mailx 12.4 7/29/08

Content-Type: text/plain; charset=us-ascii

From: [email protected] (root)

Status: R

2018-04-2323:55:49: vrrp transition, test.field.com changed to be master

& q

Held 28 messagesin /var/spool/mail/root

[root@test ~]#tail -f /var/log/keepalived.log

Apr 23 23:55:47 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) forcing a new MASTER election

Apr 23 23:55:47 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) forcing a new MASTER election

Apr 23 23:55:48 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) Transition to MASTER STATE

Apr 23 23:55:49 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) Entering MASTER STATE

Apr 23 23:55:49 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) setting protocol VIPs.

Apr 23 23:55:49 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.88.90

Apr 23 23:55:49 test Keepalived_healthcheckers[12480]:Netlink reflector reports IP 192.168.88.90 added

Apr 23 23:55:49 test Keepalived_vrrp[12481]: Opening scriptfile /etc/keepalived/notify.sh

Apr 23 23:55:54 test Keepalived_vrrp[12481]:VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.88.90

.Windows7访问http://192.168.88.80:8080/和 http://192.168.88.90:8080/

页面在【Welcome to Http on web2.field.com!!】和【Welcome to Http on test2.field.com!!】之间跳转

.www机用curl访问http://192.168.88.80:8080/,和http://192.168.88.90:8080/

会依次连接192.168.88.129:80和192.168.88.130:80

[root@wwwkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@wwwkeepalived]# curl http://192.168.88.80:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@wwwkeepalived]# curl http://192.168.88.90:8080/

<h1>Welcome to Http on test.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

[root@wwwkeepalived]# curl http://192.168.88.90:8080/

<h1>Welcome to Http on web2.field.com!!</h1>

<p>If you see this page,test ok. </p>

<p><em>Thank you!</em></p>

 

 

猜你喜欢

转载自blog.csdn.net/field_yang/article/details/80058762