Keepalived实现VIP+Nginx实现反向代理

一、实验环境

Keepalived+Nginx 服务器:10.0.0.11(keepalived-1)、10.0.0.12(keepalived-2)
nginx服务器:10.0.0.13(nginx-web-1)、10.0.0.14(nginx-web-2)
VIP:10.0.0.100

二、VIP配置

keepalived-1 配置

global_defs {
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.100 dev eth0 label eth0:0
    }
}
include /etc/keepalived/conf/*.conf

keepalived-2 配置

global_defs {
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.100 dev eth0 label eth0:0
    }
}
include /etc/keepalived/conf/*.conf

三、Nginx配置访问页面

nginx-web-1 配置

[root@nginx-web-1 ~]# vim /apps/nginx/html/index.html
10.0.0.13-nginx-web1

nginx-web-2 配置

[root@nginx-web-2 ~]# vim /apps/nginx/html/index.html
10.0.0.14-nginx-web2

四、Nginx反向代理配置

keepalived-1 配置

[root@keepalived-1 src]# vim /apps/nginx/conf/http/www.lck.net.conf
http {
	......
    include /apps/nginx/conf/http/*.conf;
}
[root@keepalived-1 src]# vim /apps/nginx/conf/http/www.lck.net.conf
upstream webserver {
    server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3;
    server 10.0.0.14:80 weight=1 fail_timeout=15s max_fails=3;
}

server {
    listen 80;
    server_name www.lck.net;
location /web {
    index index.html;
    proxy_pass http://webserver/;
    }
}

keepalived-2 配置

[root@keepalived-2 src]# vim /apps/nginx/conf/http/www.lck.net.conf
http {
	......
    include /apps/nginx/conf/http/*.conf;
}
[root@keepalived-2 src]# vim /apps/nginx/conf/http/www.lck.net.conf
upstream webserver {
    server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3;
    server 10.0.0.14:80 weight=1 fail_timeout=15s max_fails=3;
}

server {
    listen 80;
    server_name www.lck.net;
location /web {
    index index.html;
    proxy_pass http://webserver/;
    }
}

五、配置VRRP Script

keepalived-1 配置

[root@keepalived-1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2
}
vrrp_instance VI_1 {
	......
    track_script {
        check_nginx
    }
}
[root@keepalived-1 ~]# vim /etc/keepalived/check_nginx.sh
#!/bin/bash
/usr/bin/killall -0 nginx
[root@keepalived-1 ~]# yum install -y psmisc
[root@keepalived-1 ~]# chmod a+x /etc/keepalived/check_nginx.sh
[root@keepalived-1 ~]# systemctl restart keepalived

六、访问测试

在这里插入图片描述

七、模拟Master的Nginx故障

关闭Master的Nginx服务,并查看keepalived-1日志信息,发现

[root@keepalived-1 ~]# systemctl stop nginx.service
[root@keepalived-1 ~]# tail -f /var/log/messages
Jun 21 02:39:41 centos7 Keepalived_vrrp[33309]: VRRP_Script(check_nginx) failed (exited with status 1)
Jun 21 02:39:41 centos7 Keepalived_vrrp[33309]: (VI_1) Changing effective priority from 100 to 70
Jun 21 02:39:45 centos7 Keepalived_vrrp[33309]: (VI_1) Master received advert from 10.0.0.12 with higher priority 80, ours 70
Jun 21 02:39:45 centos7 Keepalived_vrrp[33309]: (VI_1) Entering BACKUP STATE
Jun 21 02:39:45 centos7 Keepalived_vrrp[33309]: (VI_1) removing VIPs.

查看keepalived-2日志信息

[root@keepalived-2 ~]# tail -f /var/log/messages
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: (VI_1) Backup received priority 0 advertisement
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: (VI_1) Receive advertisement timeout
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: (VI_1) Entering MASTER STATE
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: (VI_1) setting VIPs.
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.0.0.100
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100
Jun 21 02:39:32 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100
Jun 21 02:39:36 centos7 Keepalived_vrrp[32620]: (VI_1) Master received advert from 10.0.0.11 with higher priority 100, ours 80
Jun 21 02:39:36 centos7 Keepalived_vrrp[32620]: (VI_1) Entering BACKUP STATE
Jun 21 02:39:36 centos7 Keepalived_vrrp[32620]: (VI_1) removing VIPs.
Jun 21 02:39:43 centos7 Keepalived_vrrp[32620]: (VI_1) received lower priority (70) advert from 10.0.0.11 - discarding
Jun 21 02:39:44 centos7 Keepalived_vrrp[32620]: (VI_1) received lower priority (70) advert from 10.0.0.11 - discarding
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: (VI_1) received lower priority (70) advert from 10.0.0.11 - discarding
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: (VI_1) Receive advertisement timeout
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: (VI_1) Entering MASTER STATE
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: (VI_1) setting VIPs.
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.0.0.100
Jun 21 02:39:45 centos7 Keepalived_vrrp[32620]: Sending gratuitous ARP on eth0 for 10.0.0.100

查看keepalived-2的VIP

[root@keepalived-2 src]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    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 group default qlen 1000
    link/ether 00:0c:29:35:ff:02 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.12/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/32 scope global eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe35:ff02/64 scope link
       valid_lft forever preferred_lft forever

七、Master的Nginx故障恢复

Master的Nginx故障恢复,keepalived-1成功将VIP抢占回来

[root@keepalived-1 ~]# systemctl restart nginx.service
[root@keepalived-1 ~]# tail -f /var/log/messages
Jun 21 02:45:00 centos7 nginx: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Jun 21 02:45:00 centos7 nginx: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Jun 21 02:45:00 centos7 systemd: Started The nginx HTTP and reverse proxy server.
Jun 21 02:45:01 centos7 Keepalived_vrrp[33309]: Script `check_nginx` now returning 0
Jun 21 02:45:05 centos7 Keepalived_vrrp[33309]: VRRP_Script(check_nginx) succeeded
Jun 21 02:45:05 centos7 Keepalived_vrrp[33309]: (VI_1) Changing effective priority from 70 to 100
Jun 21 02:45:06 centos7 Keepalived_vrrp[33309]: (VI_1) received lower priority (80) advert from 10.0.0.12 - discarding
Jun 21 02:45:07 centos7 Keepalived_vrrp[33309]: (VI_1) received lower priority (80) advert from 10.0.0.12 - discarding
Jun 21 02:45:08 centos7 Keepalived_vrrp[33309]: (VI_1) received lower priority (80) advert from 10.0.0.12 - discarding
Jun 21 02:45:08 centos7 Keepalived_vrrp[33309]: (VI_1) Receive advertisement timeout
Jun 21 02:45:08 centos7 Keepalived_vrrp[33309]: (VI_1) Entering MASTER STATE
Jun 21 02:45:08 centos7 Keepalived_vrrp[33309]: (VI_1) setting VIPs.
Jun 21 02:45:08 centos7 Keepalived_vrrp[33309]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.0.0.100
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    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 group default qlen 1000
    link/ether 00:0c:29:e4:f1:3f brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.11/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/32 scope global eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee4:f13f/64 scope link
       valid_lft forever preferred_lft forever
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值