Centos 7.x 设置Lvs+ Keepalived

 

【实验环境】

  • Centos 7.2
  • Nginx 

以下为本次试验所使用的地址:

  • VIP:192.168.136.100
  • LVS-1:192.168.136.170
  • LVS-2:192.168.136.166

【安装】

在两个节点上安装lvs、keepalived(提前已经安装好nginx)

yum install ipvsadm keepalived -y

设置配置脚本

vim ipvsadm.sh

#!/bin/bash

vip=192.168.136.100
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1

执行脚本(在两台lvs主机上执行脚本)

sh ipvsadm.sh

查看是否设置成功

[root@localhost /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 192.168.136.100/32 brd 192.168.136.100 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever

【设置Keepalived】

主节点(Master),清空文件内容

vim /etc/keepalived/keepalived.conf  

global_defs{
  router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.100
    }
}

virtual_server 192.168.136.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.136.170 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.136.166 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

从节点(Backup),清空文件内容

vim /etc/keepalived/keepalived.conf  

global_defs{
  router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.100
    }
}

virtual_server 192.168.136.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.136.170 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

    real_server 192.168.136.166 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

主节点和从节点的区别

主节点:MASTER
从节点:BACKUP
state MASTER -> state BACKUP

主节点优先级:100
从节点优先级:99
priority 100 -> priority 90

另外:注意interface ens33,其中ens33是网卡名称,根据自己主机情况进行修改

开启路由转发

一次性开启:
echo "1">/proc/sys/net/ipv4/ip_forward

永久性开启:
vim /etc/sysctl.conf
#添加
net.ipv4.ip_forward=1
退出保存

#配置生效
sysctl -p

启动Keepalived(先主后从顺序开启)

systemctl start keepalived.service

查看是否开启成功

[root@localhost /]# 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.136.100:80 rr
  -> 192.168.136.166:80           Route   1      0          0         
  -> 192.168.136.170:80           Route   1      0          0      

打开浏览器输入VIP地址查看,是否成功,如果访问成功尝试断开其中某台,看是否会自动切换另外一台服务器进行访问。

 

上一篇:nodejs内存溢出 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory


下一篇:常见的消息按钮映射