keepalived+LVS ---高可用
程序员文章站
2024-01-31 23:27:52
...
实验环境 VS/DR
-
该实验的的架构图为:
-
环境:需要五台服务器,两台用来作为LVS,两台用来作为server端,一台作为客户端
-
关闭防火墙和seliunx
systemctl stop firewalld && setenforce 0
配置server-1和server-2
- 安装nginx,
如果你想要安装nginx,请点击nginx–编译安装或nginx—yum安装 - 修改配置文件
echo "验证内容" >/usr/share/nginx/html/index.html
- 启动nginx
systemctl start nginx
- 在lo接口上绑定VIP
ip addr add dev lo 192.168.92.222/32
配置LVS
- 安装keepalived和ipvsadm
yum -y install ipvsadm keepalived
- 重新创建keepalived的配置文件。(将原有的删除)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs-keepalived-master #辅助改为lvs-backup
}
vrrp_instance VI_1 {
state MASTER #备 改为BACKUP
interface ens33 #VIP绑定接口
virtual_router_id 80 #VRID 同一组集群,主备一致
priority 100 #本节点优先级,辅助改为50
advert_int 1 #检查间隔,默认为1s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.92.222/32 #要保持同网段
}
}
virtual_server 192.168.92.222 80 { #LVS配置
delay_loop 3
lb_algo rr #LVS调度算法
lb_kind DR #LVS集群模式(路由模式)
nat_mask 255.255.255.0
persistence_timeout 20 #持久性连接
protocol TCP #健康检查使用的协议
real_server 192.168.92.128 80 { #server端的IP
weight 1
inhibit_on_failure #当该节点失败时,把权重设置为0,而不是从IPVS中删除
TCP_CHECK { #健康检查
connect_port 80 #检查的端口
connect_timeout 3 #连接超时的时间
}
}
real_server 192.168.92.129 80 { #server端的ip
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
# 将其拷贝到LVS-2.做相应的修改
设置VIP
- 添加vip
ip addr add dev ens33 192.168.92.222/32 #设置VIP
ipvsadm -S > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -A -t 192.168.92.222:80 -s rr
ipvsadm -a -t 192.168.92.222:80 -r 192.168.92.128 -g
ipvsadm -a -t 192.168.92.222:80 -r 192.168.92.129 -g #server端的IP
- 启动keepalived并设置开机自启
systemctl start keepalived
systemctl enable keepalived
- 查看相关配置
ipvsadm -ln
检测
在客户端通过vip访问
推荐阅读