nginx+keepalived

主机 作用
192.168.88.140 keepalived主  nginx负载调度
192.168.88.144 keepalived备  nginx负载调度
192.168.88.136 Web服务
192.168.88.139 Web服务

1、配置Web服务器

两台相同

yum install -y nginx
systemctl start nginx

echo "hostname -i Web" > /usr/share/nginx/html/index.html
systemctl restart nginx

nginx+keepalived_第1张图片

2、配置nginx负载均衡

两台相同

upstream websrvs{
        server 192.168.88.136:80;
        server 192.168.88.139:80;
}
server {
        location / {
            proxy_pass http://websrvs;
            index index.html;
        }
}

 3、配置keepalived

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL2
}

vrrp_script check_nginx {
    script "killall -0 nginx"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.88.11
    }
}
track_script {
        check_nginx
}

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL1
}

vrrp_script check_nginx {
    script "killall -0 nginx"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        192.168.88.11
    }
}
track_script {
    check_nginx
}

                  
                    

测试vip在主上

nginx+keepalived_第2张图片

测试负载均衡

nginx+keepalived_第3张图片

 

 4、故障测试

①将主keepalived停掉 vip漂移到备 

nginx+keepalived_第4张图片

nginx+keepalived_第5张图片

 ②停掉一个web服务器(136) keepalived将宕机的Web服务器移除

nginx+keepalived_第6张图片

 

你可能感兴趣的:(nginx,服务器,运维)