Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

CentOS7 NTP server + keepalived

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

NTP installation

Yum-y install ntp

Systemctl enable ntpd

First synchronize the time.

Ntpdate time.windows.com

Ntp configuration (Host1 Host2)

/ etc/ntp.conf

Server time.windows.com preferserver 0.asia.pool.ntp.orgserver 1.asia.pool.ntp.orgserver 2.asia.pool.ntp.org# records the time spent on the last connection between NTP server and upper NTP server driftfile / var/lib/ntp/drift# setting default policy is to allow any host to synchronize time restrict 0.0.0.0 mask 0.0.0.0 nomodify# set pathstatsdir / var/log/ntp/# setting of ntp log Ntp log file 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 installation

(host1, host2)

Yum-y install Keepalived

NTP Health Detection script:

Cat / script/check_ntp.sh

#! / bin/bash# status= "ntp_failed" check failed, status= "ntp_success" check ntp successstatus= "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" fifi

Chmod + x / script/check_ntp.sh

Send an outgoing email alert python script:

Cat / script/keepalived_notify.py

#! / usr/bin/env python#-*-coding:utf-8-*-# use: / bin/python / script/keepalived_notify.py role {master/backup} Native IP virtual machine IPimport smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport sys, time Subprocess# third-party SMTP service mail_host= "smtp.exmail.qq.com" # set server mail_user= "xx@qq.com" # username mail_pass= "mail_password" # password sender = 'xx@qq.com'receivers = [' xx1@qq.com', 'xx@163.com'] # receive alarm email address It can be set to your QQ Mail or other mailbox 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% HGV% MVO% 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 is the SMTP port number smtpObj.login (mail_user,mail_pass) smtpObj.sendmail (sender, receivers, message.as_string ()) print ("message sent successfully") except smtplib.SMTPException as e: print ("Error: unable to send mail") print (e)

Host1 configuration

/ 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 # # when detection fails Priority minus 60 The absolute value of this value is greater than the absolute value of the priority difference between master and standby} vrrp_instance VI_1 {state BACKUP # MASTER/BACKUP interface ens160 virtual_router_id 51 mcast_src_ip 172.16.0.2 priority 150 # MASTER weight is higher advert_int 1 than BACKUP Authentication {auth_type PASS auth_pass GSksLAyTX9ylwG86U2Ez} track_script {chk_http_port # perform NTP Health Inspection} virtual_ipaddress {172.16.0.10} notify_master "/ bin/python / script/keepalived_notify .py master 172.16.0.2 172.16.0.10 "# # execute script notify_backup" / bin/python / script/keepalived_notify.py backup 172.16.0.2 172.16.0.10 "# # execute script when switching to backup}

Host2 configuration

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 # # when detection fails Priority minus 60 The absolute value of this value is greater than the absolute value of the priority difference between master and standby} vrrp_instance VI_1 {state MASTER # MASTER/BACKUP interface ens160 virtual_router_id 51 mcast_src_ip 172.16.0.3 priority 100 # MASTER weight is higher advert_int 1 than BACKUP Authentication {auth_type PASS auth_pass GSksLAyTX9ylwG86U2Ez} track_script {chk_http_port # perform NTP Health Inspection} virtual_ipaddress {172.16.0.10} notify_master "/ bin/python / script/keepalived_notify .py master 172.16.0.3 172.16.0.10 "# # execute script notify_backup" / bin/python / script/keepalived_notify.py backup 172.16.0.3 172.16.0.10 "# # execute script when switching to backup}

Keepalived CentOS 7 startup script

#

[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 client synchronizes time to NTP every 10 minutes

Gpedit.msc Open Group Policy

1.1 start the windows NTP client

1.2 configure the windows NTP client

1.3 time server specified in Internet time settings

Click Update now

2 linux client synchronizes with NTP every 10 minutes 2.1 installs NTP

Yum-y install ntp

2.2 set time synchronization

Execute the following command

Echo-e "\ n\ nrabbit # sync time\ nCompare 10 * root ntpdate 172.16.0.10 > / dev/null & & hwclock-w > / dev/null" > > / etc/crontab

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report