首先須要明白咱們準備使用keepalived來作什麼,今天這裏只是給你們簡單安裝和配置下keepalived實現vip對外服務,防止單點故障。keepalived是高可用高可用高可用 而非負載非負載非負載。html
下面我將使用keepalived部署雙主模模式,實現方式,利用腳本檢測某個服務,當服務宕機的時候,中止keepalived,vip漂移到另外一個主機上。python
VIP1: 192.168.80.136 eth0:10.0.0.11 em1:10.0.0.67 nginx
VIP2: 192.168.80.139 eth1:10.0.0.12 em2:10.0.0.68 #vip/主機1網卡對應ip/主機2對應網卡ipbash
1.安裝keepalived服務器
yum install -y glibc openssl-devel wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz tar zxf keepalived-1.2.24.tar.gz cd keepalived-1.2.24 mkdir /usr/local/keepalived ./configure --prefix=/usr/local/keepalived && make && make install
2.將keepalived配置爲系統服務運維
mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived chmod +x /etc/init.d/keepalived ln /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived chkconfig --add keepalived
3.配置keepalived服務ide
cd /etc/keepalived/spa
cat keepalived.conf (主機1配置)debug
! Configuration File for keepalived global_defs { router_id NODE_B } vrrp_script checkALL { script "/etc/keepalived/script/check_nginx.sh" #檢測nginx服務腳本 interval 5 } vrrp_instance VIP136 { state MASTER #VIP1的master 主機2的時候改成BACKUP interface eth0 mcast_src_ip 10.0.0.11 virtual_router_id 136 #虛擬路由id 2個主機須要同樣 priority 100 #優先級 數值越大優先級越大,第二個主機設置比她它小 advert_int 1 authentication { auth_type PASS auth_pass masterbrian #雙方認證使用的密碼 } virtual_ipaddress { 192.168.80.136 #vip1虛擬ip } track_script { checkALL #檢測腳本 } notify_master "/etc/keepalived/script/notify.sh master 10.0.0.11 192.168.80.136" #變爲主的時候觸發的腳本 notify_backup "/etc/keepalived/script/notify.sh backup 10.0.0.11 192.168.80.136" notify_fault "/etc/keepalived/script/notify.sh fault 10.0.0.11 192.168.80.136" } vrrp_instance VIP139 { state BACKUP #VIP2的slave 主機改成MASTER nopreempt #備機不搶佔模式,當備機的優先級高於主機的時候不搶佔 interface eth1 mcast_src_ip 10.0.0.12 virtual_router_id 139 priority 90 advert_int 1 authentication { auth_type PASS auth_pass masterrobinson } virtual_ipaddress { 192.168.80.139 } track_script { checkALL } notify_master "/etc/keepalived/script/notify.sh master 10.0.0.12 192.168.80.139" notify_backup "/etc/keepalived/script/notify.sh backup 10.0.0.12 192.168.80.139" notify_fault "/etc/keepalived/script/notify.sh fault 10.0.0.12 192.168.80.139" }
cat keepalived.conf (主機2配置) rest
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script checkALL { script "/etc/keepalived/script/check_nginx.sh" interval 5 } vrrp_instance VIP136 { state BACKUP interface em1 nopreempt mcast_src_ip 10.0.0.67 virtual_router_id 136 priority 90 advert_int 1 authentication { auth_type PASS auth_pass masterbrian } virtual_ipaddress { 192.168.80.136 } track_script { checkALL } notify_master "/etc/keepalived/script/notify.sh master 10.0.0.67 192.168.80.136" notify_backup "/etc/keepalived/script/notify.sh backup 10.0.0.67 192.168.80.136" notify_fault "/etc/keepalived/script/notify.sh fault 10.0.0.67 192.168.80.136" } vrrp_instance VIP139 { state MASTER interface em2 mcast_src_ip 10.0.0.68 virtual_router_id 139 priority 100 advert_int 1 authentication { auth_type PASS auth_pass masterrobinson } virtual_ipaddress { 192.168.80.139 } track_script { checkALL } notify_master "/etc/keepalived/script/notify.sh master 10.0.0.68 192.168.80.139" notify_backup "/etc/keepalived/script/notify.sh backup 10.0.0.68 192.168.80.139" notify_fault "/etc/keepalived/script/notify.sh fault 10.0.0.68 192.168.80.139" }
4.使用到的其餘腳本文件
4.1監控nginx服務腳本,當nginx服務不存在的時候中止keepalived服務
這個腳本能夠根鬚要使用高可用的服務來作相應的調整。
cat /etc/keepalived/script/check_nginx.sh
#!/bin/bash checknginx=$(ps -C nginx --no-header | wc -l) checkkeepalived=$(ps -C keepalived --no-header | wc -l) TIME=$(date +%Y-%m-%d\ %H:%M:%S) if [ ${checknginx} -eq 0 ];then service nginx restart sleep 3 fi checknginx_two=$(ps -C nginx --no-header | wc -l) if [ ${checknginx_two} -eq 0 ];then killall -TERM keepalived fi
4.2狀態變化的時候的觸發的腳本
cat /etc/keepalived/script/notify.sh
#!/bin/bash status=${1} hostip=${2} vip=${3} TIME=$(date +%Y-%m-%d\ %H:%M:%S) HOST_NAME=$(hostname) subject="${HOST_NAME} to be ${status}: ${vip} floating" send_user="xxxxx" send_contant="${TIME} ${HOST_NAME}[${hostip}] 主機keepalived的VIP:${vip} 狀態改變爲${status},請檢查相關服務。" python /etc/keepalived/script/send_mail.py "${send_user}" "${subject}" "${send_contant}"
4.3 notify.sh中觸發的發送郵件的python腳本
這個腳本最下面須要修改 用戶名和密碼,郵件服務器和收件人等相關信息
cat /etc/keepalived/script/send_mail.py
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 純文本文件發送 """ import sys import smtplib from email.header import Header from email.mime.text import MIMEText from abc import abstractmethod class Mail(object): """dohost, user_name, pwd, context, Subject, To, filename, file_path, port, nick_name, mail_adre***ing for Mail""" def __init__(self, host, user_name, pwd, context, Subject, To, port, nick_name, mail_adress): """ :param host: 郵件服務器地址 :param user_name: 郵件登錄名 :param pwd: 郵箱登錄密碼 :param context: 郵箱正文 :param Subject: 郵箱主題 :param To: 收件人 :param From: 發件人 """ self.host=host self.user_name=user_name self.pwd=pwd self.context=context self.Subject=Subject self.To=To self.port=port self.nick_name=nick_name self.mail_adress=mail_adress @abstractmethod def send(self,**kwargs): """ send mail to account :param file: :return: """ pass class TextMail(Mail): """ 只有文本信息的郵件 """ def __init__(self,**kwargs): super(TextMail,self).__init__(**kwargs) self.mail_type='TEXT' @abstractmethod def send(self): #print("send_start") server=smtplib.SMTP(self.host,self.port) server.set_debuglevel(0) server.login(self.user_name,self.pwd) msg=MIMEText(self.context,'html','utf-8') msg['From']='{0}<{1}>'.format(self.nick_name,self.mail_adress,'utf-8') msg['Subject']=Header(self.Subject,'utf-8').encode() msg['To']=','.join(self.To) try: server.sendmail(self.mail_adress,self.To,msg.as_string()) print("mail has been successfully") except smtplib.SMTPException as e: print('郵件發送失敗') print(e) def send_mail(fromstmp,fromuser,frompwd,content,subject,touser): host=fromstmp fromuser=fromuser pwd=frompwd scontent=content subject=subject touser=touser nick_name="運維監控" b=TextMail(host=host,user_name=fromuser,pwd=pwd,context=content,Subject=subject,To=touser,port=25,nick_name=nick_name,mail_adress=fromuser) b.send() if __name__ == '__main__': """ 設置發送郵箱信息 """ fromuser="XXX" #發送郵箱的 123456@qq.com frompwd="XXX" #郵箱密碼 xxxx fromstmp="XXX" #郵件服務stmp smtp.qq.com touser = [str(sys.argv[1])] #傳過來的第一個參數 subject = str(sys.argv[2]) #傳過來的第二個參數 content = str(sys.argv[3]) #傳過來的第三個參數 touser='xxx@126.com','xxxx@123.com' #收郵件的人 send_mail(fromstmp,fromuser,frompwd,content,subject,touser)