keepalived补充

一、keepalived基于服务器nginx挂了,不会自动切换

1.1 书写脚本(时刻检查nginx的状态,如果nginx关闭,keepalived也关闭)

[root@lb01 /server/scripts]# cat check-ngx.sh 
#!/bin/bash

count=`ps -ef|grep nginx|grep -v grep|wc -l`

if [ $count -eq 0 ];then 
  systemctl stop keepalived
fi

注:脚本名字不要包含监控对象的名字

1.2 通过keepalived运行检查nginx服务的脚本

1>给脚本check-ngx.sh 一个执行权限

[root@lb01 ~]#  chmod +x /server/scripts/check-ngx.sh
[root@lb01 ~]# ll /server/scripts/check-ngx.sh 
-rwxr-xr-x 1 root root 116 Jun 17 09:15 /server/scripts/check-ngx.sh
[root@lb01 ~]# 

1.3 配置keepalived

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
……
vrrp_script check_ngx {
   script  /server/scripts/check-ngx.sh
   interval  2
   weight  1
}

vrrp_instance VI_1 {
    ……
    track_script{
        check_ngx
    }
}
[root@lb01 ~]# 

keepalived补充_第1张图片
image

1.4 重启keepalived

systemctl restart keepalived.service

1.5测试

\\lb01上
[root@lb01 ~]# ip a|grep 0.3
    inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 ~]# systemctl stop nginx
[root@lb01 ~]# ip a|grep 0.3
[root@lb01 ~]#                                            \\\虚拟ip飘逸走了

\\lb02上
[root@lb02 ~]# ip a|grep 0.3
    inet 10.0.0.3/24 scope global secondary eth0:1

\\\回到lb01上
[root@lb01 ~]# systemctl start nginx
[root@lb01 ~]# ip a|grep 0.3
[root@lb01 ~]# systemctl start keepalived.service 
[root@lb01 ~]# ip a|grep 0.3   \\虚拟IP自动飘逸回来
    inet 10.0.0.3/24 scope global secondary eth0:1

二、keepalived双主模式

目的:减轻负载均衡的压力

keepalived补充_第2张图片
image

2.1 keepalived双主模式配置

keepalived补充_第3张图片
image

分别重启keepalived服务

\\\lb01
[root@lb01 /etc/keepalived]# systemctl restart keepalived.service [root@lb01 /etc/keepalived]# ip a |egrep "0.3|0.4"
    inet 10.0.0.3/24 scope global secondary eth0:1

\\\\lb02
[root@lb02 /etc/keepalived]# systemctl restart keepalived.service [root@lb02 /etc/keepalived]# ip a |egrep "0.3|0.4"

keepalived双主模式配置

\\\\lb01的keepalived双主配置文件
! Configuration File for keepalived

global_defs {
    router_id lb01
}
vrrp_script jiancha {
script "/server/scripts/jiancha.sh"
interval 2
weight 1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24 dev eth0 label eth0:1  
    }
    track_script {
    jiancha
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.4/24 dev eth0 label eth0:2
    }
}

\\\\\\\lb02的keepalived双主配置文件
! Configuration File for keepalived

global_defs {
    router_id lb02
}

vrrp_instance VI_1 {   
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24 dev eth0 label eth0:1  
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.4/24 dev eth0 label eth0:2
    }
}

让lb01和lb02的nginx配置文件相同

vim /etc/nginx/nginx.conf  :

     upstream  web_pools {
     server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
     server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
     }
#    include /etc/nginx/conf.d/*.conf;
     server {
     listen 80;
     server_name www.oldboy.com;
     location / {
         proxy_pass http://web_pools;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr; 
        }
     }
     server {
     listen 80;
     server_name blog.oldboy.com;
     location / {
         proxy_pass http://web_pools;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
        }
     }

2.1 在命令行curl测试是否正常

\\\lb01
[root@lb01 ~]# curl 10.0.0.3
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.3
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.4
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.4
web01 www.oldboy.com
[root@lb01 ~]# 

\\\lb02
[root@lb02 ~]# curl 10.0.0.3
web02 www.oldboy.com
[root@lb02 ~]# curl 10.0.0.3
web01 www.oldboy.com
[root@lb02 ~]# curl 10.0.0.4
web02 www.oldboy.com
[root@lb02 ~]# curl 10.0.0.4
web01 www.oldboy.com
[root@lb02 ~]#

2.1 在浏览器进行测试

1>先在Windows本地hosts文件中配置解析

10.0.0.3 www.oldboy.com
10.0.0.4 blog.oldboy.com

2>浏览器测试

keepalived补充_第4张图片
image

2.1 测试高可用是否正常

假如将其中1台lb01关机,然后测试www.oldboy.com和blog.oldboy.com在浏览器是否还能正常显示

三、每个域名绑定对应ip

3.1 修改nginx配置

keepalived补充_第5张图片
image

3.2 重启服务

重启服务报Cannot assign requested address的错误
nginx无法把本地不存在的ip地址进行绑定

[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 10.0.0.7:80 failed (99: Cannot assign requested address)   \\不能分配的请求ip地址
nginx: configuration file /etc/nginx/nginx.conf test failed

解决:修改内核参数
在/etc/sysctl.conf文件中,追加net.ipv4.ip_nonlocal_bind = 1
内核生效:sysctl -p

[root@lb01 ~]# cat  /etc/sysctl.conf 
……
net.ipv4.ip_nonlocal_bind = 1
[root@lb01 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@lb01 ~]# 

注:执行net.ipv4.ip_nonlocal_bind = 1,背后修改了哪里?

[root@lb01 ~]# #net.ipv4.ip_nonlocal_bind
[root@lb01 ~]# # /proc/sys/ 
[root@lb01 ~]# #net.ipv4.ip_nonlocal_bind
[root@lb01 ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind  \\修改的是这里
1

四、keepalived高可用故障案例

脑裂/裂脑

原因:
  1>SELinux/防火墙可以开启
  2>高可用服务器对之间心跳线链路故障,导致无法正常通信
  3>高可用服务器对上心跳网卡地址等信息配置不正确,导致发送心跳失败。

解决:
  1>两条心跳线路
  2>只要备任务主挂了,我让你直接挂了
  3>书写一个脚本,发送脑裂发送邮件

你可能感兴趣的:(keepalived补充)