如何给tke集群的pod设置时区

腾讯云容器服务(TKE)集群中容器系统时间默认为 UTC 协调世界时间 (Universal Time Coordinated),与节点本地所属时区 CST (上海时间)相差8个小时。在容器使用过程中,当需要获取系统时间用于日志记录、数据库存储等相关操作时,容器内时区不一致问题将会带来一系列困扰。

默认时间不支持直接以集群为单位进行修改,但可在单个容器内进行修改。

1. 制作镜像设置时区

要想修改容器时区,我们可以在做镜像的时候就将时区设置好,具体可以参考tke官方文档
https://cloud.tencent.com/document/product/457/41877

2. pod设置环境变量配置时区

首先我们起一个测试pod,不配置时区,看下容器内是什么时间

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: time-zone-test
    qcloud-app: time-zone-test
  name: time-zone-test
  namespace: weixnie
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: time-zone-test
      qcloud-app: time-zone-test
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s-app: time-zone-test
        qcloud-app: time-zone-test
    spec:
      containers:
      - args:
        - 700d
        command:
        - sleep
        image: centos:7
        imagePullPolicy: Always
        name: time-zone-test
        resources: {}
        securityContext:
          privileged: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}

容器启动后,进入容器查看时间是utc时间,和本地相差8小时

下面我们用环境变量设置下时区

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: time-zone-test
    qcloud-app: time-zone-test
  name: time-zone-test
  namespace: weixnie
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: time-zone-test
      qcloud-app: time-zone-test
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s-app: time-zone-test
        qcloud-app: time-zone-test
    spec:
      containers:
      - args:
        - 700d
        command:
        - sleep
        env:
        - name: TZ
          value: Asia/Shanghai
        image: centos:7
        imagePullPolicy: Always
        name: time-zone-test
        resources: {}
        securityContext:
          privileged: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always

再登录pod的时候,容器时间就和本地时间一致了

3. 挂载本地时区文件

一般节点的时区是cst时区,我们可以将节点的时区文件挂载到容器里面,这样容器内就变成上海时间了

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: time-zone-test
    qcloud-app: time-zone-test
  name: time-zone-test
  namespace: weixnie
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: time-zone-test
      qcloud-app: time-zone-test
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s-app: time-zone-test
        qcloud-app: time-zone-test
    spec:
      containers:
      - args:
        - 700d
        command:
        - sleep
        image: centos:7
        imagePullPolicy: Always
        name: time-zone-test
        resources: {}
        securityContext:
          privileged: false
        volumeMounts:
        - mountPath: /etc/localtime
          name: config
          readOnly: true
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
      volumes:
      - hostPath:
          path: /etc/localtime
          type: File
        name: config

登录容器,发现node节点的时区文件挂载到容器内了,然后时间也变成了上海时间

版权声明:
作者:聂伟星
链接:https://jkboy.com/archives/17754.html
来源:随风的博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
如何给tke集群的pod设置时区
腾讯云容器服务(TKE)集群中容器系统时间默认为 UTC 协调世界时间 (Universal Time Coordinated),与节点本地所属时区 CST (...
<<上一篇
下一篇>>