【kubernetes系列学习】如何使pod和host主机的时间保持一致?

2023-10-17 03:50

本文主要是介绍【kubernetes系列学习】如何使pod和host主机的时间保持一致?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

【kubernetes系列学习】如何使pod和host主机的时间保持一致?

问题及主要目的

pod内输出的当前date与host主机上输出的date是不一致的,这样有时候看pod的日志会有迷惑性,下面来解决一下~

实验环境信息

[root@ningan ~]# uname -a
Linux ningan 5.4.0-131-generic #147~18.04.1-Ubuntu SMP Sat Oct 15 13:10:18 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

在Ubuntu上,/etc/localtime是系统的本地时区设置文件,影响到系统的当前date输出。
Ubuntu 18.04上,/etc/localtime是一个symbolic link,链接到文件:/usr/share/zoneinfo/Asia/Shanghai
/usr/share/zoneinfo下存储着真正的时区设置文件,/usr/share/zoneinfo/Asia/Shanghai也是一个符号链接,指向的是/usr/share/zoneinfo/PRC

[root@ningan ~]# ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Sep 24 10:18 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
[root@ningan ~]#
[root@ningan ~]# ls -l /usr/share/zoneinfo/Asia/Shanghai
lrwxrwxrwx 1 root root 6 Aug 30 16:20 /usr/share/zoneinfo/Asia/Shanghai -> ../PRC
[root@ningan ~]#
[root@ningan ~]# ls -l /usr/share/zoneinfo/PRC
-rw-r--r-- 1 root root 582 Aug 30 16:20 /usr/share/zoneinfo/PRC
[root@ningan ~]# file /etc/localtime
/etc/localtime: symbolic link to /usr/share/zoneinfo/Asia/Shanghai[root@ningan ~]# file /usr/share/zoneinfo/Asia/Shanghai
/usr/share/zoneinfo/Asia/Shanghai: symbolic link to ../PRC[root@ningan ~]# file /usr/share/zoneinfo/PRC
/usr/share/zoneinfo/PRC: timezone data, version 2, 3 gmt time flags, 3 std time flags, no leap seconds, 29 transition times, 3 abbreviation chars

实验1:正常创建pod,pod和host的时间不一致

[root@ningan ~]# cat nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.14.2imagePullPolicy: Alwaysports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:labels:app: nginxname: nginx
spec:ports:- port: 9000name: nginx-svcprotocol: TCPtargetPort: 80selector:app: nginx
### 下发yaml,创建pod
[root@ningan ~]# kubectl apply -f nginx.yaml
deployment.apps/nginx-deployment created
### 查看启动的pod和svc
[root@ningan ~]# kubectl get pod,svc -A |grep nginx
default                pod/nginx-deployment-68d5f6d6b7-4rhj4            1/1     Running            0                20s
default                pod/nginx-deployment-68d5f6d6b7-5dm2f            1/1     Running            0                20s
default                pod/nginx-deployment-68d5f6d6b7-mvhpg            1/1     Running            0                20s
default                service/nginx                       ClusterIP   11.254.209.126   <none>        9000/TCP                       20s
### 进入到容器中,查看容器的当前时间
[root@ningan ~]# kubectl exec -it nginx-deployment-68d5f6d6b7-4rhj4 -- sh
# uname -a
Linux nginx-deployment-68d5f6d6b7-4rhj4 5.4.0-131-generic #147~18.04.1-Ubuntu SMP Sat Oct 15 13:10:18 UTC 2022 x86_64 GNU/Linux
# ls -l /etc/localtime
lrwxrwxrwx 1 root root 27 Mar 26  2019 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
# date
Fri Oct 28 15:37:43 UTC 2022
# exit
### 到主机上,查看主机的当前时间
[root@ningan ~]# date
Fri Oct 28 23:37:49 CST 2022

可以看到,pod内的时间和host主机的时间是相差了8个小时的。

实验2:挂载主机的/usr/share/zoneinfo/Asia/Shanghai

