Centos7.7+nginx1.8+keepalived负载均衡

nginx源码安装

systemctl stop firewalld
mkdir /opt
cd /opt
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel psmisc
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx
make && make install

/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop/reload
/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/conf/nginx.conf #配置文件
/usr/local/nginx/html/index.html #首页

vim /lib/systemd/system/nginx.service #添加服务,可不添加

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

pkill nginx
systemctl stop nginx
systemctl start nginx
systemctl reload nginx
systemctl enable nginx #开机启动


keepalived安装

yum -y install keepalived systemctl enable keepalived systemctl start keepalived

keepalived master配置

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    router_id master
}
    vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    mcast_src_ip 192.168.163.100
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
  }
      track_script {
        chk_nginx
    }
     virtual_ipaddress {
       192.168.163.200
    }
}

keepalived backup配置

router_id slave
mcast_src_ip 192.168.163.110
state BACKUP
priority 99

配置nginx检测脚本
vim /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 #尝试重新启动nginx
   sleep 2  #睡眠2秒
    if [ $A -eq 0 ];then
        killall keepalived #启动失败,将keepalived服务杀死。将vip漂移到其它备份节点
    fi
fi

systemctl restart keepalived

测试
systemctl stop keepalived
/usr/local/nginx/sbin/nginx -s stop
IP add #查看VIP

systemctl restart keepalived

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值