linux配置时间同步服务

内容纲要

  集群中时间不同步有可能会让大数据的应用程序运行混乱,造成不可预知的问题,比如Hbase,当时间差别过大时就会挂掉,所以在大数据集群中,ntp服务,应该作为一种基础的服务,以下为在CentOS 7集群上配置ntp服务的过程

  1. 设置时区
    #查看当前时区,如果不是中国时区(Asia/Shanghai),则需要先设置为中国时区
    #已经是Asia/Shanghai,则无需设置
    timedatectl status|grep 'Time zone'
    #设置硬件时钟调整为与本地时钟一致
    timedatectl set-local-rtc 1
    #设置时区为上海
    timedatectl set-timezone Asia/Shanghai
  2. 安装ntp时间同步服务
    #首先检查系统中是否安装ntp包,已安装则跳过安装
    rpm -q ntp
    #安装ntp
    yum -y install ntp
    #看ntp是否设置为开启启动状态
    systemctl is-enabled ntpd
    #设置为开机自启动
    systemctl enable ntpd
    #设置成功后,ntp服务并不能立即启动,而是在下次重启之后启动,所以现在手动启动ntp:
    systemctl start ntpd.service
  3. 配置时间服务器
    默认情况下ntp是从外网时间服务器来更新时间的,在集群中使用只要保证集群中所有的服务器时间一致即可,所以先配置其中一台服务器为时间服务器,其他服务器相对来说为这台时间服务器的客户端,从时间服务器上获取时间数据,从而避免联网,可用性更高
    时间服务器ip:192.168.1.201
    配置时间服务器前,建议先和标准时间服务器进行一次同步,比如阿里云的时间服务器

    # 修改时间同步服务器(阿里云)
    # 先停止服务,否则ntp socket会被占用
    systemctl stop ntpd
    # 手动执行同步:ntp1.aliyun.com或ntp2.aliyun.com
    ntpdate ntp1.aliyun.com
    # 继续启动服务
    systemctl start ntpd

    修改ntp配置文件

    #打开ntp配置文件:/etc/ntp.conf
    vim /etc/ntp.conf

    找到server指定时间服务器的位置,这些条都注释掉,在后面新增加2行固定配置

    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    #注释下面四行原来时间服务器
    #server 0.centos.pool.ntp.org iburst
    #server 1.centos.pool.ntp.org iburst
    #server 2.centos.pool.ntp.org iburst
    #server 3.centos.pool.ntp.org iburst
    #新增
    server ntp1.aliyun.com
    #将当前主机作为时间服务器,且向局域网提供时间服务
    fudge 127.127.1.0 stratum 10

    然后在restrict指定的两行下面追加一行

    # Permit all access over the loopback interface.  This could
    # be tightened as well, but to do so would effect some of
    # the administrative functions.
    restrict 127.0.0.1
    restrict ::1
    #新增
    restrict 192.168.1.1 mask 255.255.255.0 nomodify notrap 

    保存后退出,重启ntp服务即可

    systemctl restart ntpd.service
  4. 配置其它服务器与上面配置好的时间服务器同步
    首先同样执行上面的一些命令对ntp进行安装、启动、自启配置、状态查看等操作,然后修改配置文件 /etc/ntp.conf

    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    #注释下面四行原来时间服务器
    #server 0.centos.pool.ntp.org iburst
    #server 1.centos.pool.ntp.org iburst
    #server 2.centos.pool.ntp.org iburst
    #server 3.centos.pool.ntp.org iburst
    #新增
    #将时间服务器设置为局域网内的192.168.1.201
    server 192.168.1.201

    配置好之后,保存,重启ntp服务即可

    markdonw段落缩进方法推荐:
      使用全角空格,切换到全角模式下(一般的中文输入法都是按 shift + space)输入两个空格就行了。这个相对 来说稍微干净一点,而且宽度是整整两个汉字,很整齐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注