修改pod的yaml文件,挂载主机的/usr/share/zoneinfo/Asia/Shanghai到pod的/etc/localtime,保证pod和host的时间一致

[root@ningan ~]# cat nginx-date-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.14.2imagePullPolicy: Alwaysports:- containerPort: 80volumeMounts:- name: tz-configmountPath: /etc/localtimevolumes:- name: tz-confighostPath:path: /usr/share/zoneinfo/Asia/Shanghai
---
apiVersion: v1
kind: Service
metadata:labels:app: nginxname: nginx
spec:ports:- port: 9000name: nginx-svcprotocol: TCPtargetPort: 80selector:app: nginx

在之前的yaml里新加了挂载文件,如下:

在这里插入图片描述

### 重新下发yaml
[root@ningan ~]# kubectl apply -f nginx-date-test.yaml
deployment.apps/nginx-deployment configured
service/nginx unchanged
### 查看启动的pod和svc
[root@ningan ~]# kubectl get pod,svc -owide -A |grep nginx
default                pod/nginx-deployment-ff5879d6b-lxb5h             1/1     Running            0                3m55s   192.169.219.76   master   <none>           <none>
default                pod/nginx-deployment-ff5879d6b-tr2h6             1/1     Running            0                3m59s   192.169.219.75   master   <none>           <none>
default                pod/nginx-deployment-ff5879d6b-zjmzf             1/1     Running            0                4m4s    192.169.219.74   master   <none>           <none>
default                service/nginx                       ClusterIP   11.254.209.126   <none>        9000/TCP                       28m     app=nginx
### 进入到容器中,查看容器的当前时间
[root@ningan ~]# kubectl exec -it nginx-deployment-ff5879d6b-lxb5h -- date
Sat Oct 29 00:03:02 CST 2022
### 到主机上,查看主机的当前时间
[root@ningan ~]# date
Sat Oct 29 00:03:08 CST 2022

已经修改成功,大功告成!

实验3:挂载主机的/etc/localtime

修改pod的yaml文件,挂载主机的/etc/localtime到pod的/etc/localtime,保证pod和host的时间一致

[root@ningan addons]# cat nginx-date-test2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.14.2imagePullPolicy: Alwaysports:- containerPort: 80volumeMounts:- name: tz-configmountPath: /etc/localtimevolumes:- name: tz-confighostPath:path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:labels:app: nginxname: nginx
spec:ports:- port: 9000name: nginx-svcprotocol: TCPtargetPort: 80selector:app: nginx

在实验1的yaml里新加了挂载文件,如下:
在这里插入图片描述

在实验2的yaml里修改了挂载文件,如下:
在这里插入图片描述

### 删除之前pod
[root@ningan addons]# kubectl delete -f nginx-date-test.yaml
deployment.apps "nginx-deployment" deleted
service "nginx" deleted[root@ningan addons]# kubectl get pod,svc -owide -A |grep nginx
[root@ningan addons]#
### 下发新的yaml
[root@ningan addons]# kubectl get pod,svc -owide -A |grep nginx
default                pod/nginx-deployment-b85dc87bc-6zds7             1/1     Running   0                54s     192.169.219.77   master   <none>           <none>
default                pod/nginx-deployment-b85dc87bc-httb5             1/1     Running   0                54s     192.169.219.79   master   <none>           <none>
default                pod/nginx-deployment-b85dc87bc-n4rfm             1/1     Running   0                54s     192.169.219.78   master   <none>           <none>
default                service/nginx                       ClusterIP   11.254.240.43   <none>        9000/TCP                       54s     app=nginx
### 进入到容器中,查看容器的当前时间
[root@ningan addons]# kubectl exec -it nginx-deployment-b85dc87bc-6zds7 -- date
Sat Oct 29 00:15:08 CST 2022
### 到主机上,查看主机的当前时间
[root@ningan addons]# date
Sat Oct 29 00:15:10 CST 2022

大功告成~

参考

Kubernetes集群Pod使用Host的本地时区设置

这篇关于【kubernetes系列学习】如何使pod和host主机的时间保持一致?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/222750

相关文章

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用