Nginx+Keepalived高可用

文章目录

    • 前言:
        • Keeplived简介
        • 实验准备
        • Ngxin代理配置
        • Keepalive配置
        • 效果

前言:

Keeplived简介

什么是Keepalive:
keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。
工作原理:
通过VRRP协议完成(虚拟路由冗余协议)
主节点向备节点发送心跳包,如果备节点没有收到心跳包,调用自身的接管程序,接管主节点的IP资源及服务。主节点恢复故障时,备用节点释放IP资源及服务,恢复到原来的备用角色。
Nginx+Keepalived高可用_第1张图片

什么是VRRP协议:
VRRP,中文名虚拟路由冗余协议。主要解决静态路由的单节点故障。VRRP是一种竞选协议机制来将路由任务交给某台VRRP路由器。

三个模块:
core :启动、配置文件的加载
check: 负责健康检查
vrrp : 实现VRRP协议

实验准备

准备四台虚拟机:

  1. 主Nginx代理 /主Keepalive代理
    主机名:137
    地址:192.168.230.137

  1. 备Nginx代理 /备Keepalive代理
    主机名:138
    地址:192.168.230.138

  1. Nginx Web服务器1
    主机名:139
    地址:192.168.230.139

  1. Nginx Web服务器2
    主机名:141
    地址:192.168.230.1141

拓扑结构:

Nginx+Keepalived高可用_第2张图片Nginx+Keepalived高可用_第3张图片

Ngxin代理配置

[root@137~]# cd /etc/yum.repos.d/ 
[root@137~]# vim nginx.repo   # 配置 Nginx的 yum源
                       [nginx-stable]
                       name=nginx stable repo
                       baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
                       gpgcheck=0
       				   enabled=1
[root@137~]# scp -r nginx.repo  [email protected]:/etc/yum.repos.d/
[root@137~]# scp -r nginx.repo  [email protected]:/etc/yum.repos.d/
[root@137~]# scp -r nginx.repo  [email protected]:/etc/yum.repos.d/
[root@137~]#  yum install -y yum-utils nginx
[root@138~]#  yum install -y yum-utils nginx
[root@139~]#  yum install -y yum-utils nginx
[root@141~]#  yum install -y yum-utils nginx
在 139、141主机配置模拟后端的Web服务器,并启动
[root@139 ~]# echo "This is 11111111" > /usr/share/nginx/html/index.html && systemctl  start  nginx
[root@141 ~]# echo "This is 22222222" > /usr/share/nginx/html/index.html && systemctl  start  nginx

在  137、138主机配置Nginx主备代理,并启动
[root@137 ~]#  
echo "upstream index {
        server 192.168.230.139:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.230.141:80 weight=2 max_fails=2 fail_timeout=2;
}       
server {
        listen 80;
        server_name     localhost;
        access_log  /var/log/nginx/host.access.log  main;
        
        location / {
         proxy_pass http://index;
         proxy_redirect default;
         proxy_set_header Host \$http_host;
         proxy_set_header X-Real-IP \$remote_addr;
         proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        }
}" > /etc/nginx/conf.d/default.conf && systemctl start nginx
[root@137 ~]# scp -r /etc/nginx/conf.d/default.conf  [email protected]:/etc/nginx/conf.d/
[root@138 ~]# systemctl start nginx

Keepalive配置

先一个代理Nginx的检测脚本,如果代理Nginx服务器宕调,就停用Keeplived,从而达到切换代理服务器的作用 ,这个脚本将在Keepalived配置中引用

主 
[root@137~]# echo "#!/bin/bash					        
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then						    
	systemctl stop keepalived
fi " >	/etc/keepalived/check_nginx_status.sh  && 	chmod a+x /etc/keepalived/check_nginx_status.sh


备
[root@138~]# echo "#!/bin/bash					        
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then						    
	systemctl stop keepalived
fi " >	/etc/keepalived/check_nginx_status.sh  && 	chmod a+x /etc/keepalived/check_nginx_status.sh

主 keepalivedd 下载配置并启动 
[root@137 ~]# yum install -y keepalived && echo "! Configuration File for keepalived

global_defs {
     
   router_id directory1   # Keepalived 设备标识 其他备用的保持不一致
}

vrrp_script check_nginx {
     
   script "/etc/keepalived/check_nginx_status.sh"
   interval 5
}    ######需要一个检Web服务器是否正常脚本,如果不正常的话,就关闭Keepalived ,从而起到漂移的作用


vrrp_instance VI_1 {
          
    state MASTER          #定义主还是备
    interface ens33       #选择vip网卡 主备要选择一张网卡
    virtual_router_id 80  # Keepalived 集群ID 主备要一致
    priority 100          #权重
    advert_int 1          #心跳检测 为1s
    authentication {
     
        auth_type PASS    #校验账号 ,主备一致
        auth_pass 1111    #校验密码 ,主备一致
    }
    virtual_ipaddress {
     
        192.168.230.250/24    #虚拟 ip
    }
}" > /etc/keepalived/keepalived.conf && systemctl start keepalived && systemctl enable keepalived


 备 Keepalived 下载配置并启动
 [root@138 ~]# yum install -y keepalived && echo "! Configuration File for keepalived

global_defs {
     
   router_id directory2    # Keepalived 设备标识 其他备用的保持不一致
}

vrrp_script check_nginx {
     
   script "/etc/keepalived/check_nginx_status.sh"
   interval 5
}  ##### 需要一个检Web服务器是否正常脚本,如果不正常的话,就关闭Keepalived ,从而起到漂移的作用

vrrp_instance VI_1 {
     
    state BACKUP             #设置为backup
    interface ens33          #选择vip网卡 主备要选择一张网卡
    nopreempt                #不抢占资源(VIP)
    virtual_router_id 80     # Keepalived 集群ID 主备要一致
    priority 50              #权重50
    advert_int 1	         #信测检测间隔1s
    authentication {
     
        auth_type PASS       #校验账号 ,主备一致
        auth_pass 1111       #校验密码 ,主备一致
    }
    virtual_ipaddress {
     
        192.168.230.250/24   #虚拟 ip
    }
} " > /etc/keepalived/keepalived.conf && systemctl start keepalived && systemctl enable keepalived

效果

Nginx+Keepalived高可用_第4张图片

你可能感兴趣的:(Nginx,Keepalived,Nginx,Keepalived,高可用)