2019-06-17日 keepalived双主模式

keepalived双主模式

减轻负载均衡压力(两者互为主备)

第一个里程碑

  • lb01配置文件
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id lb01
}


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  
    }

}
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配置文件
[root@bl02 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id lb01
}


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  
    }
}

image.png

第二个 里程碑

  • 配置完成测试
[root@bl02 ~]# ip a |egrep "0.3|0.4"
    inet 10.0.0.4/24 scope global secondary eth0:2
[root@lb01 ~]# ip a |egrep "0.3|0.4"
    inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 ~]# 
[root@lb01 ~]# curl 10.0.0.3
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.3
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.4
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.4
web02 www.oldboy.com
[root@lb01 ~]# 
  • 关闭lb01进行测试
[root@lb01 ~]# systemctl stop keepalived.service 
    inet 10.0.0.4/24 scope global secondary eth0:2
    inet 10.0.0.3/24 scope global secondary eth0:1
[root@bl02 ~]# 

keepalived监控nginx

第一个里程碑

  • 写一个脚本监控nginx状态 如果nginx挂掉,keepalived也关闭
#!/bin/bash
count=`ps -ef |grep nginx |grep -v |wc -l`

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

第二个里程碑

  • 给脚本加上执行权限后写入keepalived的配置文件
[root@lb01 /etc/keepalived]# cat keepalived.bak.script 
I! Configuration File for keepalived

global_defs {
    router_id lb01
}
vrrp_script kep_ngx {
scrpit "/server/scripts/kep_ngx.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 {
    kep_ngx
   }
}

每个域名绑定对应 IP

  • 修改内核参数(针对nginx语法错误)
[root@lb01 ~]# cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1

实际修改

[root@lb01 ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
  • 修改配置文件
[root@lb01 /etc/keepalived]# cat /etc/nginx/nginx.conf 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    
    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;

}
    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;
 server {
    listen 10.0.0.3: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 10.0.0.4: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;
     }
 
   }    

 }

Keepalived高可用故障

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容