设为首页 收藏本站
查看: 869|回复: 0

[经验分享] CentOS7 NTP server + keepalived

[复制链接]

尚未签到

发表于 2018-4-24 09:32:37 | 显示全部楼层 |阅读模式
DSC0000.jpg

  

  NTP安装
  yum -y install ntp
  systemctl enable ntpd

  

  首先同步一次时间
  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连接接所花费的时间
driftfile /var/lib/ntp/drift

#设置默认策略为允许任何主机进行时间同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify

#设置ntp日志的path
statsdir /var/log/ntp/

#设置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健康检测脚本:
  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
  

  

  向外发送邮件告警python脚本:
  cat /script/keepalived_notify.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# use: /bin/python /script/keepalived_notify.py 角色{master/backup} 本机IP 虚拟机IP

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import sys, time, subprocess

# 第三方 SMTP 服务
mail_host="smtp.exmail.qq.com"  #设置服务器
mail_user="xx@qq.com"    #用户名
mail_pass="mail_password"   #口令

sender = 'xx@qq.com'
receivers = ['xx1@qq.com', 'xx@163.com']  # 接收告警邮件地址,可设置为你的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 端口号
    smtpObj.login(mail_user,mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException as e:
    print("Error: 无法发送邮件")
    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    ## 当检测失败时,优先级减60,该值的绝对要大于主备优先级差的绝对值
}

vrrp_instance VI_1 {
        state BACKUP        ############ MASTER/BACKUP
        interface ens160
        virtual_router_id 51
        mcast_src_ip 172.16.0.2
        priority 150                  ########### MASTER权值要比BACKUP高
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass GSksLAyTX9ylwG86U2Ez
        }
        track_script {
                chk_http_port ### 执行NTP健康检测
        }
        virtual_ipaddress {
                172.16.0.10
        }
        notify_master "/bin/python /script/keepalived_notify.py master 172.16.0.2 172.16.0.10"    ## 当切换为master时执行脚本
        notify_backup "/bin/python /script/keepalived_notify.py backup 172.16.0.2 172.16.0.10"    ## 当切换为backup时执行脚本
}  

  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    ## 当检测失败时,优先级减60,该值的绝对要大于主备优先级差的绝对值
}
vrrp_instance VI_1 {
        state MASTER        ############ MASTER/BACKUP
        interface ens160
        virtual_router_id 51
        mcast_src_ip 172.16.0.3
        priority 100                  ########### MASTER权值要比BACKUP高
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass GSksLAyTX9ylwG86U2Ez
        }
        track_script {
                chk_http_port ### 执行NTP健康检测
        }
        virtual_ipaddress {
                172.16.0.10
        }
        notify_master "/bin/python /script/keepalived_notify.py master 172.16.0.3 172.16.0.10"    ## 当切换为master时执行脚本
        notify_backup "/bin/python /script/keepalived_notify.py backup 172.16.0.3 172.16.0.10"    ## 当切换为backup时执行脚本
}  

  keepalived CentOS 7 启动脚本
  #####
[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
  

1 windows客户端向NTP每10分钟同步一次时间
  

  gpedit.msc打开组策略
  

1.1 启动windows NTP客户端
DSC0001.png

1.2 配置windows NTP客户端
DSC0002.png

1.3 Internet时间设置里指定时间服务器
DSC0003.png

  点击立即更新
  

  

2 linux客户端向NTP每10分钟同步一次时间
2.1 安装NTP
  yum -y install ntp
2.2 设置时间同步
  执行下面的命令
  echo -e "\n\n## sync time\n*/10 * * * *    root    ntpdate 172.16.0.10 > /dev/null && hwclock-w > /dev/null" >> /etc/crontab
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451206-1-1.html 上篇帖子: 在CentOS 6上安装EPEL源 下篇帖子: jumpserver 3.0 Centos
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表