本文将基于centos7搭建 keepalived 的高可用服务集群。流程如下

 centos7 + LVS + keepalived 高可用安装_keepalived

 

 

 

1、node1~node4安装centos7, 配置好网络。

 /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=233394cb-126d-4101-8dd4-3fdce0d6cf26
DEVICE=eth0
ONBOOT=yes

IPADDR=192.168.1.21
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

 

/etc/resolv.conf

# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 115.115.115.115

 

2、配置好node1 ~ node4 的hostname 

 在 /etc/hostname 下分别写入各个节点的hostname,完成后可通过hostname命令查看

 

3、关闭防火墙

#关闭防火墙
systemctl stop firewalld.service
#禁止开机启动
systemctl disable firewalld.service
#查看防火墙状态
firewall-cmd --state

 

4、服务安装

#ipvsadm及keepalived安装 (node1和node4)
yum install ipvsadm keepalived -y
#httpd安装 (node2和node3)
yum install httpd -y

5、keepalived配置

node1 keepalived配置:/etc/keepalived/keepalived.conf

global_defs{
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100/24 dev eth0 label eth0:3
    }
}

virtual_server 192.168.1.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 0
    protocol TCP
    real_server 192.168.1.22 80 {
        weight 1
       HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.1.23 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

node4 keepalived配置:/etc/keepalived/keepalived.conf

global_defs{
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100/24 dev eth0 label eth0:3
    }
}

virtual_server 192.168.1.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 0
    protocol TCP
    real_server 192.168.1.22 80 {
        weight 1
        HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.1.23 80 {
        weight 1
        HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

 

6、real server vip配置(node2、node3)

/etc/sysctl.conf

net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2

使用 sysctl -p 刷新到内存

配置VIP

ifconfig lo:6 192.168.1.100 netmask 255.255.255.255

7、写入访问文件至根目录(node2、node3)

 vi /var/www/html/index.html

from node2 or node3 service

 

8、启动httpd服务(node2、node3)

 systemctl start httpd

 

9、启动 keepalived 服务 (node1、node4)

#启动服务 
systemctl start keepalived
#查看服务是否正常运行,此时因包含两个real server节点,且node1的VIP正常挂载, node4的VIP没有挂载。
 ipvsadm -ln

 

10、通过VIP访问服务

http://192.168.1.100/

 

11、停掉node1的keepalived服务,VIP正常飘到node4节点中。服务正常运行。

#在node1中操作
systemctl stop keepalived

 

若将node1的keepalived服务重新启动,VIP正常飘回到node1中。服务正常运行。

 

12、停掉node2的服务,所有流量全都注入node3节点中,服务正常运行。

# 在node2中操作
systemctl stop httpd

 

若将node2的httpd服务重新启动,node2和node3同时提供服务,服务正常运行。