Web服务器群集——LVS-DR+Keepalived 配置步骤

准备工作

详细步骤和配置解释可以参考:LVS-DR+Keepalived 高可用集群

之前写的主要是先做lvs-dr再做keepalived 因此步骤显得比较麻烦 今天再写一个简要步骤

调度器上安装Keepalived

yum install -y ipvsadm
yum install -y keepalived

调度主机上做lvs策略

配置负载分配策略
首先清除原有策略
ipvsadm -C

添加虚拟服务器
ipvsadm -A -t 192.168.188.188:80 -s rr

添加真实服务器 工作模式为DR
ipvsadm -a -t 192.168.188.188:80 -r 192.168.188.100:80 -g
ipvsadm -a -t 192.168.188.188:80 -r 192.168.188.101:80 -g

保存
ipvsadm-save > /etc/sysconfig/ipvsadm

查看
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.188.188:80 rr
  -> 192.168.188.100:80           Route   1      0          0         
  -> 192.168.188.101:80           Route   1      0          0 

从机不需要配置策略 因为keepalived会自动解析策略

在调度器上配置Keepalived

主机

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
    
    
   router_id LVS1
}

vrrp_instance VI_1 {
    
    
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
    
    
        192.168.188.188
    }
}

virtual_server 192.168.188.188 80 {
    
    
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.188.100 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
        }
    }
   real_server 192.168.188.101 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
        }
    }
}

从机

! Configuration File for keepalived

global_defs {
    
    
   router_id LVS2
}

vrrp_instance VI_1 {
    
    
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 80
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
    
    
        192.168.188.188
    }
}

virtual_server 192.168.188.188 80 {
    
    
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.188.100 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
        }
    }
   real_server 192.168.188.101 80 {
    
    
        weight 1
        TCP_CHECK {
    
    
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 4
        }
    }
}
重启服务
systemctl restart ipvsadm
systemctl restart keepalived

查看keepalived配置完成后 ens33是否有了VIP
ip addr

从机查看策略
ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.188.188:80 rr
  -> 192.168.188.100:80           Route   1      0          0         
  -> 192.168.188.101:80           Route   1      0          0 

配置真实服务器

添加虚拟接口lo:0 配置VIP地址 
cp -a ifcfg-lo ifcfg-lo:0

vim ifcfg-lo:0

DEVICE=lo:0
IPADDR=192.168.188.188
NETMASK=255.255.255.255
NETWORK=127.0.0.0
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback

然后将网卡传给另一台web服务器
scp ./ifcfg-lo:0 [email protected]:/root

开启虚拟接口
ifup lo:0

添加一条到VIP的本地路由
route add -host 192.168.188.188 dev lo:0

查看路由
route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.188.2   0.0.0.0         UG    100    0        0 ens33
192.168.79.0    0.0.0.0         255.255.255.0   U     101    0        0 ens37
192.168.188.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.188.188 0.0.0.0         255.255.255.255 UH    0      0        0 lo

测试

浏览器输入VIP
在这里插入图片描述
在这里插入图片描述
关闭主调度器 浏览器仍然可以访问 查看从机信息
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Cantevenl/article/details/115438748