使用NTP服务时间同步

1、安装ntp。

[root@k8s-master01 ~]# yum -y install ntp

 2、启动ntp服务。

[root@k8s-master01 ~]# systemctl start ntpd
[root@k8s-master01 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

3、查看ntp服务。

[root@k8s-master01 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-08-04 08:00:47 CST; 7h left
 Main PID: 20746 (ntpd)
    Tasks: 1
   Memory: 768.0K
   CGroup: /system.slice/ntpd.service
           └─20746 /usr/sbin/ntpd -u ntp:ntp -g

Aug 04 08:00:47 node ntpd[20746]: Listen normally on 6 lo ::1 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listen normally on 7 ens33 fe80::8a83:7297:7cb8:9934 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listening on routing socket on fd #24 for interface updates
Aug 04 08:00:47 node systemd[1]: Started Network Time Service.
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c016 06 restart
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c011 01 freq_not_set
Aug 04 08:00:56 node ntpd[20746]: 0.0.0.0 c61c 0c clock_step -28798.353463 s
Aug 04 00:00:57 node ntpd[20746]: 0.0.0.0 c614 04 freq_mode
Aug 04 00:00:58 node ntpd[20746]: 0.0.0.0 c618 08 no_sys_peer

4、修改ntp.conf文件。

# 添加如下内容
[root@k8s-master01 ~]# vim /etc/ntp.conf
[root@k8s-master01 ~]# grep server /etc/ntp.conf 
server ntp.aliyun.com iburst

5、重启服务。

[root@k8s-master01 ~]# systemctl restart ntpd

 6、检查同步状态。

[root@k8s-master01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*stratum2-1.ntp. 194.190.168.1    2 u    4   64    1  170.653    6.791  40.306
+ntp1.as200552.n 195.66.241.3     2 u    4   64    1  236.322    9.481  32.132
+time.cloudflare 10.190.9.35      3 u    3   64    1  220.805  -17.283  20.695
-time.cloudflare 10.210.8.4       3 u    2   64    1  242.932   -4.425  43.233
-203.107.6.88    100.107.25.114   2 u    1   64    1   41.403  -17.157  11.194

使用ntpdate同步

1、使用ntpd命令同步。

[root@k8s-master01 ~]# ntpdate ntp.aliyun.com
14 Apr 08:51:28 ntpdate[1301]: adjust time server 203.107.6.88 offset 0.000744 sec

2、查看时间。

[root@k8s-master01 ~]# date
Fri Apr 14 08:51:53 CST 2023

使用chrony服务同步时间

1、安装chrony服务。

[root@k8s-master01 ~]# yum -y install chrony
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                            | 3.6 kB     00:00     
docker-ce-stable                                | 3.5 kB     00:00     
extras                                          | 2.9 kB     00:00     
kubernetes                                      | 1.4 kB     00:00     
updates                                         | 2.9 kB     00:00     
wlnmp                                           | 2.9 kB     00:00     
(1/8): docker-ce-stable/7/x86_64/updateinfo       |   55 B   00:00     
(2/8): base/7/x86_64/group_gz                     | 153 kB   00:01     
(3/8): docker-ce-stable/7/x86_64/primary_db       | 102 kB   00:01     
(4/8): kubernetes/primary                         | 129 kB   00:00     
(5/8): extras/7/x86_64/primary_db                 | 249 kB   00:01     
(6/8): wlnmp/7/x86_64/primary_db                  | 310 kB   00:00     
(7/8): base/7/x86_64/primary_db                   | 6.1 MB   00:25     
(8/8): updates/7/x86_64/primary_db                |  20 MB   01:17     
kubernetes                                                     953/953
Package chrony-3.4-1.el7.x86_64 already installed and latest version
Nothing to do

2、启动chrony服务。

[root@k8s-master01 ~]# systemctl enable --now chronyd

3、查看chrony服务状态。

[root@k8s-master01 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2023-04-14 08:55:42 CST; 1min 34s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1363 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1359 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1361 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─1361 /usr/sbin/chronyd

Apr 14 08:55:42 k8s-master01 systemd[1]: Starting NTP client/server...
Apr 14 08:55:42 k8s-master01 chronyd[1361]: chronyd version 3.4 star...
Apr 14 08:55:42 k8s-master01 chronyd[1361]: Frequency -93.957 +/- 22...
Apr 14 08:55:42 k8s-master01 systemd[1]: Started NTP client/server.
Apr 14 08:55:50 k8s-master01 chronyd[1361]: Selected source 2606:470...
Apr 14 08:55:53 k8s-master01 chronyd[1361]: Source 193.182.111.142 r...
Hint: Some lines were ellipsized, use -l to show in full.

4、给chrony服务配置文件添加内容。

[root@k8s-master01 ~]# vim /etc/chrony.conf
# 添加内容如下
server ntp1.aliyun.com iburst

5、重启服务。

[root@k8s-master01 ~]# systemctl restart chronyd

6、检查时间同步状态。

[root@k8s-master01 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? tick.ntp.infomaniak.ch        0   6     0     -     +0ns[   +0ns] +/-    0ns
^+ vsrv02141.customer.xenwa>     3   6    17    37    +11ms[+1532us] +/-  140ms
^+ 2402:4e00:1800:f800:0:95>     2   6    17    38    +20ms[  +11ms] +/-  133ms
^* time.cloudflare.com           3   6    17    37  +2046us[-7170us] +/-  129ms

手动修改时间

1、修改时间方法1

[root@k8s-master01 ~]# date -s "2012-05-23 01:01:01"
Wed May 23 01:01:01 CST 2012
[root@k8s-master01 ~]# date
Wed May 23 01:01:05 CST 2012

2、修改时间方法2

修改时间顺序为月日时分年.秒。

[root@tianyi ~]# date 090621282021.28
Mon Sep  6 21:28:28 CST 2021

3、硬件时间向系统时间同步

[root@k8s-master01 ~]# hwclock -w

4、按格式输出时间

[root@k8s-master01 ~]# date "+%Y-%m-%d %H:%M:%S"
2012-05-23 01:04:10

5、设置时区

[root@k8s-master01 ~]# timedatectl list-timezones | grep Shanghai
Asia/Shanghai
[root@k8s-master01 ~]# timedatectl set-timezone Asia/Shanghai