当前位置:网站首页>使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
2022-06-24 12:33:00 【我的小碗汤】
容器在主机的内核上运行,并获得时钟,但时区不是来自内核,而是来自用户空间。在大多数情况下,默认使用协调世界时 (UTC)。

时区的不一致,会带来很多困扰。即使代码与时区无关,但容器日志与系统日志时间相关联排查问题也会让人头疼。一些应用程序使用机器的时区作为默认时区,并希望用户设置时区。当集群中容器的时区不一致时,会出现问题。
k8tz
k8tz是开源项目,请查看:github.com/k8tz/k8tz
k8tz是一个 Kubernetes 准入控制器和一个将时区注入 Pod 的 CLI 工具。
可以用作手动工具来自动转换 Deployment 和 Pod
可以作为准入控制器安装并使用注释来完全自动化创建 Pod 的过程。
k8tz 可以使用 hostPath的方式,或者将 emptyDir 注入 initContainer并用 TZif(时区信息格式) 文件填充卷。然后将 emptyDir挂载到 Pod 每个容器的 /etc/localtime和 /usr/share/zoneinfo。为了确保所需的时区有效,它向所有容器添加了 TZ环境变量。
安装
用 Helm 安装 k8tz准入控制器:
helm repo add k8tz https://k8tz.github.io/k8tz/
helm install k8tz k8tz/k8tz --set timezone=Asia/Shanghai
查看 Pod 状态、Mutatingwebhookconfigurations、Service 等资源是否正常:
# kubectl get mutatingwebhookconfigurations.admissionregistration.k8s.io k8tz
NAME WEBHOOKS AGE
k8tz 1 31m
# kubectl get svc -n k8tz
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
k8tz ClusterIP 10.233.212.11 <none> 443/TCP 31m
# kubectl get pod -n k8tz
NAME READY STATUS RESTARTS AGE
k8tz-59bb7f7cbd-5dzmq 1/1 Running 0 31m
测试
现在可以创建 Pod,不需要任何额外配置:
# kubectl run -i -t ubuntu --image=ubuntu:21.04 --restart=OnFailure --rm=true --command date
Defaulted container "ubuntu" out of: ubuntu, k8tz (init)
Wed Jun 15 14:11:53 CST 2022
pod "ubuntu" deleted
此时的 Pod yaml 如下,环境变量 TZ 使用安装时指定的 Asia/Shanghai,以及注入了 initContainers、volumeMounts、volumes 等配置:
apiVersion: v1
kind: Pod
metadata:
labels:
run: ubuntu
name: ubuntu
namespace: default
spec:
containers:
- command:
- date
env:
- name: TZ
value: Asia/Shanghai
image: ubuntu:21.04
imagePullPolicy: IfNotPresent
name: ubuntu
volumeMounts:
- mountPath: /etc/localtime
name: k8tz
readOnly: true
subPath: Asia/Shanghai
- mountPath: /usr/share/zoneinfo
name: k8tz
readOnly: true
initContainers:
- args:
- bootstrap
image: quay.io/k8tz/k8tz:0.5.0
imagePullPolicy: IfNotPresent
name: k8tz
volumeMounts:
- mountPath: /mnt/zoneinfo
name: k8tz
volumes:
- emptyDir: {
}
name: k8tz
还可以指定 annotations,例如 k8tz.io/timezone=Europe/London选择 pod 的时区:
# kubectl run -i -t ubuntu --image=ubuntu:21.04 --restart=OnFailure --rm=true --command date --annotations k8tz.io/timezone=Europe/London
Defaulted container "ubuntu" out of: ubuntu, k8tz (init)
Wed Jun 15 07:13:42 BST 2022
pod "ubuntu" deleted
或者使用注解 k8tz.io/inject禁用时区注入 Pod :
# kubectl run -i -t ubuntu --image=ubuntu:21.04 --restart=OnFailure --rm=true --command date --annotations k8tz.io/inject=false
Wed Jun 15 06:14:47 UTC 2022
pod "ubuntu" deleted
如果你想使用 hostPath而不是 initContainer方式注入时区配置,可以使用 k8tz.io/strategy注解:
# kubectl run -i -t ubuntu --image=ubuntu:21.04 --restart=OnFailure --rm=true --command date --annotations k8tz.io/strategy=hostPath
Wed Jun 15 14:15:26 CST 2022
pod "ubuntu" deleted
annotations 也可以在命名空间中指定,并影响在命名空间中创建的所有 pod。下面创建一个 test-k8tz namespace 用于测试:
# k create ns test-k8tz
namespace/test-k8tz created
# k annotate ns test-k8tz k8tz.io/strategy=hostPath
namespace/test-k8tz annotated
# k annotate ns test-k8tz k8tz.io/timezone=Europe/London
namespace/test-k8tz annotated
上面将策略设置为 hostPath 注入方式。
因为安装 k8tz 时默认时区已经设置为 Asia/Shanghai,所以这里将 test-k8tz namespace 时区设置为 Europe/London,方便区分。
此时创建的 Pod 不需要加任何注解:
# kubectl run -n test-k8tz -i -t ubuntu --image=ubuntu:21.04 --restart=OnFailure --command date
Wed Jun 15 07:19:48 BST 2022
此时创建的 Pod yaml 如下,此时用的是 hostPath 注入方式:
apiVersion: v1
kind: Pod
metadata:
labels:
run: ubuntu
name: ubuntu
namespace: test-k8tz
spec:
containers:
- command:
- date
env:
- name: TZ
value: Europe/London
image: ubuntu:21.04
imagePullPolicy: IfNotPresent
name: ubuntu
volumeMounts:
- mountPath: /etc/localtime
name: k8tz
readOnly: true
subPath: Europe/London
- mountPath: /usr/share/zoneinfo
name: k8tz
readOnly: true
volumes:
- hostPath:
path: /usr/share/zoneinfo
type: ""
name: k8tz
结论
Kubernetes 中的时区问题有多种解决方案,这些解决方案可以手动实现,但在此过程中存在一些挑战和限制。
使用 k8tz可以自动执行该过程,确保系统中所有组件的时区一致,并且所有组件都可以访问有关不同时区的信息。并且无需额外设置或更改现有资源即可工作,即使在节点上没有所需文件时也是如此。
交流
请关注公众号【进击云原生】,点击下方关注,了解更多咨询,更有免费资源供您学习
边栏推荐
- Variable parameter template implements max (accepts multiple parameters, two implementation methods)
- Practice of dynamic load balancing based on open source tars
- 炒伦敦金短线稳定赚钱技巧?在哪里炒伦敦金安全靠谱?
- What should music website SEO do?
- 深度学习~11+高分疾病相关miRNA研究新视角
- Opencv learning notes - Discrete Fourier transform
- Remote terminal RTU slope monitoring and early warning
- Opencv learning notes -- Separation of color channels and multi-channel mixing
- 嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (下)
- Chenglixin research group of Shenzhen People's hospital proposed a new method of multi group data in the diagnosis and prognosis analysis of hepatocellular carcinoma megps
猜你喜欢
[mysql_16] variables, process control and cursors

