简体   繁体   English

Kube.netes 集群默认时区?

[英]Kubernetes cluster default timezone?

I had a question about the timezone used by my Kube.netes Cluster.我对我的 Kube.netes 集群使用的时区有疑问。 I know I can adjust the timezone of the pods( https://evalle.xyz/posts/kube.netes-tz/ ).我知道我可以调整 pod 的时区( https://evalle.xyz/posts/kube.netes-tz/ )。

However, I want to make sure my Cluster always uses UTC in the time zone.但是,我想确保我的集群在时区中始终使用 UTC。 Is this a default option or can it change over time?这是默认选项还是会随时间变化?

Have a look at the documentation Using Container-Optimized OS :查看使用容器优化操作系统的文档:

Container-Optimized OS is the default node OS Image in Kube.netes Engine and other Kube.netes deployments on Google Cloud Platform. Container-Optimized OS 是 Kube.netes Engine 和 Google Cloud Platform 上的其他 Kube.netes 部署中的默认节点操作系统映像

then move to the Changing the time zone for Container-Optimized OS:然后转到更改 Container-Optimized OS 的时区

The default time zone of Container-Optimized OS is UTC0 . Container-Optimized OS 的默认时区UTC0

and

Note that /etc is stateless, so the timezone will be reset to the default (UTC0) every reboot.请注意 /etc 是无状态的,因此每次重新启动时区都会重置为默认值 (UTC0)。

So, if you don't change Image type for your nodes from default Container-Optimized OS to Ubuntu you have nothing to do with time zone settings.因此,如果您不将节点的Image type从默认的 Container-Optimized OS 更改为 Ubuntu,则您与时区设置无关。

In addition, I've checked on my cluster:此外,我检查了我的集群:

$ date
Tue Feb  4 09:15:51 UTC 2020
$ ls -l /etc/ | grep localtime
lrwxrwxrwx 1 root root    25 Jan 29 08:37 localtime -> ../usr/share/zoneinfo/UTC

Containers do not inherit timezones from host machines and have only accessed to the clock from the kernel - which is always UTC.容器不会从主机继承时区,并且只能访问来自 kernel 的时钟——它始终是 UTC。 The default timezone for most images is UTC, yet it is not guaranteed and may be different from container to container since it can be changed on a pod or image level.大多数图像的默认时区是 UTC,但不能保证,并且可能因容器而异,因为它可以在 pod 或图像级别上更改。

You can set pod's timezone by mounting the UTC TZif file from the node machine to /etc/localtime in the container.您可以通过将 UTC TZif 文件从节点机器挂载到容器中的/etc/localtime来设置 pod 的时区。 For example:例如:

apiVersion: v1
kind: Pod
metadata:
  name: date-pod-amsterdam
spec:
  containers:
  - image: ubuntu:21.04
    name: ubuntu
    args:
    - date
    volumeMounts:
    - name: zoneinfo
      mountPath: /etc/localtime
      subPath: UTC
      readOnly: true
  volumes:
  - name: zoneinfo
    hostPath:
      path: /usr/share/zoneinfo
  restartPolicy: OnFailure

Sometimes, containers sets their timezone with TZ environment variable which is prior to /etc/localtime and it is required to set it to UTC too.有时,容器使用/etc/localtime之前的TZ环境变量设置它们的时区,并且也需要将其设置为UTC

spec:
  containers:
  - env:
    - name: TZ
      value: UTC

This process can be simplified by using k8tz , its a kube.netes admission controller and a CLI tool to inject timezones into Pods.这个过程可以通过使用k8tz来简化,它是一个 kube.netes 入口 controller 和一个将时区注入 Pod 的 CLI 工具。 You can install it with helm easily and it will automatically sets those properties on any created pod in the cluster.您可以使用 helm 轻松安装它,它会自动在集群中创建的任何 pod 上设置这些属性。 By default (if not specified otherwise) it enforces UTC.默认情况下(如果没有另外指定)它强制执行 UTC。

helm repo add k8tz https://k8tz.github.io/k8tz/
helm install k8tz k8tz/k8tz

DISCLAIMER: I am the author of k8tz.免责声明:我是 k8tz 的作者。

Looks k8tz is nice, I just tried it, the question, could we control using the k8tz over the pods getting created on specific namespaces not over all namespaces within my k8s clusters.看起来 k8tz 不错,我刚刚试过了,问题是,我们能否控制在特定命名空间上创建的 pod 上使用 k8tz,而不是在我的 k8s 集群中的所有命名空间上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM