Keepalived + nginx实现高可用负载均衡

实验环境:redhat6.5
server1:172.25.60.1 server2:172.25.60.2
server3:172.25.60.3 server4:172.25.60.4
server2,server3装有httpd服务开启且测试页面分别为server2 server3

Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,通过配合Nginx可以实现web前端服务的高可用。
Keepalived实现的基础是VRRP协议,Keepalived就是巧用VRRP协议来实现高可用性(HA)的.
VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个),而在路由器组内部,如果实际拥有这个对外IP的路由器如果工作正常的话就是MASTER,或者是通过算法选举产生,MASTER实现针对虚拟路由器IP的各种网络功能,如ARP请求,ICMP,以及数据的转发等;其他设备不拥有该IP,状态是BACKUP,除了接收MASTER的VRRP状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP将接管原先MASTER的网络功能。

VRRP协议:使用多播数据来传输VRRP数据,VRRP数据使用特殊的虚拟源MAC地址发送数据而不是自身网卡的MAC地址,VRRP运行时只有MASTER路由器定时发送VRRP通告信息,表示MASTER工作正常以及虚拟路由器IP(组),BACKUP只接收VRRP数据,不发送数据,如果一定时间内没有接收到MASTER的通告信息,各BACKUP将宣告自己成为MASTER,发送通告信息,重新进行MASTER选举状态。

1.源码安装keepalived

上篇已经说明,但配置文件有所改变

[root@server1 ~]# vim /etc/keepalived/keepalived.conf
  1 ! Configuration Filefor keepalived
  2
  3 global_defs {
  4    notification_email {
  5         root@localhost      
  6    }
  7    notification_email_from keepalived@server1
  8    smtp_server 127.0.0.1      
  9    smtp_connect_timeout 30    
 10    router_id LVS_DEVEL
 11    vrrp_skip_check_adv_addr
 12    vrrp_strict
 13    vrrp_garp_interval 0
 14    vrrp_gna_interval 0
 15 }
 16
 17 vrrp_instance VI_1 {
 18     state MASTER        
 19     interface eth0      
 20     virtual_router_id 20    
 21     priority 100            
 22     advert_int 1
 23     authentication {
 24         auth_type PASS
 25         auth_pass 1111
 26     }
 27     virtual_ipaddress {
 28         172.25.60.100/24  #虚拟ip
 29     }
 /etc/init.d/keepalived start

 [root@server4 ~]# vim /etc/keepalived/keepalived.conf #配置文件和server1只有以下区别
 7    notification_email_from keepalived@server4
18     state BACKUP     
21     priority 50      
/etc/init.d/keepalived start

2.源码安装nginx,之前也有提到

tar zxf nginx-1.12.0.tar.gz
cd /root/nginx-1.12.0/atuo/cc/
vim gcc  ##去掉debug模式编译,压缩其大小
cd /root/nginx-1.12.0
./configure --prefix=/usr/local/nginx #安装所需依赖包开始编译
make && make install
vim /usr/local/nginx/conf/nginx.conf
 17 http {
 18         upstream westos {
 19         server 172.25.60.2:80;
 20         server 172.25.60.3:80;
 21         }
121 server {
122         listen 80;
123         server_name www.westos.org;
124 
125         location / {
126                 proxy_pass http://westos;
127         }
128 }
cd /usr/local/nginx/sbin/
./nginx -t
./nginx
cd ..
scp -r nginx/ server4:/usr/local/
iptables -L
iptables -F

物理机器解析 vim /etc/hosts
172.25.60.100 www.westos.org

测试:

Keepalived + nginx实现高可用负载均衡_第1张图片

Keepalived + nginx实现高可用负载均衡_第2张图片

[root@server1 ~]# /etc/init.d/keepalived stop

Keepalived + nginx实现高可用负载均衡_第3张图片

Keepalived + nginx实现高可用负载均衡_第4张图片

3.上面的设置是最基础的设置,实现的功能是如果主服务器的Keepalived停止服务(一般情况下服务器宕机),则将虚拟IP切换至从服务器,主服务器恢复后从新切换回主服务器。

 但是很多情况下我们面临的处境是nginx挂掉了,而这个时候Keepalived就不能发挥作用,这时候就需要我们来改良下Keepalived了。通过向Keepalived添加一个自定义脚本来监控neginx的运行状态,如果nginx进程结束,则kill Keepalived进程,以此来达到主从服务器的切换功能

当脚本检测到nginx没有运行的时候会尝试去启动nginx,如果失败则停掉keepalived进程

[root@server1 ~]# vim /opt/nginx_check.sh
#!/bin/bash
curl http://127.0.0.1/index.html -o /dev/null -s || /usr/local/nginx/sbin/nginx
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop &> /dev/null
fi
~  
chmod +x nginx.sh
scp nginx_check.sh server4:/opt/                   

修改server1的配置文件,在中间添加脚本实现

[root@server1 ~]# vim /etc/keepalived/keepalived.conf
  1 ! Configuration File for keepalived
  2 
  3 vrrp_script nginx_check {
  4         script /opt/nginx_check.sh #脚本链接
  5 #       weight -2 #脚本结果导致的优先级变更
  6         interval 2 #脚本执行间隔
  7 }
  8 
  9 global_defs {
 10    notification_email {
 11         root@localhost
 12 }
 13    notification_email_from keepalived@server1
 14    smtp_server 127.0.0.1
 15    smtp_connect_timeout 30
 16    router_id LVS_DEVEL
 17    vrrp_skip_check_adv_addr
 18    vrrp_strict
 19    vrrp_garp_interval 0
 20    vrrp_gna_interval 0
 21 }
 22 
 23 vrrp_instance VI_1 {
 24     state MASTER
 25     interface eth0
 26     virtual_router_id 51
 27     priority 100
 28     advert_int 1
 29     authentication {
 30         auth_type PASS
 31         auth_pass 1111
 32     }
 33     virtual_ipaddress {
 34         172.25.60.100/24
 35     track_script {  
 36         nginx_check  #添加脚本执行
 37     }
 38 }
 39 }
server4服务端同理
 7    notification_email_from keepalived@server4
18     state BACKUP     
21     priority 50   

4.测试:
配置完成,server1 server4 启动nginx和keepalived
浏览器输入 www.westos.org
将server1 vernginx有关进程全部关闭,执行脚本内容,keepalived stop
服务转移到server4

Keepalived + nginx实现高可用负载均衡_第5张图片

你可能感兴趣的:(linux运维企业部分)