Linux Centos7 keepalived + nginx负载均衡

首先需要服务器关都闭防火墙和selinux

1,准备四台nginx服务器,两台做代理,两台做后端真实服务器。

2,配置两台真实服务器
第一台:

    [[email protected] ~]# systemctl start nginx
    [[email protected] ~]# systemctl enable nginx
    [[email protected] conf.d]# echo "server111" > /usr/share/nginx/html/index.html
    测试:
    [[email protected] conf.d]# curl localhost
    server111

第二台:

    [[email protected] ~]# systemctl start nginx
    [[email protected] ~]# systemctl enable nginx
    [[email protected] conf.d]# echo "server222" > /usr/share/nginx/html/index.html
    测试:
    [[email protected] conf.d]# curl localhost
    server222

3,配置两台nginx代理服务器

    [[email protected] ~]# systemctl start nginx
    [[email protected] ~]# systemctl enable nginx
    [[email protected] ~]# cd /etc/nginx/conf.d/
    [[email protected] conf.d]# cp default.conf proxy.conf
    [[email protected] conf.d]# mv default.conf default.conf.bak
    [[email protected] conf.d]# vi upstream.conf
    upstream nginx_pool {
    server 10.30.161.241:80 weight=1 max_fails=2 fail_timeout=2;
    server 10.30.161.242:80 weight=1 max_fails=2 fail_timeout=2;
    }
    [[email protected] conf.d]# vi proxy.conf
     location / {
     proxy_pass http://nginx_pool;
     proxy_redirect default;
     proxy_set_header Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header REMOTE-HOST $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

将nginx的配置文件拷贝到另一台代理服务器中:

   [[email protected] conf.d]# scp proxy.conf upstream.conf 10.30.161.214:/etc/nginx/conf.d/
    分别重启nginx:
    systemctl restart nginx

4, 两台代理服务器分别安装软件

    master:
    [[email protected] conf.d]# yum install -y keepalived
    [[email protected] conf.d]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
    [[email protected] conf.d]# vim /etc/keepalived/keepalived.conf

配置keepalived,将里面内容全部删除,添加下列内容:

! Configuration File for keepalived
global_defs {
     router_id director1   #辅助改为director2
}

vrrp_instance VI_1 {
        state MASTER        #定义主还是备
        interface ens33     #VIP绑定接口
        virtual_router_id 80  #整个集群的调度器一致
        priority 100         #back改为50
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                10.30.161.200/24   #设置VIP
        }
}
                backup:
                [[email protected]_nginx ~]# cp /etc/keepalived/keepalived.conf
                [[email protected]_nginx ~]# vi /etc/keepalived/keepalived.conf
                配置keepalived,将里面内容全部删除,添加下列内容:
! Configuration File for keepalived

global_defs {
     router_id directory2
}

vrrp_instance VI_1 {
        state BACKUP
        interface ens33
        nopreempt
        virtual_router_id 80
        priority 50
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                10.30.161.200/24
        }
}

5、两台代理服务器分别启动keepalived

    [[email protected]_nginx ~]# systemctl enable keepalived.service
    [[email protected]_nginx ~]# systemctl start keepalived.service

6、检测是否成功

   在master上 ip a  可显示VIP为10.30.161.200/24
  2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:29:a6:12 brd ff:ff:ff:ff:ff:ff
inet 10.30.161.51/24 brd 10.30.161.255 scope global dynamic ens33
   valid_lft 168379sec preferred_lft 168379sec
inet 10.30.161.200/24 scope global secondary ens33
   valid_lft forever preferred_lft forever
inet6 fe80::8cfe:8d87:6478:baee/64 scope link
   valid_lft forever preferred_lft forever
inet6 fe80::d9c7:8228:5b45:afed/64 scope link tentative dadfailed
   valid_lft forever preferred_lft forever

用curl访问VIP,正常轮询

[[email protected] conf.d]# curl 10.30.161.200
server111
[[email protected] conf.d]# curl 10.30.161.200
server222
[[email protected] conf.d]# curl 10.30.161.200
server111
[[email protected] conf.d]# curl 10.30.161.200
server222
 测试当一台master故障
 [[email protected] conf.d]# systemctl stop keepalived
 ip a 发现VIP偏移到backup代理服务器上

