nginx+keepalived实现负载均衡nginx的高可用

时间:2022-12-19 10:59:40

准备四台服务器

两台做主备,另外两台做访问

192.168.1.120 master

192.168.1.121 backup

192.168.1.122 nginx

192.168.1.123 nginx

安装keepalived

yum -y install gcc pcre-devel zlib-devel openssl-devel

yum -y install popt-devel

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure

make&&make install

 

漫长的等待后.....

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp
/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/

加入启动服务 注册成服务

echo "/etc/init.d/keepalived start" >> /etc/rc.local

  chkconfig --add keepalived

配置master nginx 192.168.1.110 和 192.168.1.121 

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  logs/access.log;
    sendfile        on;
 upstream 120.com {
      #ip_hash;
      server 192.168.1.122:80;
      server 192.168.1.123:80; }
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            proxy_pass http://120.com;
            include        fastcgi_params;
        }

    }

    include vhost/*.conf;

}

然后访问试试

nginx+keepalived实现负载均衡nginx的高可用nginx+keepalived实现负载均衡nginx的高可用

nginx+keepalived实现负载均衡nginx的高可用nginx+keepalived实现负载均衡nginx的高可用

实现了负载均衡调用另外两台服务器

配置master keepalived 

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
        25427801@qq.com
   }
   notification_email_from 25427801@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id nginx_master
}
vrrp_script check_nginx_is_run {
    script "/etc/keepalived/check_nginx_is_run.sh"
    interval 2
    weight 2
}
# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
    state MASTER
    interface ens33 #你的网卡
    virtual_router_id 51
    priority 100
    advert_int 1
    track_interface {
        ens33
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.51
    }
    track_script {
        check_nginx_is_run
    }
}

配置backup keepalived

! Configuration File for keepalived

global_defs {
   notification_email {
        25427801@qq.com
   }
   notification_email_from 25427801@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id nginx_backup
}
vrrp_script check_nginx_is_run {
    script "/etc/keepalived/check_nginx_is_run.sh"
    interval 2
    weight 2
}
# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    track_interface {
        ens33
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.51
    }
    track_script {
        check_nginx_is_run
    }
}

安装 killall 命令

yum install psmisc

编写 shell 脚本

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/local/nginx/sbin/nginx                #重启nginx
      sleep 3
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败,则停掉keepalived服务,进行VIP转移
              killall keepalived                    
      fi
fi

设置可执行命令

chmod a+x check_nginx_is_run.sh

 

如果你是window下放进去的,会有 

-bash: ./check_nginx_is_run.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录

解决办法

sed -i 's/\r$//' check_nginx_is_run.sh

查看master keepalived 日记 vim /var/log/messages

nginx+keepalived实现负载均衡nginx的高可用

查看 backup 

nginx+keepalived实现负载均衡nginx的高可用

 

好了不说了 停止nginx ,再不行就关机 192.168.1.120 or 121

如果你service nginx stop 来测试,你会发现关不掉,因为脚本重启了

你会发现OK了~ nginx+keepalived实现负载均衡nginx的高可用nginx+keepalived实现负载均衡nginx的高可用

 nginx+keepalived实现负载均衡nginx的高可用