首页 > 系统相关 >Keepalived+Nginx实现高可用负载均衡

Keepalived+Nginx实现高可用负载均衡

时间:2023-02-13 14:45:19浏览次数:55  
标签:负载 etc Keepalived keepalived 192.168 Nginx usr nginx local

最近刚学习了负载均衡的知识,昨天实战了一下,但是遇到了一些奇怪的问题,查找无方之后,自己探索摸出了原因,所以今天就带大家实战一遍,避免走坑。

提前准备
VMwareCentOS7(两台)

方案规划

VIP IP Nginx port 主从
192.168.100.241 192.168.100.70 80 master
192.168.100.241 192.168.100.3 80 backup

一、虚拟机的网络适配器修改为桥接模式
在这里插入图片描述
二、更新yum

$ yum upgrade

三、安装依赖

$ yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

四、安装wget

$ yum install -y wget

五、下载Nginx、Keepalived

$ cd /home
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz

六、解压

$ tar -zxvf nginx-1.13.0.tar.gz
$ tar -zxvf keepalived-2.0.20.tar.gz

七、安装Nginx
① 创建Nginx目录

$ mkdir /usr/local/nginx

② 进入Nginx解压文件目录,配置安装路径

$ cd /home/nginx-1.13.0
$ ./configure --prefix=/usr/local/nginx

③ 编译安装

$ make && make install

④ 设置Nginx开机自启

$ vi /etc/rc.local

#末尾加入
/usr/local/nginx/sbin/nginx

⑤ 修改页面标识

$ cd /usr/local/nginx/html
$ vi /index.html

如图,在h1标签中的nginx后加上-1,另一台的加上-2
在这里插入图片描述
⑥ 启动nignx

$ ./usr/local/nginx/sbin/nginx

在这里插入图片描述
在这里插入图片描述
八、安装Keepalived
① 创建keepalived目录

$ mkdir /usr/local/keepalived

② 进入keepalived解压目录,配置安装路径

$ cd /home/keepalived-2.0.20
$ ./configure --prefix=/usr/local/keepalived

③ 编译安装

$ make && make install

④ 创建目录,复制keepalived的配置到该目录

$ mkdir /etc/keepalived
$ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

⑤ 将keepalived安装service服务

$ cp /home/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
$ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# 以下两步如果提示已存在则忽略
$ ln -s /usr/local/sbin/keepalived /usr/sbin/
$ ln -s /usr/local/keepalived/sbin/keepalived /sbin/

⑥ 设置keepalived服务开机启动

$ chkconfig keepalived on

⑦ 编辑keepalived配置(192.168.100.70)

$ vi /etc/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id nginx_master
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2 ## 检测时间间隔
    weight -20 ## 如果条件成立,权重 -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 58
    mcast_src_ip 192.168.100.70
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.100.241
    }
}

⑧ 编辑keepalived配置(192.168.100.3)

$ vi /etc/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id nginx_backup_01
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 58
    mcast_src_ip 192.168.100.3
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.100.241
    }
}

特别注意interface的值要跟你的网卡(外网)名称一样, ip addr 可查看到名称
⑨ 编写Nginx状态检测脚本

$ touch /etc/keepalived/nginx_check.sh
$ vi /etc/keepalived/nginx_check.sh

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
fi
fi

⑩ 给nginx_check.sh加上执行权限

$ chmod +x /etc/keepalived/nginx_check.sh

⑪ 启动keepalived

$ service keepalived start
Starting keepalived (via systemctl):                       [  确定  ]

⑫ 查看日志

$ tail -fn 200 /var/log/messages

在这里插入图片描述
⑬ 查看网卡信息

$ ip addr

在这里插入图片描述
⑭ 在浏览器上输入VIP地址
在这里插入图片描述
⑮ 关闭master(192.168.100.70)的Keepalived和Nginx

$ service keepalived stop
$ /usr/local/nginx/sbin/nginx -s stop

⑯ 查看backup(192.168.100.3)的网卡信息

$ ip addr

在这里插入图片描述
⑰ 刷新浏览器
在这里插入图片描述

标签:负载,etc,Keepalived,keepalived,192.168,Nginx,usr,nginx,local
From: https://www.cnblogs.com/Smile-yun-1996/p/17116273.html

相关文章

  • 使用Docker来配置Nginx映射静态H5页面
    前提:需要预装docker1、拉取Nginx镜像$dockerpullnginx2、挂载镜像$dockerrun--namenginx-p80:80-dnginx3、查看挂载的镜像,获取CONTAINERID$dockerps......
  • docker 安装nginx1.22.1
    1,拉取镜像:dockerpullnginx:1.22.12,创建映射目录mkdir-p/docker/nginx3,创建临时容器用于复制配置信息dockerrun--namenginx-p80:80-dnginx:1.22.14,拷贝......
  • Nginx日志割接,生成周期日志
    #!/bin/bashlogs_path="/usr/local/nginx-1.14.2/logs/any/"pid_path="/usr/local/nginx-1.14.2/logs/nginx.pid"mv${logs_path}access.log${logs_path}access_$(dat......
  • 详解Nginx配置文件nginx.conf的每行含义
     Nginx配置文件的位置随着安装方式的不同,所在的位置也会不同,通过yum/dnf方式安装,那配置文件是在/etc/nginx/nginx.conf;通过手动编译安装的话可以指定配置文件位置,不指定的......
  • Keepalived安装与配置
    Keepalived的安装过程keepalived的安装非常简单,以操作系统环境Centos7为例,建议通过yum方式直接安装:yuminstallkeepalived如果需要lvs功能,还需要安装ipvs模块:yumins......
  • centos下安装部署nginx
    1.在安装Nginx之前,要确保已经安装了需要的软件:gcc、pcre-devel、zlib-devel、openssl-devel。如果没有安装,执行下面命令。  yum-yinstallgccpcre-develzlib-deve......
  • confluence 在高负载期间变慢并超时
     由于数据库连接池,Confluence在高负载期间变慢并超时诊断在减速过程中需要10线程转储间隔20-30秒并分析线程转储。您会发现许多HTTP线程正在等待获取与数据库......
  • .Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换
    原文网址:https://www.cnblogs.com/linhuiy/p/12029652.html1.什么是API网关API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API。......
  • Nginx优化与防盗链
    Nginx优化与防盗链......
  • Nginx - 深入理解nginx的处理请求、进程关系和配置文件重载
    概述Nginx的系统学习整理的第三篇博客,主要介绍nginx的应用场景和架构基础,以便更好的理解,再生产环境中进行性能调优。Nginx的三个主要应用场景1.静态资源服务,通过本地文......