到此:
可以解决心跳故障keepalived
但不能解决Nginx服务故障

  1. 扩展对调度器Nginx健康检查(可选)两台都设置
    思路:
    让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
    (1) script

    [[email protected] ~]# vim /etc/keepalived/check_nginx_status.sh

    #!/bin/bash
    #+检查nginx进程是否存在
    counter=$(ps -C nginx --no-heading|wc -l) #此行有服务名
    if [ "${counter}" = "0" ]; then
    #尝试启动一次nginx,停止5秒后再次检测
    systemctl start nginx #启动服务
    sleep 5
    counter=$(ps -C nginx --no-heading|wc -l) #此行有服务名
    if [ "${counter}" = "0" ]; then
    #如果启动没成功,就杀掉keepalive触发主备切换
    service keepalived stop
    fi
    fi
    [[email protected] ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

(2). keepalived使用script

! Configuration File for keepalived

global_defs {
     router_id director1
}
vrrp_script check_nginx {     #健康检测模块调用
     script "/etc/keepalived/check_nginx_status.sh"  #指定脚本
     interval 5  #检查频率,秒
}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 80
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.246.16/24
        }
        track_script { 引用脚本
                check_nginx
        }
}

注:必须先启动nginx,再启动keepalived

原文地址:https://blog.51cto.com/14482279/2437601

时间: 2024-11-05 20:34:03

Linux Centos7 keepalived + nginx负载均衡的相关文章

lvs+keepalived+nginx负载均衡搭建测试

1. 简介 1.1 LVS简介 LVS(Linux Virtual Server),也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目.使用LVS技术要达到的目标是:通过LVS提供的负载均衡技术和Linux操作系统实现一个高性能.高可用的服务器群集,它具有良好可靠性.可扩展性和可操作性.从而以低廉的成本实现最优的服务性能. LVS主要用来做四层负载均衡. 1.2 Keepalived简介 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtu

linux下配置nginx负载均衡例子

准备2台虚拟机: 分别在两个虚拟机上安装tomcat,并在服务器A安装nginx,其中nginx端口设置为了 70. 服务器A的tomcat安装目录: 服务器B的tomcat安装目录: 服务器A的nginx安装目录: 准备test.jsp文件,分别上传到tomcat的 ROOT 目录下: 上传到服务器A的test.jsp : <%@ page language="java" contentType="text/html; charset=UTF-8" page

keepalived+nginx负载均衡+ApacheWeb实现高可用

1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,keepalived除了能够管理LVS软件外,还可以作为其他服务的高可用解决方案软件. keepalived软件主要是通过VRRP协议实现高可用功能的.VRRP是Virtual  Router  Redundancy Protocol(虚拟路由冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由的单点

Linux下部署LVS(DR)+keepalived+Nginx负载均衡

架构部署 LVS/keepalived(master):192.168.21.3 LVS/keepalived(Slave):192.168.21.6 Nginx1:192.168.21.4 Nginx2:192.168.21.5 VIP:192.168.21.10 1.安装ipvsadm.keepalived(Master/Slave) yum -y install keepalived ipvsadm 2.修改keepalived.conf文件 LVS_master cd /etc/keep

linux文件同步+nginx负载均衡分流网站

1.主服务器安装rsync wget https://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz tar -zxvf rsync-3.1.1.tar.gz ./configure --prefix=/usr/local/rsync make && make install cd /usr/local/rsync/ echo "密码" >/usr/local/rsync/rsync.passwd chmod

Keepalived+LVS+Nginx负载均衡之高可用

Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Serve

Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用

上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx负载均衡出现单机故障时,系统正常运行的需求.所以系统架构引入Keepalived组件,实现系统高可用. 一.Keepalived介绍 Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Server)使用,其功能类似于heartbeat,解决单机宕机的问

Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术

一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器: Keepalived 可实现 Nginx负载均衡器双机互备,任意一台机器发生故障,对方都能够将虚拟IP接管过去. Memcache可以实现Tomcat服务器的Sission共享整个拓补如下: 注意: 1.由于服务器有限,IP相同的为同一台机.只是端

Nginx+Tomcat+Keepalived+Memcache负载均衡动离分离技术

一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器: Keepalived 可实现 Nginx负载均衡器双机互备,任意一台机器发生故障,对方都能够将虚拟IP接管过去. Memcache可以实现Tomcat服务器的Sission共享整个拓补如下: 注意: 1.由于服务器有限,IP相同的为同一台机.只是端