编程技术分享平台

网站首页 > 技术教程 正文

面试官问LVS+keepalived+nginx怎么实现时该怎么答?

xnh888 2024-09-08 10:59:56 技术教程 23 ℃ 0 评论

概述

前面大家已经对基本概念有个了解了,接下来就是搭建过程了~


需求

1、LVS给两台nginx做负载均衡

2、keepalived做lvs高可用,同时做Real-Server健康检查,如果发现Real-Server80端口没开,就认为故障,从集群中剔除

3、在keepalived配置文件内就能配置LVS


环境说明

这里大家要准备4台服务器,5个IP(其中一个是虚拟IP)来做。


在负载均衡器端配置lvs+keepalived

1、配置linux系统内核参数

LVS主备上面,配置linux系统内核参数:开启内核的路由模式

#vim /etc/sysctl.conf 
net.ipv4.ip.forward = 1 

立即生效

# sysctl -p 

2、LVS主备关闭iptables服务

#service iptables stop


3、LVS主备安装LVS

可以选择yum或源码包的安装方式,这里我选择yum安装

#yum install ipvsadm

4、在主备服务器编译安装keepalived

(keepalived官网:http://www.keepalived.org/)

1)安装popt的开发包

#yum install popt-devel -y 

2)安装openssl

#yum install openssl-devel -y 

3)安装gcc

#yum install gcc -y

4)安装keepalived

#wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
#tar –zxvf keepalived-1.2.4.tar.gz
#./configure –prefix=/usr/local/keepalived
#make && make install

5、LVS主备将keepalived设置开机启动服务

#mkdir -p /etc/keepalived/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig --add keepalived
# chkconfig --level 35 keepalived on

6、LVS主备配置keepalived.conf

注意LVS网卡为eth1,下面介绍两个配置文件

6.1、主LVS上面的keepalived的配置文件

! Configuration File for keepalived
global_defs { #全局配置部分
 notification_email { #email 通知,基本不用此处可注释掉
 acassen@firewall.loc
 failover@firewall.loc
 sysadmin@firewall.loc
 }
 notification_email_from Alexandre.Cassen@firewall.loc #定义发送邮件的邮箱
 smtp_server 127.0.0.1
 smtp_connect_timeout 30
 router_id LVS_DEVEL # 设置lvs的id,在一个网络内应该是唯一的
}
vrrp_instance VI_1 { #vrrp实例定义部分
 state MASTER #设置lvs的状态,报错MASTER和BACKUP两种,必须大写
 interface eth1 #指定LVS网卡,设置对外服务的接口
 virtual_router_id 51 #虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
 priority 100 #定义优先级,数字越大优先级越高,在一个vrrp——instance下,master的优先级必须大于backup
 advert_int 1 #设定master与backup负载均衡器之间同步检查的时间间隔,单位是秒
 authentication { #设置验证类型和密码
 auth_type PASS #主要有PASS和AH两种
 auth_pass 1111 
 }
 virtual_ipaddress { #设置虚拟ip地址,可以设置多个,每行一个
 xx.xx.xx.E
 }
}
virtual_server xx.xx.xx.E 80 { #设置虚拟服务器,需要指定虚拟ip和服务端口
 delay_loop 2 #健康检查时间间隔
 lb_algo wrr #负载均衡调度算法
 lb_kind DR #负载均衡转发规则
 #net_mask 255.255.255.0 
 persistence_timeout 60 #设置会话保持时间,对动态网页非常有用
 protocol TCP #指定转发协议类型,有TCP和UDP两种
 real_server xx.xx.xx.C 80 { #配置服务器节点1,需要指定real server的真实IP地址和端口
 weight 1 #设置权重,数字越大权重越高
 TCP_CHECK { #realserver的状态监测设置部分单位秒
 connect_timeout 3 #超时时间
 nb_get_retry 3 #重试次数
 delay_before_retry 3 #重试间隔
 connect_port 80 #监测端口
 }
 }
 real_server xx.xx.xx.D 80 {
 weight 1
 TCP_CHECK {
 connect_timeout 3
 nb_get_retry 3
 delay_before_retry 3
 connect_port 80
 }
 }
}

6.2、LVS-backup的配置文件

! Configuration File for keepalived
global_defs {
 notification_email {
 acassen@firewall.loc
 failover@firewall.loc
 sysadmin@firewall.loc
 }
 notification_email_from Alexandre.Cassen@firewall.loc
 smtp_server 127.0.0.1
 smtp_connect_timeout 30
 router_id LVS_DEVEL
}
vrrp_instance VI_1 {
 state BACKUP
 interface eth1
 virtual_router_id 51
 priority 99
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 virtual_ipaddress {
 xx.xx.xx.E
 }
}
virtual_server xx.xx.xx.E 80 {
 delay_loop 2
 lb_algo wrr
 lb_kind DR
 #net_mask 255.255.255.0
 persistence_timeout 60
 protocol TCP
 real_server xx.xx.xx.C 80 {
 weight 1
 TCP_CHECK {
 connect_timeout 3
 nb_get_retry 3
 delay_before_retry 3
 connect_port 80
 }
 }
 real_server xx.xx.xx.D 80 {
 weight 1
 TCP_CHECK {
 connect_timeout 3
 nb_get_retry 3
 delay_before_retry 3
 connect_port 80
 }
 }
}

到这里关于lvs和keepalived部分就已经配置好了。


篇幅有限,关于LVS+KEEPALIVED部分就介绍到这了,后面主要介绍在relay_server端安装配置nginx部分和测试集群是否生效,能不能自动漂移,感兴趣的朋友可以关注下!


Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表