使用k8tz优雅的解决pod内的时区问题
1.问题简介
容器在主机的内核上运行,并获得时钟,但时区不是来自内核,而是来自用户空间。在大多数情况下,默认使用协调世界时 (utc)。 时区的不一致,会带来很多困扰。即使代码与时区无关,但容器日志与系统日志时间相关联排查问题也会让人头疼。一些应用程序使用机器的时区作为默认时区,并希望用户设置时区。当集群中容器的时区不一致时,管理会很不容易。
2.k8tz
k8tz开源地址: https://github.com/k8tz/k8tz
k8tz
是一个 kubernetes 准入控制器和一个将时区注入 pod 的 cli 工具。可以用作手动工具来自动转换 deployment 和 pod 可以作为准入控制器安装并使用注释来完全自动化创建 pod 的过程。
k8tz
可以使用hostpath
的方式,或者将emptydir
注入initcontainer
并用 tzif(时区信息格式) 文件填充卷。然后将emptydir
挂载到 pod 每个容器的 /etc/localtime
和 /usr/share/zoneinfo
。为了确保所需的时区有效,它向所有容器添加了 tz
环境变量。
3.安装k8t
#官方提供的helm部署方式 helm repo add k8tz https://k8tz.github.io/k8tz/ helm install k8tz k8tz/k8tz --set timezone=asia/beijing ###除了helm安装官方还提供了其它方式,可以查看官方文档。 #查看 pod 状态、mutatingwebhookconfigurations、service 等资源是否正常: [root@master ~]# kubectl get mutatingwebhookconfigurations.admissionregistration.k8s.io name webhooks age k8zt-k8tz 1 2m [root@master ~]# kubectl get all -n k8tz name ready status restarts age pod/k8zt-k8tz-7559df766-zlxdz 1/1 running 0 2m36s name type cluster-ip external-ip port(s) age service/k8zt clusterip 10.68.247.230 <none> 443/tcp 2m36s name ready up-to-date available age deployment.apps/k8zt-k8tz 1/1 1 1 2m36s name desired current ready age replicaset.apps/k8zt-k8tz-7559df766 1 1 1 2m36s
4.注入策略
官方提供了三种策略:hostpath、initcontainer、annotations
这里主要讲annotations方式
#annotations主要有三个键值对 #k8tz.io/inject: true/false 是否禁止注入,当注入时区操作和禁止注入同时存在时,注入时区的优先级会高于禁止注入 #k8tz.io/timezone: asia/beijing 注入时区,时区选择根据自己需求 #k8tz.io/strategy: hostpath/initcontainer 提供了挂载本地文件和init容器两种方式 #测试 #部署一个测试ng [root@master k8tz]# cat ng1.yaml apiversion: apps/v1 kind: deployment metadata: name: nginx spec: replicas: 1 selector: matchlabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:alpine ports: - containerport: 80 [root@master k8tz]# date mon oct 10 15:02:41 cst 2022 [root@master k8tz]# kubectl exec -it nginx-7fb7fd49b4-xm5wr sh kubectl exec [pod] [command] is deprecated and will be removed in a future version. use kubectl exec [pod] -- [command] instead. / # date mon oct 10 07:02:54 utc 2022 ##添加注释 k8tz.io/timezone: asia/beijing [root@master k8tz]# cat ng2.yaml apiversion: apps/v1 kind: deployment metadata: name: nginx2 annotations: k8tz.io/timezone: asia/beijing spec: replicas: 1 selector: matchlabels: app: nginx2 template: metadata: labels: app: nginx2 spec: containers: - name: nginx2 image: nginx:alpine ports: - containerport: 80 [root@master k8tz]# date mon oct 10 15:06:14 cst 2022 [root@master k8tz]# kubectl exec -it nginx2-67b5db4568-zhps7 sh kubectl exec [pod] [command] is deprecated and will be removed in a future version. use kubectl exec [pod] -- [command] instead. / # date mon oct 10 07:06:25 utc 2022 #查看nginx2的启动流程,可以看到先启动了一个k8tz的init容器 events: type reason age from message ---- ------ ---- ---- ------- normal scheduled 111s default-scheduler successfully assigned default/nginx2-67b5db4568-zhps7 to 192.168.130.176 normal pulled 111s kubelet container image "quay.io/k8tz/k8tz:0.8.0" already present on machine normal created 111s kubelet created container k8tz normal started 111s kubelet started container k8tz normal pulled 110s kubelet container image "nginx:alpine" already present on machine normal created 110s kubelet created container nginx2 normal started 110s kubelet started container nginx2 ##annotations 也可以在命名空间中指定,并影响在命名空间中创建的所有 pod。下面创建一个 test namespace 用于测试: #因为k8tz默认会对新创建的pod更改时区所以这里测试用了shanghai和之前的beijing做区分 [root@master ~]# kubectl create ns test [root@master ~]# kubectl annotate ns test k8tz.io/strategy=hostpath namespace/test annotated [root@master ~]# kubectl annotate ns test k8tz.io/timezone=asia/shanghai namespace/test annotated #可以看到新创建的nginx2的时区为shanghai name: nginx2-67b5db4568-9zjpb namespace: test priority: 0 node: 192.168.130.176/192.168.130.176 start time: mon, 10 oct 2022 15:26:35 +0800 labels: app=nginx2 pod-template-hash=67b5db4568 annotations: k8tz.io/injected: true k8tz.io/timezone: asia/shanghai status: running
坑和解决办法
1.helm安装失败
#有时候会因为网络问题导致安装失败 error: failed to download "k8tz/k8tz" (hint: running `helm repo update` may help) #可以先helm拉到本地再安装,多尝试几次安装也可 [root@master root]# helm install k8zt --set timezone=asia/beijing k8tz-0.8.0.tgz name: k8zt last deployed: mon oct 10 14:46:55 2022 namespace: default status: deployed revision: 1
2.查看时间的顺序
查看pod时区时一定要在安装k8tz之前操作,安装k8tz时会默认添加时区给新创建的pod自动添加k8tz.io/timezone
到此这篇关于使用k8tz优雅的解决pod内的时区问题的文章就介绍到这了,更多相关k8tz时区问题解决内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论