Keepalived 简单实现 Nginx 的高可用

文章
林里克斯

Keepalived 简单实现 Nginx 的高可用


实验平台:CentOS Linux release 7.9.2009

keepalived Version:Keepalived v2.1.5

Nginx Version:nginx/1.16.1

VIP:10.10.12.100 / Keepalived1(Nginx1):10.10.12.5 / Keepalived12(Nginx2):10.10.12.6


一、环境搭建


1.安装 keepalived

$ yum -y install keepalived
#这里测试就使用了 yum 来安装了,2 个节点都要安装

其他安装方式可以参考 -> Linux 下搭建 Keepalived

2.安装 Nginx

$ yum -y install nginx
#这里测试就使用了 yum 来安装了,2 个节点都要安装

其他安装方式可以参考 -> Linux 下编译安装 Nginx

3.修改下 Nginx 的页面方便测试

$ vim /usr/share/nginx/html/index.html
<h1>CentOS 1</h1>
#主节点的

Keepalived 简单实现 Nginx 的高可用

$ vim /usr/share/nginx/html/index.html
<h1>CentOS 2</h1>
#备节点的

Keepalived 简单实现 Nginx 的高可用


二、配置 Keepalived


1.编辑主节点配置文件 keepalived.conf

$ vim /data/keepalived/conf/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id nginx01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    mcast_src_ip 10.10.12.5
    priority 100
    advert_int 1              
    authentication {
        auth_type PASS
        auth_pass 111111
    }
    virtual_ipaddress {
        10.10.12.100/24 brd 10.10.12.255 dev ens33 lable ens33:0
    }
}

virtual_server 10.10.12.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 10.10.12.5 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }

    real_server 10.10.12.6 80 {
        weight 3
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }
}

2.编辑备节点 keepalived.conf

$ vim /data/keepalived/conf/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id nginx02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    mcast_src_ip 10.10.12.6
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 111111
    }
    virtual_ipaddress {
        10.10.12.100/24 brd 10.10.12.255 dev ens33 lable ens33:0
    }
}

virtual_server 10.10.12.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 10.10.12.5 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }

    real_server 10.10.12.6 80 {
        weight 3
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 1
            retry 3
            delay_before_retry 1
        }
    }
}

3.各节点启动服务

$ systemctl start keepalived

4.查看 2 节点 ip 地址,我这里方便对比,使用 ansible

$ ansible keepalived -m shell -a "ip a s ens33"
10.10.12.6 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5c:d9:2d brd ff:ff:ff:ff:ff:ff
    inet 10.10.12.6/24 brd 10.10.12.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::de8b:5b08:ee0f:8e47/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
10.10.12.5 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:26:54:bf brd ff:ff:ff:ff:ff:ff
    inet 10.10.12.5/24 brd 10.10.12.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.10.12.100/24 brd 10.10.12.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::df07:293c:e304:f9c/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
#多了一个 10.10.12.100 的ip

三、验证效果


1.写个循环访问验证

$ while true;do curl 10.10.12.100; sleep 1;done

2.直观验证

Keepalived 简单实现 Nginx 的高可用

$ sudo systemctl stop nginx

Keepalived 简单实现 Nginx 的高可用

3.查看 VIP ,发现 VIP 漂移到了另外一台上

$ ansible keepalived -m shell -a "ip a s ens33"
10.10.12.6 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5c:d9:2d brd ff:ff:ff:ff:ff:ff
    inet 10.10.12.6/24 brd 10.10.12.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.10.12.100/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::de8b:5b08:ee0f:8e47/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
10.10.12.5 | CHANGED | rc=0 >>
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:26:54:bf brd ff:ff:ff:ff:ff:ff
    inet 10.10.12.5/24 brd 10.10.12.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::df07:293c:e304:f9c/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

1204 0 2020-12-03


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-12-28

今日天气
站点统计
  • 文章总数:241篇
  • 分类总数:29个
  • 评论总数:12条
  • 本站总访问量 353772 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!