Nginx + Keepalived 高可用方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx + Keepalived 高可用方案相关的知识,希望对你有一定的参考价值。

防火墙:外网口填写的是电信运营商给的公网IP地址,然后映射内网vip地址,再开启一些安全策略。

nginx + Keepalived:Nginx 接受到用户的请求后copy一份 转给后端的某台Web服务器,如果已经接收到请求的Web服务器down机了,此时Nginx调度器会 把请求再copy一份给另一台Web服务器,直至完成用户的响应。

Keepalived 负责虚拟出一个虚拟IP 称 VIP

WebServer:负责解析用户的请求抓取相应的数据给前端的Nginx调度器,关于WebServer优化,请看下集续讲。

技术图片

1、Nginx负载均衡的配置文件可以是保留用到的其它部分全注释或者删除

Nginx调度器一:IP 192.168.1.16
[[email protected] ~]# cat /etc/nginx/conf.d/default.conf
upstream groupweb
server 192.168.1.21;
server 192.168.1.22;
server 192.168.1.23;

server
listen 80;
server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / 
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://groupweb;

Nginx调度器二: IP 192.168.1.17
[[email protected] ~]# cat /etc/nginx/conf.d/default.conf
upstream groupweb
server 192.168.1.21;
server 192.168.1.22;
server 192.168.1.23;

server
listen 80;
server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / 
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://groupweb;

2、Keepalived 的配置文件的修改,保留需要的,其它部分建议注释或者删除

Keepalived高可用一 IP: 192.168.1.16
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
vrrp_instance VI_1
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication
auth_type PASS
auth_pass 1111

virtual_ipaddress
192.168.1.100/24

Keepalived高可用二 IP: 192.168.1.17
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
vrrp_instance VI_1
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication
auth_type PASS
auth_pass 1111

virtual_ipaddress
192.168.1.100/24

3、Web服务器 :为了简单方便 我就直接装了Apache服务

Web服务器一 IP :192.168.1.21
[[email protected] ~]# echo web1 > /var/www/html/index.html

Web服务器二 IP :192.168.1.22
[[email protected] ~]# echo web2 > /var/www/html/index.html

Web服务器三 IP :192.168.1.23
[[email protected] ~]# echo web3 > /var/www/html/index.html

以上是关于Nginx + Keepalived 高可用方案的主要内容,如果未能解决你的问题,请参考以下文章

超详细!nginx高可用方案:keepalived+nginx

超详细!nginx高可用方案:keepalived+nginx

Nginx + Keepalived 高可用方案

LVS+Keepalived+Nginx基于DR模式构建高可用方案

Keepalived+Nginx实现双主高可用负载均衡

nginx学习——nginx高可用方案nginx+keepalived