232121 发表于 2016-5-12 09:05:27

keepalived 双实例_nginx互为主备

一、实验目的:两台nginx服务器,利用keepalived实现双实例互为主备,并能够对http服务检测动态切换主备,主服务器检测失败后,重启对应http服务。
二、实验环境

服务器类型IP软件主备情况VIP
CentOS 6.6(nginx1)172.18.100.11nginx+keepalived实例VI_1为主服务器;实例VI_2为辅服务器172.18.100.21
CentOS 6.6(nginx2)172.18.100.12nginx+keepalived实例VI_2为主服务器;实例VI_1为辅服务器172.18.100.22
三、操作步骤
    1.安装nginx并设置测试页;

   2.安装keepalived实现地址双主备切换;
    3.编写检测服务脚本;

    4.编写状态脚本。

四、安装nginx服务
五、安装keepalived,配置
nginx1 keepalived.conf配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.0.100.18
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 20
    priority 100
    advert_int 1

   authentication {
      auth_type PASS
      auth_pass 11112222
    }
    virtual_ipaddress {
      172.18.100.21
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 22
    priority 98
    advert_int 1

   authentication {
      auth_type PASS
      auth_pass 11112222
    }
    virtual_ipaddress {
      172.18.100.22
    }
}




    nginx2 keepalived.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2
   vrrp_mcast_group4 224.0.100.18
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 20
    priority 98
    advert_int 1

   authentication {
      auth_type PASS
      auth_pass 11112222
    }
    virtual_ipaddress {
      172.18.100.21
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 22
    priority 100
    advert_int 1

   authentication {
      auth_type PASS
      auth_pass 11112222
    }
    virtual_ipaddress {
      172.18.100.22
    }
}




测试:停掉nginx1的keepalived服务,可以看到服务地址的全都配置到了nginx2服务器网卡上,反之亦然。
六、配置服务检测脚本
在每个主机的两个实例中http上下文添加


1
2
3
4
5
vrrp_script chk_httpd {
   script "killall -0 httpd"
   interval 2
   weight -5
}




在实例上下文中添加检测track

1
2
3
track_script {
    chk_httpd
}




测试:查看关掉nginx服务后,服务主动降低优先级,主节点切换为备份节点,IP地址漂移到另一个服务器。这时候无论那个服务器的服务进程宕掉(当然可以用其他方式测试,注意效率,毕竟一秒一次测试),服务都能将地址和服务转移到另一台服务器。

七、添加服务状态检测脚本,对服务失败的服务器重启服务
为每个主服务器主节点添加检测脚本:当主节点服务不可用时,重启服务。
/etc/keepalived/changToBackup.sh

1
2
#!/bin/sh
service httpd restart && echo "httpd service restart success.@$(date +'%F %T')"




为每个主服务器实例上下文中添加检测脚本:

1
notify_backup "/etc/keepalived/changToBackup.sh"




测试:当主动将一台服务的服务停掉后,服务会将服务重启,主服务器从主服务器转换动辅服务器,在切换到主服务器,自动完成!不必手动操作!

著名点评师 发表于 2016-6-4 19:52:13

到底是 高可用 nginx 还是 httpd ????

BenjaminDu 发表于 2016-6-20 00:06:10

learning this page site oflvs keepalived
页: [1]
查看完整版本: keepalived 双实例_nginx互为主备