keepalived是一种几群的高可用软件,可以为我们提供高可用服务,它可以实现心跳监测和浮动资源管理;
它是基于VRRP的的冗余技术;接下来我们进行keepalived+HAproxy的高可用和负载均衡的集群部署;
1.准备好2台虚拟机作为keepalived+Haproxy服务器;2台作为httpd web服务器;
HAproxy的部署这里就略过了,不明白的可以看我上一个笔记https://www.cnblogs.com/loganSxb/p/11285966.html
我们在1台keepalived服务器上进行配置,然后再scp到另一台服务器上;
编译源码keepalived;
[root@sxb-1 keepalived-1.3.5]# tar xf keepalived-1.3.5.tar.gz ^C [root@sxb-1 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived^C
make&& maek install
如果失败问题是:OpenSSL is not properly installed on your system;执行:yum -y install openssl-devel
编辑配置文件;
[root@sxb-1 keepalived]# pwd /usr/local/keepalived/etc/keepalived
! Configuration File for keepalived global_defs { notification_email { root@zhaoyg.com #出现问题给she发送邮件 } notification_email_from keepalived@zhaoyg.com #发件人信息 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Director1 #这台keppalived的名字 } vrrp_instance VI_1 { state MASTER interface ens33 #根据自己网卡进行修改 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.29.210.150 /16 } }
我们还需要制作一个keepalived的启动文件;
[root@sxb-1 keepalived]# vim /usr/lib/systemd/system/keepalived.service [Unit] Description=LVS and VRRP High Availability Monitor After=syslog.target network-online.target [Service] Type=forking PIDFile=/var/run/keepalived.pid KillMode=process EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived ExecStart=/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf $KEEPALIVED_OPTIONS ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target
我们将keepalived ,启动文件scp到另一台服务器上,修改一下就可以了;
[root@sxb-1 keepalived]# scp -r /usr/local/keepalived/ 192.168.88.104:/usr/loca
我们来到另一台服务器上
! Configuration File for keepalived global_defs { notification_email { songxuebin128@163.com } notification_email_from localhost@uplooking.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id haproxy2 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.88.230 dev ens33 } }
启动keppalived;
我们的master上出现了我们当时使用的浮动IP192.168.88.230
[root@sxb-1 keepalived]# ip addr s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:88:9d:40 brd ff:ff:ff:ff:ff:ff inet 192.168.88.101/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.88.230/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe88:9d40/64 scope link valid_lft forever preferred_lft forever
我们用httpd服务进行测试;
[root@105 ~]# curl 192.168.88.230 103 [root@105 ~]# curl 192.168.88.230 102 [root@105 ~]# curl 192.168.88.230 103 [root@105 ~]# curl 192.168.88.230 102
现在我们将master停掉,来测试
[root@sxb-1 keepalived]# systemctl stop keepalived.service
查看一下BACKUP的浮动IP,可以看到过来了;
[root@sxb-4 ~]# ip addr s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:5b:71:db brd ff:ff:ff:ff:ff:ff inet 192.168.88.104/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.88.230/24 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe5b:71db/64 scope link valid_lft forever preferred_lft forever
我们继续测试;master故障后并没有影响我们的HAproxy集群;
[root@105 ~]# curl 192.168.88.230 102 [root@105 ~]# curl 192.168.88.230 103 [root@105 ~]# curl 192.168.88.230 102 [root@105 ~]# curl 192.168.88.230 103
到这里有一个问题;就是keepalived的master恢复后,会出现抢夺问题(“王者归来”);这里我们就要看实际情况来定夺要不要抢夺,当两台服务器的配置一样,那就不用进行抢夺(forever preferred_lft forever);
我们恢复master浮动IP就会自动回到master上;
[root@sxb-1 keepalived]# systemctl start keepalived.service [root@sxb-1 keepalived]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:88:9d:40 brd ff:ff:ff:ff:ff:ff inet 192.168.88.101/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.88.230/24 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe88:9d40/64 scope link valid_lft forever preferred_lft forever