最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

K8S修改Pod時(shí)間方案詳細(xì)代碼實(shí)例

 更新時(shí)間:2025年08月21日 10:35:06   作者:SixSixzero  
在Kubernetes中,修改Pod的時(shí)間通常指的是修改Pod的時(shí)區(qū)設(shè)置,因?yàn)镻od的時(shí)間戳(如創(chuàng)建時(shí)間)是由Kubernetes集群管理的,并且通常不建議直接修改,這篇文章主要介紹了K8S修改Pod時(shí)間方案的相關(guān)資料,需要的朋友可以參考下

需求:

不修改宿主系統(tǒng)時(shí)間前提下,只修改Pod 或 容器進(jìn)程時(shí)間。

方案:

  1. 本地部署faketime
  2. 部署fake-time-injector 插件 
  3. 對Pod新增sidecar (比較靈活)

本地部署faketime:

1. 部署 faketime (所有節(jié)點(diǎn))

yum install faketime -y

2. yaml文件添加對應(yīng)環(huán)境

apiVersion: v1
kind: Pod
metadata:
  name: fake-time-pod
spec:
  containers:
  - name: app
    image: registry.cn-hangzhou.aliyuncs.com/acs/testc:v1
    env:
    - name: LD_PRELOAD
      value: /usr/lib64/faketime/libfaketime.so.1 
    - name: FAKETIME
      value: "+2y"  # 設(shè)置為未來2年,使用"-2y"表示過去2年
    volumeMounts:
    - name: faketime-lib
      mountPath: /usr/lib64/faketime 
  volumes:
  - name: faketime-lib
    hostPath:
      path: /usr/lib64/faketime 

部署fake-time-injector 插件

vi fake-time-injector.yaml,新增以下內(nèi)容

