04.Keepalived实战----Keepalived实现单主模式的Nginx反向代理的高可用

在这里插入图片描述

1.环境准备

节点名称ip地址
client10.0.0.166
ka1(主节点)10.0.0.154 vip:10.0.0.105
ka2(备节点)10.0.0.155 vip:10.0.0.105
web110.0.0.100
web210.0.0.101

2.相关配置

2.1 后端http服务器上创建网页

#在两个后端服务器上创建网页,作为测试效果使用。
#web1:
[root@web1 ~]# yum install httpd  -y
[root@web1 ~]# echo " 10.0.0.100--web1 " > /var/www/html/index.html
[root@web1 ~]#  systemctl start httpd
#-------------------------------------------------------------------------
#web2:
[root@web2 ~]# yum install httpd  -y
[root@web2 ~]# echo " 10.0.0.101--web2 " > /var/www/html/index.html
[root@web2 ~]# systemctl start httpd

2.2 配置nginx服务

1.在两个节点都配置nginx反向代理

#web1:
#安装nginx
root@ka1:/etc/keepalived# apt -y install nginx
root@ka1:~# vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    upstream websrvs {
        server 10.0.0.100:80;
        server 10.0.0.101:80;
    }
    server {
        listen 80;
        location /{
            proxy_pass http://websrvs/;
        }
    }
}

#web2:
#安装nginx
root@ka2:/etc/keepalived# apt -y install nginx 
root@ka2:~# vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    upstream websrvs {
        server 10.0.0.100:80;
        server 10.0.0.101:80;
    }
    server {
        listen 80;
        location /{
            proxy_pass http://websrvs/;
        }
    }
}

2.check_nginx脚本

#web1:
root@ka1:~# cat /etc/keepalived/check_nginx.sh
#!/bin/bash
/usr/bin/killall -0 nginx
# 实现切换时,自动重启服务
# /usr/bin/killall -0 nginx || systemctl restart nginx

# 脚本添加执行权限
root@ka1:~# chmod a+x /etc/keepalived/check_nginx.sh

#web2:
root@ka2:~# cat /etc/keepalived/check_nginx.sh
#!/bin/bash
/usr/bin/killall -0 nginx
# 实现切换时,自动重启服务
# /usr/bin/killall -0 nginx || systemctl restart nginx

# 脚本添加执行权限
root@ka2:~# chmod a+x /etc/keepalived/check_nginx.sh

2.3 配置keepalived服务

#web1:
root@ka1:~# cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id ka1        
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh" #检查脚本路径
    interval 1
    weight -30      #权重-30
    fall 3
    rise 5
    timeout 2    
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.105/24 dev ens33 label ens33:2    #vip
    }

    track_script {
        chk_nginx
    }

}

#web2:
root@ka2:/etc/nginx# cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id ka2
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh" 
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2    
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.105/24 dev ens33 label ens33:2   #vip
    }

    track_script {
        chk_nginx
    }

}

3. client进行测试


正常情况

#发现vip漂移在ka1上
root@ka1:/etc/nginx# hostname -I
10.0.0.154 10.0.0.105 
root@ka2:/etc/nginx# hostname -I
10.0.0.155 

#client进行curl  vip 测试
#ka1和ka2上的keepalived和nginx都正常的时候
root@client:~# curl 10.0.0.105
<h1> web01 10.0.0.154 </h1>
root@client:~# curl 10.0.0.105
<h1> web01 10.0.0.154 </h1>

情况一: ka1上的keepalived停掉

root@ka1:/etc/nginx# systemctl stop keepalived.service 
#vip漂移到ka2了
root@ka1:~# hostname -I
10.0.0.154
root@ka2:/etc/nginx# hostname -I
10.0.0.155 10.0.0.105 
#client进行curl  vip 测试
root@client:~# curl 10.0.0.105
<h1> web02 10.0.0.155 </h1>

情况二: ka1上的nginx停掉

root@ka1:/etc/nginx# systemctl stop nginx.service 
#vip漂移到ka2了
root@ka1:~# hostname -I
10.0.0.154
root@ka2:/etc/nginx# hostname -I
10.0.0.155 10.0.0.105 
#client进行curl  vip 测试
root@client:~# curl 10.0.0.105
<h1> web02 10.0.0.155 </h1>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值