Keepalived+Nginx配置实战

该博客介绍了如何通过Keepalived和Nginx配置实现负载均衡和高可用性。在实验环境中,配置了两台web服务器和两台负载均衡器,详细步骤包括web服务器及负载均衡器的Nginx安装、配置,以及Keepalived的安装、配置和启动。通过健康检查脚本来确保服务的稳定性和故障切换。
摘要由CSDN通过智能技术生成

实验目的:

  • 实现负载均衡和高可用

实验环境:

两台web-server

  • web-server-1: 192.168.122.166
  • web-serbver-2: 192.168.122.152

两台负载均衡器

  • master: 192.168.122.221
  • backup: 192.168.122.192

实验部署:

web-server部署(两台都需要做)
安装nginx,并且保证nginx正常运行

[root@web-1 ~]# yum -y install nginx
[root@web-2 ~]# yum -y install nginx

分别要在web-server上创建一个测试界面

[root@web-1 ~]# echo "web-1 site" >/usr/share/nginx/html/index.html
[root@web-2 ~]# echo "web-2 site" >/usr/share/nginx/html/index.html 

启动nginx服务

[root@web-1 ~]# systemctl start nginx
[root@web-2 ~]# systemctl start nginx

检测web-server是否正常被访问

负载均衡的部署(两台都需要做)

  • master和backup同样安装nginx,因为要先用nginx做负载均衡器

vim /etc/nginx/nginx.conf 添加以下内容
在这里插入图片描述

保证nginx的负载均衡可用,客户端可以访问测试:

Keepalived实现调度器HA

主/备调度器安装软件(安装keepalived)
[root@master ~]# yum install -y keepalived
[root@backup ~]# yum install -y keepalived
修改配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id director1   #辅助改为director2
}

vrrp_instance VI_1 {
    state MASTER        #定义主还是备
    interface eth0    #VIP绑定接口
    virtual_router_id 80  #整个集群的调度器一致
    priority 100         #backup改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.100/24
    }
}

[root@backup ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id director2 
}

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

启动KeepAlived(主备均启动)
[root@master ~]# systemctl start keepalived
[root@master ~]# systemctl enable keepalived
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:13:23:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.122.221/24 brd 192.168.122.255 scope global dynamic eth0
valid_lft 2510sec preferred_lft 2510sec
inet 192.168.122.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever
测试(另找一台客户机)
[root@localhost ~]# curl 192.168.122.100
web-1 site
[root@localhost ~]# curl 192.168.122.100
web-2 site

到此:
可以解决心跳故障keepalived
不能解决Nginx服务故障
可以写一个健康检测脚本,当nginx故障,尝试重启一次,如果起不来,则关闭本机keepalived
[root@master ~]# vim /etc/keepalived/check_nginx_status.sh

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

[root@master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh
[root@master ~]# vim /etc/keepalived/keepalived.conf

! 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 eth0
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.122.100/24
    }
    track_script { 引用脚本
        check_nginx
    }
}

测试:
[root@master ~]# systemctl stop nginx
[root@master ~]# systemctl restart keepalived
[root@master ~]# ps -ef |grep nginx| grep -v grep
root 12323 1 0 17:15 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 12324 12323 0 17:15 ? 00:00:00 nginx: worker process

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值