Nginx高可用实践

1.准备工作

(1)准备两台服务器分别为主(192.168.137.100)和备(192.168.137.18);

(2)两台服务器均安装完Nginx;

(3)两台服务器均要安装keepalived;

2.安装Nginx

(1)使用官方的RPM包进行安装

[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo   

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/7/$basearch/

gpgcheck=0

enabled=1

(2)执行yum安装

[root@nginx ~]# yum install nginx -y

[root@nginx ~]# systemctl start nginx

[root@nginx ~]# systemctl enable nginx
 

3. 安装keepalived

[root@test ~]# yum install keepalived -y 
4.  配置主keepalived

[root@test ~]# cd /etc/keepalived/

[root@test keepalived]# cp keepalived.conf keepalived.conf.bak

[root@test keepalived]# > keepalived.conf

[root@test keepalived]# vi keepalived.conf

global_defs {

    vrrp_garp_interval 0

    vrrp_gna_interval 0

}

 

#VIP1

    vrrp_instance VI_1 {

      state MASTER

      interface ens33  

      virtual_router_id 50 

      priority 100

      advert_int 1

      authentication {

         auth_type PASS

         auth_pass 1111

      }

      virtual_ipaddress {

         192.168.137.50

      }

   }

5. 主keepalived 传到备机并修改

[root@test keepalived]# scp keepalived.conf root@192.168.137.18:/etc/keepalived/

#切换到备机192.168.137.18()上去修改keepalived.conf,只需修改

state BACKUP

priority 90

[root@localhost keepalived]# vi keepalived.conf

global_defs {

    vrrp_garp_interval 0

    vrrp_gna_interval 0

}

 

#VIP1

    vrrp_instance VI_1 {

      state BACKUP

      interface ens33

      virtual_router_id 50

      priority 90

      advert_int 1

      authentication {

         auth_type PASS

         auth_pass 1111

      }

      virtual_ipaddress {

         192.168.137.50

      }

   }

6.  配置准备Nginx

#192.168.137.100

[root@test yum.repos.d]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak

[root@test yum.repos.d]# >/usr/share/nginx/html/index.html

[root@test yum.repos.d]# vi /usr/share/nginx/html/index.html

web01

 

# 192.168.137.18

[root@test yum.repos.d]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak

[root@test yum.repos.d]# >/usr/share/nginx/html/index.html

[root@test yum.repos.d]# vi /usr/share/nginx/html/index.html

web02

7.  启动服务并开机自启

[root@test yum.repos.d]# systemctl start nginx

[root@test yum.repos.d]# systemctl start keepalived

[root@test yum.repos.d]# systemctl enable nginx

[root@test yum.repos.d]# systemctl enable keepalived

 

8. 访问各站点(主备及VIP)

9.  测试主备切换

在主(192.168.137.100)上,停掉keepalived,测试主备切换

[root@test ~]# systemctl stop keepalived

刷新页面,VIP已切换到备(192.168.137.18)上

在主服务器上启动keepalived

[root@test ~]# systemctl start keepalived

刷新页面,发现切换回主服务器

10. Nginx存活测试

在主服务器(192.168.137.100)上添加以下参数:

global_defs {

    vrrp_garp_interval 0

    vrrp_gna_interval 0

}

vrrp_script chk_nginx {

    script "/opt/chknginx.sh"

    interval 1

    weight -20

}

#VIP1

    vrrp_instance VI_1 {

      state MASTER

      interface ens33

      virtual_router_id 50

      priority 100

      advert_int 1

      authentication {

         auth_type PASS

         auth_pass 1111

      }

      virtual_ipaddress {

         192.168.137.50

      }

      track_script {

        chk_nginx

      }

   }

 

#VIP1

    vrrp_instance VI_1 {

      state BACKUP

      interface ens33

      virtual_router_id 50

      priority 90

      advert_int 1

      authentication {

         auth_type PASS

         auth_pass 1111

      }

      virtual_ipaddress {

         192.168.137.50

      }

      track_script {

        chk_nginx

      }

   }

在/opt目录下创建chknginx.sh 文件并授予权限

[root@test ~]# vi /opt/chknginx.sh

#!/bin/bash

A=`ps -C nginx --no-header|wc -l`

if [ $A -eq 0 ];then

    systemctl start nginx

    if [ `ps -C nginx --no-header|wc -l` -eq 0 ];then

        killall keepalived

    fi

fi

[root@test ~]# chmod 775 /opt/chknginx.sh

在主Nginx上关闭服务器

[root@test ~]# shutdown -h now

则VIP会移动到备服务器上(192.168.137.180)上

在浏览器输入http://192.168.137.50 课查看是否跳转至备服务器-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值