Nginx+keepalived实现LB+HA

本文介绍如何在LB-M和LB-S上配置NGINX实现负载均衡,并通过Keeplived确保高可用性。包括NGINX的安装与配置、Keeplived的安装、配置及启动步骤。

配置NGINX负载均衡

在LB-M和LB-S上进行相同操作

安装NGINX

生产环境最好编译安装,这里直接装个epel源yum安装了

yum install epel-release -y
yum install nginx -y

配置NGINX

cat >> /etc/nginx/nginx.conf << "EOF"

# 这里通过端口配置的是四层负载,也可以通过域名方式,配置七层负载

stream {

    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
    access_log  /var/log/nginx/upstream-access.log  main;
    
    upstream www {
       ip_hash;
       server 192.168.2.101:8001;
       server 192.168.2.102:8001;
    }

    server {
       listen 8001;
       proxy_pass www;
    }
}
EOF

启动NGINX

systemctl restart nginx
systemctl enable nginx

配置Keeplived高可用

安装Keeplived

keeplived--yum安装
yum -y install keepalived
 
keeplived--编译安装
tar -zxvf keepalived-1.2.19.tar.gz 
./configure --prefix=/usr/local/keeplived
make && make install
做软连接
mkdir /etc/keepalived/
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

配置Keeplived

LB-M

cat > /etc/keepalived/keepalived.conf << "EOF"
global_defs { 
   notification_email { 
     www.localhost.com
   } 
   notification_email_from www.localhost.com
   router_id LB-M
} 

vrrp_script check_http {
    script "</dev/tcp/127.0.0.1/8001"      #修改为自己需要监听的端口,理论上可以监听远程端口
    interval 2                             #检查脚本的频率,单位(秒)
    weight -30                             #端口检查失败,优先级减少30,weight的绝对值要大于两台priority的差值
}

vrrp_instance VI_1 { 
    state MASTER 
    interface ens33  						# 修改为实际网卡名
    virtual_router_id 51 					# VRRP 路由 ID实例,每个实例是唯一的 
    priority 100    						# 优先级,备服务器设置 90 
    advert_int 1    						# 指定VRRP 心跳包通告间隔时间,默认1秒 
    authentication { 
        auth_type PASS      
        auth_pass 123456 
    }  
    # VIP
    virtual_ipaddress { 
        192.168.2.100/24
    } 
    track_script {
        check_http
    } 
}
EOF

LB-S

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     www.localhost.com 
   } 
   notification_email_from www.localhost.com  
   router_id LB-S
}

vrrp_script check_http {
    script "</dev/tcp/127.0.0.1/8001"      #修改为自己需要监听的端口,理论上可以监听远程端口
    interval 2                             #检查脚本的频率,单位(秒)
    weight -30                             #端口检查失败,优先级减少30,weight的绝对值要大于两台priority的差值
}

vrrp_instance VI_1 { 
    state BACKUP 
    interface ens33  						# 修改为实际网卡名
    virtual_router_id 51 					# VRRP 路由 ID实例,每个实例是唯一的 
    priority 90	    						# 优先级,备服务器设置 90 
    advert_int 1    						# 指定VRRP 心跳包通告间隔时间,默认1秒 
    authentication { 
        auth_type PASS      
        auth_pass 123456 
    }  
    # VIP
    virtual_ipaddress { 
        192.168.2.100/24
    } 
    track_script {
        check_http
    } 
}
EOF

启动keepalived

systemctl restart keepalived
systemctl enable keepalived

两个重要的值: priority和weight
priority: 为默认权重值,一般master要大于back,权重值还和weight有关,当检测失败时候,priority会和weight相加或者相减,得到一个新的权重值
vrrp_script中weight:

如果weight值为正: 切换机制为,当mater检测失败,master的priority值不变,back的权重值为priority+weight,master的权重小于back的权重,进行切换

如果weight值为负: 切换机制为,当master检测失败,master的权重值为priority-weight的值,back的权重值不变,master的权重值小于back,进行切换

总结: 一般来说weight的绝对值要小于master和back的priority的差值绝对值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值