nginx高可用

1.准备工作

(1)需要准备两台服务器192.168.60.10(主) 和 192.168.60.13(备)

(2)两台服务器都安装nginx

(3)在两台服务器安装keepalived

2. 安装Nginx

(1)使⽤用Nginx官⽅方提供的rpm包。

[root@skj ~]# 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@skj ~]# yum install nginx -y
[root@skj ~]# systemctl start nginx
[root@skj ~]# systemctl enable nginx

3. 安装keepalived

[root@skj ~]# yum install keepalived -y

4. 配置主keepalived

[root@skj ~]# cd /etc/keepalived/
[root@skj keepalived]# cp keepalived.conf keepalived.conf.bak
[root@skj keepalived]# >keepalived.conf
[root@skj 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.60.50
      }
   }

5. 主keepalived.conf传到备机并修改

[root@skj keepalived]# scp keepalived.conf root@192.168.60.13:/etc/keepalived/

nginx高可用

#切换到备机192.168.60.13(备)上去修改keepalived.conf

[root@abc ~]# cd /etc/keepalived/
[root@abc 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.60.50
      }
   }

6. 配置准备nginx

#主192.168.60.10

[root@skj keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@skj keepalived]# >/usr/share/nginx/html/index.html
[root@skj keepalived]# vi /usr/share/nginx/html/index.html
web01

#备 192.168.60.13

[root@abc keepalived]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@abc keepalived]# >/usr/share/nginx/html/index.html
[root@abc keepalived]# vi /usr/share/nginx/html/index.html
web02

7. 启动服务并开机自启

[root@skj keepalived]# systemctl start nginx
[root@skj keepalived]# systemctl start keepalived
[root@skj keepalived]# systemctl enable nginx
[root@skj keepalived]# systemctl enable keepalived

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

nginx高可用nginx高可用nginx高可用

9. 测试主备切换

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

[root@skj keepalived]# systemctl stop keepalived

刷新页面,vip已经切换到备(192.168.60.13上)

 

nginx高可用

再主上启动keepalived

[root@skj keepalived]# systemctl start keepalived

刷新页面,发现又切换回来了

10. nginx存活测试

在主服务器192.168.60.10上添加如下参数:

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.60.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.60.50
      }
      track_script {
        chk_nginx
      }
   }

在/opt下创建chknginx.sh文件,授予权限

[root@skj keepalived]# 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@skj keepalived]# chmod 775 /opt/chknginx.sh

在主nginx上关闭服务器

[root@skj keepalived]# shutdown -h now

则vip会漂移到备服务器192.168.60.13上。

浏览器输入 http://192.168.60.50 可查看是否有跳转到备服务器上

上一篇:keepalived+msyql+nginx 高可用数据库主从同步 nginx负载均衡+vsftpd发布


下一篇:keepalived实现nginx负载均衡机高可用