由于jdk8的不能取到容器中正确cpu croe数的原因,在打包的时候,使用了libsysconfcpus。dockerfile如下:

FROM alpine/git

WORKDIR /
RUN git clone https://github.com/obmarg/libsysconfcpus.git


FROM gcc:5

COPY --from=0 /libsysconfcpus /libsysconfcpus
RUN /libsysconfcpus/configure && make && make install 



FROM openjdk:8u191-alpine

COPY --from=1 /usr/local/lib/libsysconfcpus.so /usr/local/lib/

COPY cpu.sh /

RUN chmod 755 /cpu.sh

COPY testcpu.jar /

WORKDIR /
CMD [ "sh" "-c" "sh /cpu.sh"]
ENTRYPOINT [ "sh", "-c", "java -jar /testcpu.jar" ]

这里用了三个阶段,分别是git下载、gcc进行安装、打包三个过程。前两个过程不用说,一看就明白。第三个过程要解释一下:

先把装好的libsysconfcpus.so文件cp过来,后面要用。然后把一个shell脚本cp进来:

#!/bin/sh
if [ "x$CONTAINER_CORE_REQUEST" != "x" ]; then
   LIBSYSCONFCPUS="$CONTAINER_CORE_REQUEST"
   if [ ${LIBSYSCONFCPUS} -lt 2 ]; then
      LIBSYSCONFCPUS=2
   fi
   export LIBSYSCONFCPUS
fi
export LD_PRELOAD="/usr/local/lib/libsysconfcpus.so:$LD_PRELOAD"

这个脚本不多解释了,别的地方都有。

下面就是执行这个脚本,执行后再执行jar包,jar包的内容如下:

package test;

public class TestCpu {

    public static void main(String[] args) throws InterruptedException {
        while (true) {
            int p = Runtime.getRuntime().availableProcessors();
            System.out.println("availableProcessors = " + p);
            Thread.sleep(1000);
        }
    }

}

就是获取了一下CPU的核数。

发布到kubernetes试一下:

apiVersion: v1
kind: Pod
metadata:
  name: cpu-demo
  namespace: default
spec:
  containers:
  - name: cpu-demo-ctr
    image: 192.168.1.190/ircloud.com/testcpu
    resources:
      limits:
        cpu: "3"
      requests:
        cpu: "2"

Kubernetes libsysconfcpus的使用

结果没有问题,说明运行正常。

相关文章:

  • 2021-08-10
  • 2022-12-23
  • 2021-11-18
  • 2021-06-06
  • 2021-10-31
  • 2021-06-10
  • 2021-06-28
猜你喜欢
  • 2022-03-08
  • 2021-07-11
  • 2021-11-22
  • 2021-07-13
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
相关资源
相似解决方案