CentOS7 集群使用 NTP 同步所有服务器的时间

在 CentOS 7 集群中,你可以通过配置 NTP(Network Time Protocol)来让其他服务器从可以联网的服务器同步时间。以下是步骤:

1. 在可以联网的服务器上配置 NTP 服务器

  1. 安装 NTP 服务

    在可以联网的服务器上安装 NTP 服务:

    1
    sudo yum install -y ntp
  2. 配置 NTP 服务器

    编辑 NTP 配置文件 /etc/ntp.conf,将其配置为网络时间服务器:

    1
    sudo vi /etc/ntp.conf

    在文件中,找到 server 配置项,并根据需要添加/修改为以下内容:

    1
    2
    3
    4
    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

    然后,确保本地网络中的服务器可以从这个服务器获取时间:

    1
    2
    # Allow local network to synchronize time
    restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

    这里的 IP 地址范围需要根据你的实际网络进行调整。

  3. 启动 NTP 服务

    启动并启用 NTP 服务:

    1
    2
    sudo systemctl start ntpd
    sudo systemctl enable ntpd
  4. 检查 NTP 服务状态

    检查 NTP 服务是否正常运行:

    1
    sudo systemctl status ntpd

2. 配置其他服务器从 NTP 服务器同步时间

  1. 安装 NTP 客户端

    在其他服务器上安装 NTP 客户端:

    1
    sudo yum install -y ntp
  2. 配置 NTP 客户端

    编辑 NTP 客户端配置文件 /etc/ntp.conf,将其配置为使用刚才设置的 NTP 服务器:

    1
    sudo vi /etc/ntp.conf

    修改 server 配置项为 NTP 服务器的 IP 地址:

    1
    server <NTP_SERVER_IP> iburst

    替换 <NTP_SERVER_IP> 为可以联网的服务器的实际 IP 地址。

  3. 启动 NTP 客户端服务

    启动并启用 NTP 客户端服务:

    1
    2
    sudo systemctl start ntpd
    sudo systemctl enable ntpd
  4. 检查同步状态

    确认客户端是否成功同步时间:

    1
    ntpq -p

这些步骤应该能帮助你在局域网中的 CentOS 7 服务器上实现时间同步。如果有其他问题或需要进一步的帮助,请随时告知!

为了批量执行时间同步配置,你可以使用以下方法:

1. 使用 Ansible

Ansible 是一个强大的自动化工具,适用于大规模服务器配置管理。你可以通过编写 Ansible Playbook 来批量配置所有服务器的时间同步。

安装 Ansible

在可以联网的服务器上安装 Ansible:

1
sudo yum install -y ansible

配置 inventory 文件

创建一个包含所有需要同步时间的服务器的 inventory 文件,例如 hosts

1
2
3
4
[all_servers]
server1 ansible_host=192.168.1.2
server2 ansible_host=192.168.1.3
...

编写 Ansible Playbook

创建一个 Playbook 文件,例如 sync_time.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
---
- hosts: all_servers
become: yes
tasks:
- name: Install ntp
yum:
name: ntp
state: present

- name: Configure ntp.conf
lineinfile:
path: /etc/ntp.conf
regexp: '^server'
line: 'server <NTP_SERVER_IP> iburst'
state: present

- name: Start and enable ntpd service
systemd:
name: ntpd
enabled: yes
state: started

<NTP_SERVER_IP> 替换为你已联网的服务器的 IP 地址。

执行 Playbook

使用以下命令执行 Playbook,在所有服务器上批量配置时间同步:

1
ansible-playbook -i hosts sync_time.yml

2. 使用 SSH 批量执行命令

如果你不想使用 Ansible,可以通过 SSH 在多个服务器上批量执行命令。

编写 Bash 脚本

编写一个简单的 Bash 脚本,通过 SSH 批量在所有服务器上执行命令:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

SERVERS=("192.168.1.2" "192.168.1.3" ...)

for SERVER in "${SERVERS[@]}"; do
ssh root@$SERVER << EOF
yum install -y ntp
sed -i 's/^server.*/server <NTP_SERVER_IP> iburst/' /etc/ntp.conf
systemctl start ntpd
systemctl enable ntpd
EOF
done

<NTP_SERVER_IP> 替换为可以联网的服务器的实际 IP 地址。

运行脚本

给予脚本执行权限并运行它:

1
2
chmod +x sync_time.sh
./sync_time.sh

3. 使用 Parallel SSH (PSSH)

PSSH 是一种用于在多台服务器上并行执行命令的工具。

安装 PSSH

在控制服务器上安装 PSSH

1
sudo yum install -y pssh

创建服务器列表文件

创建一个包含所有目标服务器的文件,例如 servers.txt

1
2
3
192.168.1.2
192.168.1.3
...

使用 PSSH 执行命令

通过 PSSH 批量执行命令:

1
pssh -h servers.txt -l root -i "yum install -y ntp && sed -i 's/^server.*/server <NTP_SERVER_IP> iburst/' /etc/ntp.conf && systemctl start ntpd && systemctl enable ntpd"

<NTP_SERVER_IP> 替换为可以联网的服务器的实际 IP 地址。

以上方法都可以用来批量在服务器上配置时间同步。根据你的实际环境选择合适的方法。