欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

企业实例~3.lvs+Keepalived配置

程序员文章站 2022-07-12 17:41:19
...

Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统 中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人 工干涉,需要人工做的只是修复故障的服务器。

完成DR

https://blog.csdn.net/Stella_Pooter/article/details/87778029         企业实例~1.lvs三种工作模式及原理——DR模式

 

本次实验基于rhel6.5版本虚拟机

调度器1 server1 IP:172.25.35.1

调度器2 server4 IP:   172.25.35.4

服务端1 server2  IP:172.25.35.2

服务端2 server3 IP:172.25.35.3


调度器1,主节点配置

停止 ldirectord

/etc/init.d/ldirectord stop
Stopping ldirectord... success
chkconfig ldirectord off


清除以前策略
ipvsadm -C
删除ip
ip addr del 172.25.35.100/24 dev eth0


keepalived 源码编译

tar zxf keepalived-2.0.6.tar.gz

安装编译所需软件

yum install openssl-devel

编译

./configure --prefix=/usr/local/keepalived --with-init=SYSV

加入系统环境变量

make && make install

添加执行权限

chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived

方便使用,作软链接
 

ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

修改软件配置文件
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]        #有问题发邮件给服务器1
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER        #主节点
    interface eth0
    virtual_router_id 35    #多台处于同一网段须分开
    priority 100        #优先级最高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.35.100
    }
}

virtual_server 172.25.35.100 80 {    #虚拟服务器地址
    delay_loop 6
    lb_algo rr
    lb_kind DR                #类型为DR
    #persistence_timeout 50
    protocol TCP
    real_server 172.25.35.2 80 {    #真实服务器地址
        TCP_CHECK {            #健康检查
    weight 1            #权重
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 172.25.35.3 80 {
        TCP_CHECK {
    weight 1
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

开启服务

/etc/init.d/keepalived start

可以看到生成的策略

ipvsadm -l

服务器2,3开启httpd服务
客户机测试:

[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
server3's Apache server
[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
server3's Apache server

2.主副节点

调度器2:副节点
调度器1 :

[[email protected] ~]# scp -r /usr/local/keepalived [email protected]:/usr/local/

同调度器1作软连接

vim /etc/keepalived/keepalived.conf
 18     state BACKUP        #副节点
 21     priority 50        #优先级低于主节点
[[email protected] ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

测试1:
关闭调度器1主节点

[[email protected] init.d]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

查看副节点IP:

[[email protected] ~]# ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:b5:c9:f6 brd ff:ff:ff:ff:ff:ff
    inet 172.25.35.4/24 brd 172.25.35.255 scope global eth0
    inet 172.25.35.100/32 scope global eth0
    inet6 fe80::5054:ff:feb5:c9f6/64 scope link
       valid_lft forever preferred_lft forever

我们可以看到35.100的地址到了副节点上,这就叫做:
节点漂移

客户机仍正常访问

 

测试2:

开启主节点

[[email protected] init.d]# /etc/init.d/keepalived start
[[email protected] init.d]# ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:03:88:63 brd ff:ff:ff:ff:ff:ff
    inet 172.25.35.1/24 brd 172.25.35.255 scope global eth0
    inet 172.25.35.100/32 scope global eth0
    inet6 fe80::5054:ff:fe03:8863/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:b5:c9:f6 brd ff:ff:ff:ff:ff:ff
    inet 172.25.35.4/24 brd 172.25.35.255 scope global eth0
    inet6 fe80::5054:ff:feb5:c9f6/64 scope link
       valid_lft forever preferred_lft forever

节点回服务端1

 

测试3:
关闭服务端3 httpd服务

[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
curl: (7) Failed connect to 172.25.35.100:80; Connection refused
[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
curl: (7) Failed connect to 172.25.35.100:80; Connection refused

等待数秒后,正常运行

[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
server2's Apache server
[[email protected] images]# curl 172.25.35.100
server2's Apache server

此处为keepalive服务器将宕机服务器踢出

附 keepalive用户手册