nginx+keepealived 部署高可用

nginx用docker部署

1、安装docker
2、拉取nginx镜像:docker pull nginx
3、启动nginx:docker run -d -p 80:80 --name nginx nginx:latest
4、新建相关目录和文件夹(用来挂载容器内的文件):
    ·mkdir -p /opt/nginx/{logs,conf,conf.d}
    ·touch /opt/nginx/logs/{access.log,error.log}
5、拷贝容器内的配置文件到本机:
    ·docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/conf
    ·docker cp nginx:/etc/nginx/conf.d/default.conf /opt/nginx/conf.d
    ·docker cp nginx:/usr/local/nginx/html /opt/nginx/
6、删除容器:
    ·docker rm -f nginx
7、启动容器并挂载文件:
docker run -d --name nginx -p 80:80 \
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx \
nginx:latest

8、测式、配置负载均衡即可

 

安装配置keepalived

1、下载包到opt目录下:wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
2、解压:tar -xf keepalived-2.0.20.tar.gz
3、安装依赖:
    ·yum install -y gcc openssl-devel popt-devel
4、到解压后的目录下:
    ·cd /opt/keepalived-2.0.20
5、编译安装
    ·创建目录:mkdir /usr/local/keepalived
    ·./configure --prefix=/usr/local/keepalived        #指定安装路径
    ·make && make install
    
6、拷贝文件到相应的目录下:
    ·cp /opt/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/            #拷贝安装包下的文件
    · mkdir /etc/keepalived    #创建目录
    ·cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/            #拷贝安装目录下的文件
    ·cp /opt/keepalived-2.0.20/keepalived/etc/sysconfig/keepalived /etc/sysconfig/        #拷贝安装包下的文件
    ·cp /usr/local/keepalived/sbin/keepalived /usr/sbin/                                    #拷贝安装目录下的文件
    
7、配置keepalived配置文件:
    ·vim /etc/keepalived/keepalived.conf
------------------------------------------------------
! Configuration File for keepalived

global_defs {
        #keepalived机器标识,无特殊作用,一般为机器名
        router_id  LVS_DEVEL
}

# 检查nginx状态的脚本,健康监测脚本、chk_nginx为脚本名
vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh" # 脚本路径
        interval 2 # 脚本执行间隔时间
        weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33  # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
    virtual_router_id 100  # 虚拟路由编号,主从要一至
    priority 100 # 优先级,数值越大,获取处理请求的优先级越高 master要大于slave
    advert_int 1   ##主备之间通信检查的时间间隔,单位秒
    unicast_src_ip 192.168.177.197 # 本机ip


    #检查脚本,与vrrp_script对应
    track_script {
        chk_nginx
    }

    ##keepalived之间认证类型为密码
    authentication {
        auth_type PASS  # 指定认证方式。PASS简单密码认证(推荐),AH:IPSEC认证(不推荐)
        auth_pass 1111  # 指定认证所使用的密码。最多8位
    }

     ##虚拟IP池
     virtual_ipaddress {  
        # 指定VIP地址、访问地址、虚拟ip随意定义
        172.33.1.12/24
    }

}
-----------------------------------------------------------------------------------------------
    
    
8、编写脚本:
    ·vim /etc/keepalived/nginx_check.sh        #脚本路径和名称要和上面keepalived.conf配置文件里的要一致
--------------------------------------------------------
#!/bin/bash
#version 0.0.1
#当nginx进程不存在时,会自动重启nginx服务;
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
     docker restart nginx     #重启nginx
     sleep 2
     if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        systemctl stop keepalived
     fi
fi
-------------------------------------------------------------
9、给脚本授权:chmod 777 nginx_check.sh


注:以上操作主备一致、需要修改的是keepalived.conf配置文件里的本机ip、state、priority即可

 

测式:

1、先使用虚拟ip(VIP)访问网页:192.168.177.15  或curl 192.168.177.15    #注意这里负载均衡nginx设置的端口为80

#如果虚拟机可以访问,而本地浏览器不能访问,检查一下本地与虚拟ip的通讯

 

2、测式停掉nginx、看脚本会不会自动重启nginx

 

3、测式停掉主机:keepalived.service服务、看备机上 ip a  网卡 'inet 192.168.177.15/24'  是否转移到备机、网页是否能继续访问

posted @ 2024-06-30 22:31  little小新  阅读(21)  评论(0编辑  收藏  举报