因业务订单需要生成报表需要修改单个k8s的单个pod时间,而且不影响其他业务得使用
之前测试alpine时发现兼容性不好,ubuntu验证正常

Dockerfile

FROM ubuntu:latest as builder
RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends wget unzip make gcc g++ \
    && apt-get autoclean -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

RUN wget --no-check-certificate https://github.com/wolfcw/libfaketime/archive/master.zip \
    && unzip master.zip \
    && cd libfaketime-master \
    && make && make install \
    && cd ../ \
    && rm -rf libfaketime-master master.zip

#修改成您要使用的镜像
FROM  harbor.mvmyun.com/library/openjdk:11.0.7-jre-slim
LABEL auth="Myki"
LABEL maintainer="21kixc@gmail.com"

COPY --from=builder /usr/local/lib/faketime /usr/local/lib/faketime 
COPY --from=builder /usr/local/bin/faketime /usr/local/bin/faketime

简单验证,更多查看git文档

只需要添加如下环境变量即可:

  • LD_PRELOAD
    用于动态库的加载,动态库加载的优先级最高,一般情况下,其加载顺序为 LD_PRELOAD>LD_LIBRARY_PATH>/etc/ld.so.cache>/lib>/usr/lib
  • FAKETIME
    用来指定时间, @ 表示start at,时间会一直推进,不加@时间会一直保持不变,
  • FAKETIME_DONT_RESET
    新起进程时间也会跟着一起推进
  • FAKETIME_NO_CACHE=1
    禁用缓存,防止由于程序已经运行时修改假时间需要10秒后才会应用
/ # export LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME="@2030-08-12 10:30:33" FAKETIME_DONT_RESET=1 FAKETIME_NO_CACHE=1
/ # /bin/sh -c "while true;do date; sleep 1;done"

Mon Aug 12 10:30:33 UTC 2030
Mon Aug 12 10:30:34 UTC 2030
Mon Aug 12 10:30:35 UTC 2030
Mon Aug 12 10:30:36 UTC 2030
Mon Aug 12 10:30:37 UTC 2030
Mon Aug 12 10:30:38 UTC 2030

yaml文件部分

            #时间修改
            - name: "LD_PRELOAD"
              value: "/usr/local/lib/faketime/libfaketime.so.1"
            - name: "FAKETIME_DONT_RESET"
              value: "1"
            - name: "FAKETIME_NO_CACHE"
              value: "1"
            - name: "FAKETIME"
              value: "@2030-08-12 10:30:33"