pangxia75 发表于 2018-12-30 12:16:39

keepalived+nginx构建高可用web

  一、试验拓扑
http://blog.运维网.com/attachment/201201/115342347.png
二、环境描述
WEB1(MASTER):192.168.232.131
WEB2(BACKUP):192.168.232.132
VIP : 192.168.232.150
三、安装配置
#yum -y install keepalived
web1配置信息如下:
  # cat /etc/keepalived/keepalived.conf

  global_defs {
     notification_email {
     admin@localhost.com
     }
     notification_email_from Alexandre.Cassen@firewall.loc
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id LVS_DEVEL
  }
  vrrp_script chk_run {
      script "/root/nginx_check.sh"
      interval 1
      weight -2
  }
  vrrp_instance VI_1 {
      state MASTER
      interface eth0
      virtual_router_id 55
      priority 101
      advert_int 1
      nopreempt
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.232.150
      }
      track_script {
        chk_run
      }
  }




web2如下:

  # cat /etc/keepalived/keepalived.conf
  
  global_defs {
     notification_email {
        admin@localhost.com
     }
     notification_email_from Alexandre.Cassen@firewall.loc
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id LVS_DEVEL
  }
  
  vrrp_script chk_run {
      script "/root/nginx_check.sh"
      interval 1
      weight -2
  }
  
  
  vrrp_instance VI_1 {
      state BACKUP
      interface eth0
      virtual_router_id 55
      priority 100
      advert_int 1
      nopreempt
      authentication {
        auth_type PASS
        auth_pass 1111
      }
      virtual_ipaddress {
        192.168.232.150
      }
      track_script {
        chk_run
      }
  }


启动keepalived,#service keepalived start
看下master的日志
VRRP_Instance(VI_HA) Dropping received VRRP packet...
Terminating on signal
Stopping Keepalived v1.2.1 (01/04,2012)
Terminating VRRP child process on signal
Starting Keepalived v1.2.1 (01/04,2012)
Registering Kernel netlink reflector
Registering Kernel netlink command channel
Registering gratutious ARP shared channel
Opening file '/etc/keepalived/keepalived.conf'.
Configuration is using
Using LinkWatch kernel netlink reflector...
VRRP sockpool
Starting VRRP child process, pid=4997
VRRP_Instance(VI_HA) Transition to MASTER STATE
VRRP_Instance(VI_HA) Entering MASTER STATE
VRRP_Instance(VI_HA) setting protocol VIPs.
VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150
VRRP_Group(VGM) Syncing instances to MASTER state
Registering new address record for 192.168.232.150 on eth0.
VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150
Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150
ip信息:
# ip a
1: lo:mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:6e:b2:0f brd ff:ff:ff:ff:ff:ff
inet 192.168.232.130/24 brd 192.168.152.255 scope global eth0
inet 192.168.232.150/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe6e:b20f/64 scope link
valid_lft forever preferred_lft forever


再看backup的日志:
VRRP_Instance(VI_HA) ignoring received advertisment...
Terminating on signal
Stopping Keepalived v1.2.1 (01/05,2012)
Terminating VRRP child process on signal
Starting Keepalived v1.2.1 (01/05,2012)
Registering Kernel netlink reflector
Registering Kernel netlink command channel
Registering gratutious ARP shared channel
Opening file '/etc/keepalived/keepalived.conf'.
Configuration is using
Using LinkWatch kernel netlink reflector...
VRRP_Instance(VI_HA) Entering BACKUP STATE
VRRP sockpool
Starting VRRP child process, pid=10968
ip信息:
# ip a
1: lo:mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:c6:24:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.131/24 brd 192.168.152.255 scope global eth0
inet6 fe80::20c:29ff:fec6:24a8/64 scope link
valid_lft forever preferred_lft forever
四、验证
首先访问看下是谁提供服务:

http://s3.运维网.com/wyfs02/M00/47/D4/wKioL1QALibw0rydAABRHjb2lUQ898.jpg
  

之后我们把master的keepalived服务关掉,看backup日志就可发现已经切换了
http://s3.运维网.com/wyfs02/M02/47/D2/wKiom1QALQ_ydt5FAABLgOzJwnQ755.jpg
但是现在有个问题就是,当web1在启动时会抢占回来,虽然设置了nopreempt不抢占,但是不生效
所以要将web1的state MASTER改成state BACKUP将2节点都设成BACKUP状态,之后就不会再抢占了



页: [1]
查看完整版本: keepalived+nginx构建高可用web