apiVersion: v1
kind: ServiceAccount
metadata:
  name: fake-time-injector-sa
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: fake-time-injector-cr
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "patch", "update", "watch"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "list"]
  - apiGroups: ["admissionregistration.k8s.io"]
    resources: ["mutatingwebhookconfigurations"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: fake-time-injector-rb
subjects:
  - kind: ServiceAccount
    name: fake-time-injector-sa
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: fake-time-injector-cr
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubernetes-faketime-injector
  namespace: kube-system
  labels:
    app: kubernetes-faketime-injector
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kubernetes-faketime-injector
  template:
    metadata:
      labels:
        app: kubernetes-faketime-injector
    spec:
      containers:
        - image: registry-cn-hangzhou.ack.aliyuncs.com/acs/fake-time-injector:v5     #  使用 fake-time-injector/Dockerfile 構(gòu)建鏡像
          imagePullPolicy: Always
          name: kubernetes-faketime-injector
          resources:
            limits:
              cpu: 100m
              memory: 100Mi
            requests:
              cpu: 100m
              memory: 100Mi
          env:
            - name: CLUSTER_MODE     # CLUSTER_MODE為true時(shí),命名空間內(nèi)的所有pod在一定時(shí)間范圍內(nèi)(40s)啟動(dòng)時(shí)獲得一致的偏移量
              value: "true"
            - name: Namespace_Delay_Timeout     # 命名空間內(nèi)的所有pod在一定時(shí)間范圍內(nèi)(120s)啟動(dòng)時(shí)獲得一致的偏移量, 默認(rèn)值為40s.
              value: "120"
            - name: LIBFAKETIME_PLUGIN_IMAGE
              value: "registry.cn-hangzhou.aliyuncs.com/acs/libfaketime:v1"
            - name: FAKETIME_PLUGIN_IMAGE
              value: "registry-cn-hangzhou.ack.aliyuncs.com/acs/fake-time-sidecar:v4.1"   # 使用 fake-time-injector/plugins/faketime/build/Dockerfile 創(chuàng)建鏡像
      serviceAccountName:  fake-time-injector-sa
---
kind: Service
apiVersion: v1
metadata:
  name: kubernetes-faketime-injector
  namespace: kube-system
spec:
  ports:
    - port: 443
      targetPort: 443
      name: webhook
  selector:
    app: kubernetes-faketime-injector

 kubectl apply -f  fake-time-injector.yaml

對Pod修改時(shí)間方式:

libfaketime

apiVersion: v1
kind: Pod
metadata:
  name: test
  labels:
    app: myapp
    version: v1
#  namespace: test
  annotations:
    cloudnativegame.io/fake-time-rate: "1.0"  # 時(shí)間流逝速率(1.0=正常速度)
    cloudnativegame.io/fake-time: "-10d" # 初始時(shí)間(支持絕對時(shí)間或相對偏移,如 +3h/-7h)
    cloudnativegame.io/fake-time-enabled: "true"  # 確保時(shí)間模擬功能完全激活
    cloudnativegame.io/time-source: "incremental" # 指定增量時(shí)間源
spec:
  containers:
    - name: test
      image: registry.cn-hangzhou.aliyuncs.com/acs/testc:v1
      volumeMounts:
        - name: host-timezone
          mountPath: /etc/localtime  # 掛載宿主機(jī)時(shí)區(qū)文件
          readOnly: true             # 只讀模式確保安全
  volumes:
    - name: host-timezone
      hostPath:
        path: /etc/localtime  # 宿主機(jī)時(shí)區(qū)文件路徑
        type: File            # 明確指定資源類型為文件

 結(jié)果:

watchmaker

apiVersion: v1
kind: Pod
metadata:
  name: testpod
  labels:
    app: myapp
    version: v1
  annotations:
    cloudnativegame.io/process-name: "hello"     # 如果需要同時(shí)修改多個(gè)進(jìn)程用`,`隔開進(jìn)程名即可
    cloudnativegame.io/fake-time: "2030-01-01 00:00:00"     # 此處還可以配置調(diào)整的秒數(shù),'86400'表示時(shí)間向后漂移一天,watchmaker不支持過去的時(shí)間。
spec:
  containers:
    - name: myhello
      image: registry.cn-hangzhou.aliyuncs.com/acs/hello:v1 
      env:
        - name: Modify_Sub_Process        # Modify_Sub_Process為true時(shí),同時(shí)修改子進(jìn)程的時(shí)間。
          value: "true"

進(jìn)入容器查看時(shí)間 

結(jié)果:

采用sidecar方式

即Pod 新增一個(gè)修改時(shí)間的容器

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: hello
  name: hello
spec:
  containers:
    - image: 'registry.cn-hangzhou.aliyuncs.com/acs/hello:v1'
      imagePullPolicy: IfNotPresent
      name: myhello
    - env:
        - name: modify_process_name
          value: hello               # 如果需要同時(shí)修改多個(gè)進(jìn)程用`,`隔開進(jìn)程名即可
        - name: delay_second
          value: '86400'
      image: 'registry-cn-hangzhou.ack.aliyuncs.com/acs/fake-time-sidecar:v4.1'
      imagePullPolicy: Always
      name: fake-time-sidecar
  shareProcessNamespace: true

問題1:

同namespace下 多個(gè)pod 同時(shí)啟動(dòng)時(shí),可能會(huì)相互影響

podA 時(shí)間設(shè)定是:+2y , podB 時(shí)間設(shè)定是 -10d

那么有可能 podA 和podB 都是 -10d 或者都是 -2y

kubectl apply -f nginx.yaml & kubectl apply -f test.yaml 

解決方案:

1.延遲啟動(dòng) (具體延遲多久根據(jù)fake-time-injector 配置的時(shí)間)

2.劃分命名空間(不推薦)

3.修改fake-time-injector

4.采用sidecar方式

修改fake-time-injector:

方案一中部署的fake-time-injector插件,其中需要修改CLUSTER_MODE 和Namespace_Delay_Timeout 

問題2:

時(shí)間設(shè)定問題,

絕對偏移量 如"2024-12-01 12:00:00" 時(shí)間凍結(jié)這個(gè)時(shí)間點(diǎn),不會(huì)變動(dòng)。

相對偏移量 如"'+3h'或者'-20m'" ,時(shí)間會(huì)變動(dòng) 只能針對一個(gè)時(shí)間單位修改,

如 +7h -20m +30s -1y 不能 -1y10d  7h20m 這種形式。

# 可以下載faketime 單獨(dú)嘗試。 faketime -f '-7h' date 查看效果

絕對偏移量:cloudnativegame.io/fake-time: "2024-12-01 12:00:00" 

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    cloudnativegame.io/fake-time-rate: "1.0"  # 時(shí)間流逝速率(1.0=正常速度)
    cloudnativegame.io/fake-time: "2024-12-01 12:00:00" # 初始時(shí)間(支持絕對時(shí)間或相對偏移,如 +3h/-7h)
    cloudnativegame.io/fake-time-enabled: "true"  # 確保時(shí)間模擬功能完全激活
    cloudnativegame.io/time-source: "incremental" # 指定增量時(shí)間源
spec:
  containers:
  - name: my-container
    image: nginx
    resources:
      requests:
        cpu: "0.1"
        memory: "50Mi"
      limits:
        cpu: "1"
        memory: "100Mi"
    volumeMounts:
      - name: host-timezone
        mountPath: /etc/localtime  # 掛載宿主機(jī)時(shí)區(qū)文件
        readOnly: true             # 只讀模式確保安全
  volumes:
    - name: host-timezone
      hostPath:
        path: /etc/localtime  # 宿主機(jī)時(shí)區(qū)文件路徑
        type: File            # 明確指定資源類型為文件

相對偏移量:

只會(huì)識(shí)別最后的單位以及數(shù)字

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    cloudnativegame.io/fake-time-rate: "1.0"  # 時(shí)間流逝速率(1.0=正常速度)
    cloudnativegame.io/fake-time: "+2y10d2h" # 初始時(shí)間(支持絕對時(shí)間或相對偏移,如 +3h/-7h20m)           
    cloudnativegame.io/fake-time-enabled: "true"  # 確保時(shí)間模擬功能完全激活
    cloudnativegame.io/time-source: "incremental" # 指定增量時(shí)間源
spec:
  containers:
  - name: my-container
    image: nginx
    resources:
      requests:
        cpu: "0.1"
        memory: "50Mi"
      limits:
        cpu: "1"
        memory: "100Mi"
    volumeMounts:
      - name: host-timezone
        mountPath: /etc/localtime  # 掛載宿主機(jī)時(shí)區(qū)文件
        readOnly: true             # 只讀模式確保安全
  volumes:
    - name: host-timezone
      hostPath:
        path: /etc/localtime  # 宿主機(jī)時(shí)區(qū)文件路徑
        type: File            # 明確指定資源類型為文件

只識(shí)別最后的單位以及數(shù)字,增加兩小時(shí)

總結(jié)

到此這篇關(guān)于K8S修改Pod時(shí)間方案的文章就介紹到這了,更多相關(guān)K8S修改Pod時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

德惠市| 图片| 绥德县| 稻城县| 罗江县| 大方县| 贵阳市| 中方县| 株洲市| 林甸县| 龙南县| 彭山县| 马龙县| 莱阳市| 容城县| 通海县| 尉氏县| 东阿县| 当阳市| 大埔县| 龙川县| 秦皇岛市| 西安市| 甘孜| 陈巴尔虎旗| 墨玉县| 永寿县| 饶河县| 新竹县| 新龙县| 巨鹿县| 灵川县| 济宁市| 紫云| 封丘县| 浪卡子县| 通海县| 沙河市| 色达县| 祁连县| 雷州市|