keepalived高可用Nginx Proxy服务

  • 准备四台主机为host(1-4);
  • host1 host2上配置Nginx调度服务和keepalived服务;
  • 主机host3和主机host4提供Web服务
    • host1:192.168.10.11
    • host2:192.168.10.12
    • host3:192.168.10.13
    • host4:192.168.10.14

安装服务:

#安装服务:给四台主机分别安装所需服务应用

[root@host1 ~]#yum -y install nginx keepalived
[root@host2 ~]#yum -y install nginx keepalived
[root@host3 ~]#yum -y install nginx 
[root@host3 ~]#yum -y install nginx

#编辑测试页:直接把内容重定向即可

[root@host1 ~]#echo "

测试页内容

" > /usr/share/nginx/html/index.html [root@host2 ~]#echo "

测试页内容

" > /usr/share/nginx/html/index.html [root@host3 ~]#echo "

测试页内容

" > /usr/share/nginx/html/index.html [root@host4 ~]#echo "

测试页内容

" > /usr/share/nginx/html/index.html

两台Nginx调度主机上配置Nginx Proxy服务

主机host1上配置Nginx Proxy服务:

keepalived高可用Nginx Proxy服务_第1张图片

主机host2上配置Nginx Proxy服务:

keepalived高可用Nginx Proxy服务_第2张图片

两台Nginx调度主机上配置keepalived服务

  • 这里我采用单主模式!也可以双主模式,双主模式也很简单,keepalived配置文件里再vrrp_instance即可;
  • 这里我定义了两个脚本,稍微演示下keepalived的script功能;

主机host1上配置keepalived服务:

[root@host1 keepalived]#cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id host1
   vrrp_mcast_group4 224.89.51.18
}

vrrp_script chk_down {             # 定义的脚本,意思是/etc/keepalived目录下有down文件就虚拟IP地质转移
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1                      # 检测间隔
   weight -10                      # 降权,也就是减去优先级多少值
   fall 2                          # 检测到两次错误才发生转移
   rise 1                          # 至少正常1次才算正常
}

vrrp_script chk_nginx {            # 定义的脚本,意思是Nginx进程是否在运行,如果没有就虚拟IP地址转移;
        script "killall -0 nginx && exit 0 || exit 1"
        interval 2
        weight -10
        fall 2
        rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 89
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass nQyVIaY1
    }
    virtual_ipaddress {
        192.168.10.88
    }

        track_script {               # 调用定义的脚本
                chk_down
                chk_nginx
        }
}
[root@host1 keepalived]#

主机host2上配置keepalived服务:

[root@host2 keepalived]#cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id host2
   vrrp_mcast_group4 224.89.51.18
}

vrrp_script chk_down {
   scrip "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight -10
   fall 2
   rise 1
}

vrrp_sript chk_nginx {
   script "killall -0 nginx"
   interval 2
   weight -10
   fall 2
   rise 2
}
vrrp_instance VI_1 {
    state BACKUP     <-- 这里是BACKUP
    interface ens33
    virtual_router_id 89
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass nQyVIaY1
    }
    virtual_ipaddress {
        192.168.10.88
    }
    track_script {                # 调用定义的脚本
        chk_nginx
        chk_down
    }
}

在四台主机上启动各自的服务:

systemctl start nginx
systemctl start keepalived

测试:

  • 1. 关闭主机host1的Nginx:systemctl stop nginx

    • 查看host2的keepalived状态和IP地址


      keepalived高可用Nginx Proxy服务_第3张图片
      host2
    • 客户端上测试是否能够访问后端Web服务


      keepalived高可用Nginx Proxy服务_第4张图片
      在客户端主机上
  • 2. 测试调用脚本:这里只检测一项脚本

    keepalived高可用Nginx Proxy服务_第5张图片
    测试脚本,创建down文件和删除down文件,兼顾查看IP

keepalived高可用Nginx Proxy服务_第6张图片
注意降权和地址漂移

你可能感兴趣的:(keepalived高可用Nginx Proxy服务)