上文已经介绍了lvs和keepalived的基本概念和用法,下面直接做lvs和keepalived来实现高可用负载均衡
配置:
| 主机名 | ip | 系统 | 用途 | 
|---|---|---|---|
| client | 172.16.147.1 | mac | 客户端 | 
| lvs-keepalived-master | 172.16.147.154 | centos7.5 | 分发器 | 
| lvs-keepalived-slave | 172.16.147.155 | centos7.5 | 分发器备 | 
| test-nginx1 | 172.16.147.153 | centos7.5 | web1 | 
| test-nginx2 | 172.16.147.156 | centos7.5 | web2 | 
| vip | 
1. 主备调度器安装软件
[root@lvs-keepalived-master ~]# yum -y install ipvsadm keepalived 
[root@lvs-keepalived-slave ~]# yum -y install ipvsadm keepalived
2. Keepalived
lvs-master
[root@lvs-keepalived-master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id lvs-keepalived-master    #辅助改为lvs-backup
}vrrp_instance VI_1 {state MASTERinterface ens33                #VIP绑定接口virtual_router_id 80         #VRID 同一组集群,主备一致          priority 100            #本节点优先级,辅助改为50advert_int 1            #检查间隔,默认为1sauthentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.16.147.101/24  # 可以写多个vip}
}virtual_server 172.16.147.101 80 {    #LVS配置delay_loop 3lb_algo rr     #LVS调度算法lb_kind DR     #LVS集群模式(路由模式)net_mask 255.255.255.0protocol TCP      #健康检查使用的协议real_server 172.16.147.153 80 {weight 1inhibit_on_failure   #当该节点失败时,把权重设置为0,而不是从IPVS中删除TCP_CHECK {          #健康检查connect_port 80   #检查的端口connect_timeout 3  #连接超时的时间}}real_server 172.16.147.156 80 {weight 1inhibit_on_failureTCP_CHECK {connect_timeout 3connect_port 80}}
}2. 备用机安装和配置
[root@lvs-keepalived-slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id lvs-keepalived-slave
}vrrp_instance VI_1 {state BACKUPinterface ens33nopreempt                    #不抢占资源virtual_router_id 80priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.16.147.101/24}
}
virtual_server 172.16.147.101 80 {delay_loop 3lb_algo rrlb_kind DRnet_mask 255.255.255.0protocol TCPreal_server 172.16.147.153 80 {weight 1inhibit_on_failureTCP_CHECK {connect_port 80connect_timeout 3}}real_server 172.16.147.156 80 {weight 1inhibit_on_failureTCP_CHECK {connect_timeout 3connect_port 80}}
}3. 启动KeepAlived
[root@lvs-keepalived-master ~]# systemctl start keepalived
[root@lvs-keepalived-master ~]# systemctl enable keepalived[root@lvs-keepalived-master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.147.101:80 rr persistent 20-> 172.16.147.153:80           Route   1      0          0         -> 172.16.147.156:80           Route   0      0          04. 所有RS配置(nginx1,nginx2)
[root@test-nginx1 ~]# yum install -y nginx
[root@test-nginx2 ~]# yum install -y nginx
[root@test-nginx1 ~]# echo "ip addr add dev lo 172.16.147.101/32" >> /etc/rc.local
[root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
[root@test-nginx1 ~]# sysctl -p
[root@test-nginx1 ~]# echo "web1..." >> /usr/share/nginx/html/index.html
[root@test-nginx1 ~]# systemctl start nginx
[root@test-nginx1 ~]# chmod +x /etc/rc.local上述步骤实现了两台机器的高可用并进行172.16.147.156和172.16.147.153的负载均衡