keepalived与nginx联动-双主

一、双主说明
二、配置实现

  1. keepalived配置
  2. 修改nginx反向代理监控地址信息

一、双主说明

如果一个负载均衡集群中,所有访问都交给主机,一则浪费从机资源,二则增加主机压力,因此可以配置互为主从,即:

A机为www的主机,bbs的备机
B机为bbs的主机,www的备机

二、配置实现

  1. keepalived配置
    lb01配置[只显示vrrp部分]
    cat /etc/keepalived/keepalived.conf
    vrrp_instance gorup01 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
     auth_type PASS
     auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24 dev eth0 label eth0:1
    }
    }
    vrrp_instance gorup02 {
     state BACKUP
     interface eth0
     virtual_router_id 52
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         10.0.0.4/24 dev eth0 label eth0:1
     }
    }
    lb02配置[只显示vrrp部分]
    cat /etc/keepalived/keepalived.conf
    vrrp_instance gorup01 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
     auth_type PASS
     auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24 dev eth0 label eth0:1
    }
    }
    vrrp_instance gorup02 {
     state MASTER
     interface eth0
     virtual_router_id 52
     priority 150
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         10.0.0.4/24 dev eth0 label eth0:1
     }
    }
  2. 修改nginx反向代理监控地址信息
    修改lb01,lb02的nginx配置文件中,修改www,bbb主机监控的地址分别为10.0.0.3和10.0.0.4即可,示例如下
cat /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream noah {
        server 10.0.0.7:80;
        server 10.0.0.8:80;
        server 10.0.0.9:80;
    }
    server {
        listen       10.0.0.3:80;
        server_name  www.etiantian.com;
        root   html;
        index  index.html index.htm;
        location / {
          proxy_pass http://noah;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
         }   
}
    server {
        listen       10.0.0.4:80;
        server_name  bbs.etiantian.com;
        root   html;
        index  index.html index.htm;
        location / {
          proxy_pass http://noah;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
         }   
    }
}
文档更新时间: 2023-08-09 03:10   作者:月影鹏鹏
IT运维支持-AIWALY-月影工作室