首页 > 代码库 > nginx + keepalived 高可用负载均衡

nginx + keepalived 高可用负载均衡

虚拟IP:    192.168.2.52

 

nginx-1:   192.168.2.50

 

nginx-2:   192.168.2.51


关闭/etc/sysconfig/selinux

SELINUX=disabled

 

nginx-1

yum install keepalived

vim /etc/keepalived/keepalived.conf
---------------------------------------------------

! Configuration File for keepalived

global_defs {                    
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER                 //定义为MASTER主机
    interface eth0
    virtual_router_id 51      //虚拟id
    priority 100          //优先级
advert_int 1                   //检测间隔
    authentication {
        auth_type PASS          //认证方式
        auth_pass abcd          //认证密码
    }
    virtual_ipaddress {
       
192.168.2.52          //虚拟ip,即VIP
 }

-------------------------------------------------

chkconfig keepalived on

service keepalived start

 

 

nginx-2

yum install keepalived

vim /etc/keepalived/keepalived.conf

--------------------------------------------------只改动如下

state BACKUP       //定义为BACKUP

priority 99         //优先级

virtual_ipaddress {

        192.168.2.52

}

-------------------------------------------------

chkconfig keepalived on

service keepalived start

ip a

技术分享                    技术分享       

已经绑定成功


检测keepalived自动切换

客户机:

ping 192.168.2.52 -t

nginx-1: 关闭keepalived服务

service keepalived stop

客户机是否正常ping



安装nginx

因为centos 没有nginx,先安装第三方epel-release

 

yum install -y epel-release

yum install -y nginx

 

负载均衡

vi /etc/nginx/nginx.conf

添加以下内容

------------------------

    # for more information.

    include /etc/nginx/conf.d/*.conf;


    upstream zhizhimao.com {

    server  192.168.2.101:80;    #web服务器

    server  192.168.2.100:80;    #web服务器

        }

}

------------------------

反向代理

vi /etc/nginx/conf.d/default.conf

--------------------------------------------

server {

   listen       80;

   listen       [::]:80default_server;

   server_name     zhizhimao.com;             #如只做代理填写IP

   root        /usr/share/nginx/html;

 

    #Load configuration files for the default server block.

   include /etc/nginx/default.d/*.conf;

 

   location / {

    proxy_pass http://zhizhimao.com;          #如只做代理填写IP

    }

 

   error_page 404 /404.html;

       location = /40x.html {

    }

 

   error_page 500 502 503 504 /50x.html;

       location = /50x.html {

    }

}

-----------------------------------------------------

chkconfig nginx on

service nginx start

防火墙打开80端口

本文出自 “Shell” 博客,请务必保留此出处http://zhizhimao.blog.51cto.com/3379994/1940131

nginx + keepalived 高可用负载均衡