Kubernetes:修改系统时区

方式一:手动设置时区

1
2
3
4
5
6
7
8
9
10
11
12
# 查看当前时间
date

# 检查时区文件是否存在
ll /usr/share/zoneinfo/Asia/Shanghai

# 删除旧链接并设置新时区
rm -f /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 验证时间
date

方式二:使用 timedatectl 和 chrony

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看当前时间设置
timedatectl

# 设置为上海时区
timedatectl set-timezone Asia/Shanghai

# 验证设置
timedatectl

# 安装 chrony 时间同步服务
yum install -y chrony

# 查看时间源状态
chronyc -n sources -v

# 查看时间同步状态
chronyc tracking

方式三:使用 NTP 服务同步

基础 NTP 同步

1
2
3
4
5
6
7
8
# 安装 ntpdate
yum install -y ntpdate

# 与公共 NTP 服务器同步
ntpdate -u ntp.api.bz

# 验证时间
timedatectl

内网 NTP 服务器配置

1. 检查并安装 NTP 服务

1
2
3
4
5
# 检查是否已安装 ntpd
rpm -qa | grep ntpd

# 如未安装则进行安装
yum -y install ntpd

2. 配置 NTP 服务

1
2
3
4
5
6
7
8
9
10
11
# 查看服务状态
service ntpd status

# 启动服务
service ntpd start

# 设置开机启动
chkconfig ntpd on

# 编辑配置文件
vim /etc/ntp.conf

/etc/ntp.conf 中进行以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
# 允许特定网段同步时间
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

# 注释默认的公共 NTP 服务器
# server 0.centos.pool.ntp.org
# server 1.centos.pool.ntp.org
# server 2.centos.pool.ntp.org
# server 3.centos.pool.ntp.org

# 启用本地时钟
server 127.127.1.0 # localclock
fudge 127.127.1.0 stratum 10

3. 配置硬件时钟同步

1
2
3
4
5
6
7
8
# 编辑配置文件
vim /etc/sysconfig/ntpd

# 添加以下内容
SYNC_HWCLOCK=yes

# 重启服务
service ntpd restart

4. 客户端同步配置

1
2
# 客户端与主服务器同步时间
ntpdate 192.168.100.201

注意事项

  1. 方式一适合快速设置时区但不提供时间同步功能
  2. 方式二使用 systemd 的 timedatectl 和 chrony 服务,适合现代 Linux 系统
  3. 方式三适合需要内网时间服务器的情况
  4. 在重要生产环境中,建议使用内网时间服务器(方式三)确保时间一致性
  5. 配置完成后可使用 ntpq -p 命令查看 NTP 对等体状态