配置keepalived
$sudo vim /etc/keepalived/keepalived.conf //内容如下
MASTER机
- vrrp_script chk_nginx {
- script "/tmp/check_http.sh"
- interval 5
- weight 2
- }
- global_defs {
- notification_email {
- }
- notification_email_from
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_Nginx
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eth1
- virtual_router_id 51
- mcast_src_ip 192.168.0.116
- priority 100
- advert_int 3
- smtp_alert
- authentication {
- auth_type PASS
- auth_pass 123456
- }
- track_script {
- chk_nginx
- }
- virtual_ipaddress {
- 192.168.0.200
- }
- }
BACKUP机
- vrrp_script chk_nginx {
- script "/tmp/check_http.sh"
- interval 5
- weight 2
- }
- global_defs {
- notification_email {
- }
- notification_email_from
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_Nginx
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth1 #实际情况而定
- virtual_router_id 51
- mcast_src_ip 192.168.0.117
- priority 50
- advert_int 3
- smtp_alert
- authentication {
- auth_type PASS
- auth_pass 123456
- }
- track_script {
- chk_nginx
- }
- virtual_ipaddress {
- 192.168.0.200
- }
- }
CHECK_HTTP SHELL
$sudo vim /tmp/check_http.sh
- #!/bin/bash
- NGINX_PROCESS='ps -C nginx --no-header | ws -l'
- if[$NGINX_PROCESS -eq 0]
- then
- /etc/init.d/nginx start
- sleep 3
- if[$NGINX_PROCESS -eq 0]
- then
- /etc/init.d/keepalived stop
- fi
- fi
PS:check_http.sh在双机上相同
最后,启动全部服务进行测试,如停止MASTER机上的nginx,让BACKUP机来接管,测试见下一页 http://www.linuxidc.com/Linux/2012-10/72748p3.htm 。
