

NTP安裝
yum -y install ntp
systemctl enable ntpd
首先同步一次時(shí)間
ntpdate time.windows.com
ntp配置(Host1 Host2)
/etc/ntp.conf
server time.windows.com prefer server 0.asia.pool.ntp.org server 1.asia.pool.ntp.org server 2.asia.pool.ntp.org #記錄上次的NTP server與上層NTP server連接接所花費(fèi)的時(shí)間 driftfile /var/lib/ntp/drift #設(shè)置默認(rèn)策略為允許任何主機(jī)進(jìn)行時(shí)間同步 restrict 0.0.0.0 mask 0.0.0.0 nomodify #設(shè)置ntp日志的path statsdir /var/log/ntp/ #設(shè)置ntp日志文件 logfile /var/log/ntp/ntp.log
mkdir /var/log/ntp/; touch /var/log/ntp/ntp.log; chown ntp:ntp /var/log/ntp/ntp.log
systemctl start ntpd
Keepalived安裝
(host1、host2)
yum -y install Keepalived
NTP健康檢測(cè)腳本:
cat /script/check_ntp.sh
#!/bin/bash # status="ntp_failed" check failed, status="ntp_success" check ntp success status="ntp_failed" if [ $(ps -C ntpd --no-header |wc -l) != 0 ]; then status="ntp_success" else /bin/systemctl restart ntpd if [ $(ps -C ntpd --no-header |wc -l) != 0 ]; then status="ntp_success" fi fi
chmod +x /script/check_ntp.sh
向外發(fā)送郵件告警python腳本:
cat /script/keepalived_notify.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# use: /bin/python /script/keepalived_notify.py 角色{master/backup} 本機(jī)IP 虛擬機(jī)IP
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import sys, time, subprocess
# 第三方 SMTP 服務(wù)
mail_host="smtp.exmail.qq.com" #設(shè)置服務(wù)器
mail_user="xx@qq.com" #用戶名
mail_pass="mail_password" #口令
sender = 'xx@qq.com'
receivers = ['xx1@qq.com', 'xx@163.com'] # 接收告警郵件地址,可設(shè)置為你的QQ郵箱或者其他郵箱
p = subprocess.Popen('hostname', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
hostname = p.stdout.readline().split('\n')[0]
message_to = ''
for i in receivers:
message_to += i + ';'
def print_help():
note = '''python script.py role ip vip
'''
print(note)
exit(1)
time_stamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
if len(sys.argv) != 4:
print_help()
elif sys.argv[1] == 'master':
message_content = '%s server: %s(%s) change to Master, vIP: %s' %(time_stamp, sys.argv[2], hostname, sys.argv[3])
subject = '%s change to Master -- keepalived notify' %(sys.argv[2])
elif sys.argv[1] == 'backup':
message_content = '%s server: %s(%s) change to Backup, vIP: %s' %(time_stamp, sys.argv[2], hostname, sys.argv[3])
subject = '%s change to Backup -- keepalived notify' %(sys.argv[2])
else:
print_help()
message = MIMEText(message_content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(message_to, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 為 SMTP 端口號(hào)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("郵件發(fā)送成功")
except smtplib.SMTPException as e:
print("Error: 無(wú)法發(fā)送郵件")
print(e)host1配置
/etc/keepalived/keepalived.conf
global_defs {
notification_email {
xx@xx.com
}
notification_email_from keepalived@xx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_ntp {
script "/script/check_ntp.sh |grep 'ntp_success' "
interval 4
weight -60 ## 當(dāng)檢測(cè)失敗時(shí),優(yōu)先級(jí)減60,該值的絕對(duì)要大于主備優(yōu)先級(jí)差的絕對(duì)值
}
vrrp_instance VI_1 {
state BACKUP ############ MASTER/BACKUP
interface ens160
virtual_router_id 51
mcast_src_ip 172.16.0.2
priority 150 ########### MASTER權(quán)值要比BACKUP高
advert_int 1
authentication {
auth_type PASS
auth_pass GSksLAyTX9ylwG86U2Ez
}
track_script {
chk_http_port ### 執(zhí)行NTP健康檢測(cè)
}
virtual_ipaddress {
172.16.0.10
}
notify_master "/bin/python /script/keepalived_notify.py master 172.16.0.2 172.16.0.10" ## 當(dāng)切換為master時(shí)執(zhí)行腳本
notify_backup "/bin/python /script/keepalived_notify.py backup 172.16.0.2 172.16.0.10" ## 當(dāng)切換為backup時(shí)執(zhí)行腳本
}Host2配置
global_defs {
notification_email {
xx@xx.com
}
notification_email_from keepalived@xx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_ntp {
script "/script/check_ntp.sh |grep 'ntp_success' "
interval 4
weight -60 ## 當(dāng)檢測(cè)失敗時(shí),優(yōu)先級(jí)減60,該值的絕對(duì)要大于主備優(yōu)先級(jí)差的絕對(duì)值
}
vrrp_instance VI_1 {
state MASTER ############ MASTER/BACKUP
interface ens160
virtual_router_id 51
mcast_src_ip 172.16.0.3
priority 100 ########### MASTER權(quán)值要比BACKUP高
advert_int 1
authentication {
auth_type PASS
auth_pass GSksLAyTX9ylwG86U2Ez
}
track_script {
chk_http_port ### 執(zhí)行NTP健康檢測(cè)
}
virtual_ipaddress {
172.16.0.10
}
notify_master "/bin/python /script/keepalived_notify.py master 172.16.0.3 172.16.0.10" ## 當(dāng)切換為master時(shí)執(zhí)行腳本
notify_backup "/bin/python /script/keepalived_notify.py backup 172.16.0.3 172.16.0.10" ## 當(dāng)切換為backup時(shí)執(zhí)行腳本
}keepalived CentOS 7 啟動(dòng)腳本
#####
[Unit]
Description=Keepalived, LVS and VRRP High Availability Monitor
After=syslog.target network.target
[Service]
Type=forking
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
#####
systemctl enable keepalived
systemctl start keepalived
gpedit.msc打開(kāi)組策略



點(diǎn)擊立即更新
yum -y install ntp
執(zhí)行下面的命令
echo -e "\n\n## sync time\n*/10 * * * * root ntpdate 172.16.0.10 > /dev/null && hwclock-w > /dev/null" >> /etc/crontab
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:CentOS7NTPserver+keepalived-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.yijiale78.com/article38/dodssp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站策劃、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容