Nginx + keepalived 고장 전환 및 메 일 경고 실현

5627 단어
토폴로지 그림 은 다음 과 같다.
실현 방향 은 master 의 우선 순 위 는 100 이 고 backup 의 우선 순 위 는 99 이다.master 에 nginx 모니터링 상 태 를 감지 하 는 스 크 립 트 (backup 설정 하지 않 음) 를 설정 합 니 다. master 의 nginx 고장 을 발견 하면 master 의 우선 순 위 를 98 로 줄 여 backup 우선 순위 가 master 보다 높 고 bakup 는 vip 가 대외 적 으로 서 비 스 를 제공 합 니 다.
master 서버 의 nginx 서비스 가 정상 으로 돌아 온 후에 master 의 우선 순 위 는 2 를 줄 이지 않 고 원래 의 100 을 회복 합 니 다. master 는 vip 를 받 아 대외 적 으로 서 비 스 를 제공 합 니 다.
 
메 일 경보 실현 사고: keepalived 의 상태 변환 실행 스 크 립 트 파라미터 notifymaster|notify_backup;캐릭터 가 master 나 backup 으로 변 할 때 메 일 을 보 내 는 스 크 립 트 를 호출 하여 지정 한 사용자 에 게 메 일 알림 을 보 냅 니 다.
Keepalived_master 프로필:
---------------------------------------------------------------------------------------------
global_defs {
   router_id proxy-master             ##       
}
vrrp_script chknginx {                  ##  nginx      
        script "/etc/keepalived/scripts/chk_nginx.sh"         ##          
        interval 3             ##            
        weight -2             ##        2
    }
vrrp_instance VI_1 {                 ##        
    state MASTER                 ##     master  
    interface ens33                 ##vip       
    virtual_router_id 1          ##   id,    id   
    priority 100                     ##        
    advert_int 3                     ##              
    notify_master "/etc/keepalived/scripts/mail.sh Master" ##   master       
    notify_backup "/etc/keepalived/scripts/mail.sh Backup" ##   backup       
    authentication {                 ##        
        auth_type PASS             ##     
        auth_pass putianhui      ##     
    }
    virtual_ipaddress {                 ##  vip   
        192.168.2.254 dev ens33 label ens33:1 ##  vip         
    }
    track_script {
        chknginx                     ##  nginx    
    }

Keepalived_백업 프로필:
---------------------------------------------------------------------------------------------
global_defs {
   router_id proxy-slave ##        
}
vrrp_instance VI_1 {                     ##       
    state BACKUP                     ##         
    interface ens33                 ##vip       
    virtual_router_id 1             ##   id,    id   
    priority 99                     ##        
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass putianhui         ##     ,         
    }
    virtual_ipaddress {
        192.168.2.254 dev ens33 label ens33:1
    }
}

위 에서 사용 한 스 크 립 트 코드 정보
chk_nginx. sh 스 크 립 트 정보
---------------------------------------------------------------------------------------------
#!/bin/bash
#  80      ,        1
#  1          ,0   
LISTEN_PORT=80
STATUS=`/usr/bin/netstat -anpt | grep "$LISTEN_PORT" | grep -v grep |wc -l`
if [ "$STATUS" -eq 0 ];then
#   /usr/sbin/nginx
   sleep 5
   if [ "$STATUS" -eq 0 ];then
      exit 1
   else
      exit 0
   fi
else
   exit 0
fi

mail. sh 메 일 경보 스 크 립 트 정보 보 내기
---------------------------------------------------------------------------------------------
#!/bin/bash
#  mailx         
#$1 keepalived                   
#[email protected]              
NAME="Proxy_master Server"
TIME=$(date +%F_%H:%M)
echo "${TIME}--${NAME} status is $1" | mail -s "${NAME} status is $1"  [email protected]

경고 메 일 보 내기 설정:
1、  sendmail 또는 postfix (메 일 전송 에이전트 MTA) 를 설치 하고 본 튜 토리 얼 은 sendmail 소프트웨어 를 사용 합 니 다.
(표시: 외부 메 일 을 직접 사용 하면 [qq 기업 메 일과 왕 이 기업 메 일 등] 메 일 을 보 낼 때 sendmail 이나 potfix 를 설정 하지 않 고 이 두 소프트웨어 를 끄 고 바로 세 번 째 단계 로 넘 어 갈 수 있 습 니 다. mail 을 설정 하면 이 루어 집 니 다) 
[root@ssticentos65 ~]# yum -y install sendmail                #  yum  sendmail
[root@ssticentos65 ~]# /etc/init.d/sendmail start             #  sendmail  
[root@ssticentos65 ~]# /etc/init.d/sendmail status          #  sendmail    
sendmail dead but subsys locked                            #sendmail    ,   postfix      sendmail      ,    postfix  。
sm-client (pid  1759) is running...
[root@ssticentos65 ~]# /etc/init.d/postfix stop                #  postfix      ,   postfix      ,    kill    postfix  
Shutting down postfix:                                     [FAILED]
[root@ssticentos65 ~]# /etc/init.d/postfix status                #  postfix          
master (pid  1647) is running...
[root@ssticentos65 ~]# kill -9 1647                                     #  postfix     
[root@ssticentos65 ~]# /etc/init.d/postfix status                #  postfix  
master dead but pid file exists
[root@ssticentos65 ~]# chkconfig postfix off                      #  postfix     
[root@ssticentos65 ~]# /etc/init.d/sendmail stop               #  sendmail  
Shutting down sm-client:                                   [  OK  ]
Shutting down sendmail:                                    [FAILED]
[root@ssticentos65 ~]# /etc/init.d/sendmail start                 #  sendmail  
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]

2. 메 일 발송 도구 mailx 를 설치 합 니 다.(메 일 사용자 에이전트 MUA)
[root@ssticentos65 ~]# yum -y install mailx                     #    mailx

3、  메 일 설정
[root@ssticentos65 ~]# vim /etc/mail.rc
set  [email protected]    ##          
set  smtp=smtp.exmail.qq.com   ##      smtp  
set smtp-auth-user="[email protected]" smtp-auth-password="XXXXXXX" ##          (qq           ,       )
set  smtp-auth=login     ##  ,    

다음으로 전송:https://blog.51cto.com/13777759/2407329

좋은 웹페이지 즐겨찾기