GTEST from getting started to getting started
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
![[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map

How is the e-commerce red envelope realized? For interview (typical high concurrency)

ArrayList # sublist these four holes, you get caught accidentally

How stupid of me to hire a bunch of programmers who can only "Google"!

Group planning - General Review

FreeRTOS概述与体验
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue
随机推荐
Is it safe to open an account under the conditions of new bonds
Tencent security monthly report - zero trust development trend forum, digital Expo Technology Award, Mercedes Benz security research results
Getting started with scrapy
可变参数模板实现max(接受多个参数,两种实现方式)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
Istio practical skills: implement header based authorization
怎样打新债具体操作 开户是安全的吗
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
文本转语音功能上线,可以体验专业播音员的服务,诚邀试用
Conceptual analysis of DDD Domain Driven Design
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 1)
Pipeline post instruction
[2021 techo youth dry goods sorting post, there is always one you are interested in]
Ingenious conception - iron death regulatory factor classification and prognosis 6+
我真傻,招了一堆只会“谷歌”的程序员!
Introduction to C language circular statements (foe, while, do... While)
Kubernetes practical skills: use cert manager to issue free certificates for DNSPod domain names
5分+的单基因泛癌纯生信思路!
11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
[tke] GPU node NVIDIA Tesla driver reinstallation