![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7885cd7d3cb1213e142a75914a97d12e.png)
1.环境准备
节点名称 | ip地址 |
---|
client | 10.0.0.166 |
ka1(主节点) | 10.0.0.154 vip:10.0.0.105 |
ka2(备节点) | 10.0.0.155 vip:10.0.0.105 |
web1 | 10.0.0.100 |
web2 | 10.0.0.101 |
2.相关配置
2.1 后端http服务器上创建网页
[root@web1 ~]
[root@web1 ~]
[root@web1 ~]
[root@web2 ~]
[root@web2 ~]
[root@web2 ~]
2.2 配置nginx服务
1.在两个节点都配置nginx反向代理
root@ka1:/etc/keepalived
root@ka1:~
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/;
}
}
}
root@ka2:/etc/keepalived
root@ka2:~
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脚本
root@ka1:~
/usr/bin/killall -0 nginx
root@ka1:~
root@ka2:~
/usr/bin/killall -0 nginx
root@ka2:~
2.3 配置keepalived服务
root@ka1:~
! Configuration File for keepalived
global_defs {
router_id ka1
}
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 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
}
track_script {
chk_nginx
}
}
root@ka2:/etc/nginx
! 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
}
track_script {
chk_nginx
}
}
3. client进行测试
正常情况
root@ka1:/etc/nginx
10.0.0.154 10.0.0.105
root@ka2:/etc/nginx
10.0.0.155
root@client:~
<h1> web01 10.0.0.154 </h1>
root@client:~
<h1> web01 10.0.0.154 </h1>
情况一: ka1上的keepalived停掉
root@ka1:/etc/nginx
root@ka1:~
10.0.0.154
root@ka2:/etc/nginx
10.0.0.155 10.0.0.105
root@client:~
<h1> web02 10.0.0.155 </h1>
情况二: ka1上的nginx停掉
root@ka1:/etc/nginx
root@ka1:~
10.0.0.154
root@ka2:/etc/nginx
10.0.0.155 10.0.0.105
root@client:~
<h1> web02 10.0.0.155 </h1>