需求:

搞个镜像,可以运行java -jar xxx.jar包,xxx.jar包名称要用参数传

  • 思路1: 打对应运行jar包的jdk的image.
  • 思路2: 打通用jdk

1, 运行指定jar的指定版的jdk

  • k8s运行该image遇到的问题

    • kubectl create -f sms.yaml时报
  rpc error: code = 2 desc = failed to start container "cffbbc3d295f7b5a8d497c8147f7222636b51647387cda491a89d292437c7e47": Error response from daemon: {"message":"invalid header field value \"oci runtime error: container_linux.go:247: starting container process caused \\\"exec: \\\\\\\"/tmp/sms-xx.jar\\\\\\\": permission denied\\\"\\n\""} 
  • 等了一会pod奔溃了,报错
  failed to open log file "/var/log/pods/6533426e-aeec-11e7-b1c6-025622f1d9fa/sms-test_3.log": open /var/log/pods/6533426e-aeec-11e7-b1c6-025622f1d9fa/sms-test_3.log: no such file or directory 

gg了好一阵,没发现方法解决

  • 这是我的yaml
sms.yaml

apiVersion: v1
kind: Pod
metadata:
  name: sms-test
  labels:
    app: sms-test
spec:
  containers:    
  - name: sms-test
    image: sms
    imagePullPolicy: IfNotPresent
    command: ["/tmp/sms-xxx.jar"]
    volumeMounts:
    - mountPath: /tmp
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /tmp

报错问题见上!


  • 但是我这样启镜像是正常的
#vm的tmp下放xxx.jar,挂到容器里
docker run -v /tmp:/tmp -itd sms '/tmp/sms-xxx.jar'

我一般处理容器化业务思路:

0,物理vm先打通该服务. 1. 打docker镜像.docker run先跑起来. 2.写yaml改造成k8s

  • 我安装思路1定义运行jar包的jdk:的dockerfile
Dockerfile


FROM airdock/base:jessie

RUN mkdir -p /srv/java/
# Add java dynamic memory script
COPY java-dynamic-memory-opts /srv/java/

# Install Oracle JDK 8u25
RUN cd /tmp && \
    curl -L -O "http://xxx/jdk-8u25-linux-x64.gz" && \
    tar xf jdk-8u25-linux-x64.gz -C /srv/java && \
    rm -f jdk-8u25-linux-x64.gz && \
    ln -s /srv/java/jdk* /srv/java/jdk && \
    ln -s /srv/java/jdk /srv/java/jvm && \
    chown -R java:java /srv/java && \
    /root/post-install

# Define commonly used JAVA_HOME variable
# Add /srv/java and jdk on PATH variable
ENV JAVA_HOME=/srv/java/jdk \
    PATH=${PATH}:/srv/java/jdk/bin:/srv/java

COPY docker-entrypoint.sh /bin/

ENTRYPOINT ["docker-entrypoint.sh"]
docker-entrypoint.sh

#!/bin/bash
java -jar $1

嗯哼? 没毛病.

解决k8s运行定制jdk环境的问题:

方法: yaml里command换args指令即可.

sms.yaml

...
spec:
  containers:    
  - name: sms-test
    image: sms
    imagePullPolicy: IfNotPresent
    args: ["/tmp/sms-xxx.jar"]
...
  • 剖析
docker run -v /tmp:/tmp -itd sms '/tmp/sms-xxx.jar'
                                                         这里args,而非commands

2, 使jdk环境通用化

想想为了运行一个jar包,定义个运行jar的jdk环境,有点得不偿失.思路:为了通用性,搞个指定版本jdk image,管他运行什么呢.

Dockerfile

FROM airdock/base:jessie

RUN mkdir -p /srv/java/
# Add java dynamic memory script
COPY java-dynamic-memory-opts /srv/java/

# timezone 这里把时区改掉
COPY localtime /etc/localtime
# Install Oracle JDK 8u25
RUN cd /tmp && \
    curl -L -O "http://xxx/jdk-8u25-linux-x64.gz" && \
    tar xf jdk-8u25-linux-x64.gz -C /srv/java && \
    rm -f jdk-8u25-linux-x64.gz && \
    ln -s /srv/java/jdk* /srv/java/jdk && \
    ln -s /srv/java/jdk /srv/java/jvm && \
    chown -R java:java /srv/java && \
    /root/post-install

# Define commonly used JAVA_HOME variable
# Add /srv/java and jdk on PATH variable
ENV JAVA_HOME=/srv/java/jdk \
    PATH=${PATH}:/srv/java/jdk/bin:/srv/java
apiVersion: v1
kind: Pod
metadata:
  name: sms-test
  labels:
    app: sms-test
spec:
  containers:    
  - name: sms-test
    image: jdk8u25-ori
    imagePullPolicy: IfNotPresent
    command: ["java","-jar","/tmp/sms-xxx.jar"]
    volumeMounts:
    - mountPath: /tmp
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /data

经过观察jar包运行良好.
可见理解k8s yaml指令还是有点必要的.不然天天闲的蛋疼,没事干. 人生最大的敌人是无聊.

彻底理解entrypiont-命令行揉搓挤捏

指定entrypiont

  • 错误的姿势
 docker run -itd -v /tmp/:/tmp/ jdk-ori 'java -jar /tmp/sms.jar'
  • 正确的姿势1
 docker run -itd -v /tmp/:/tmp/ jdk-ori java -jar '/tmp/sms.jar'
  • 正确姿势2:
docker run -it -itd -v /tmp/:/tmp/ --entrypoint /srv/java/jdk/bin/java jdk-ori -jar /tmp/sms.jar


 --entrypoint "/srv/java/jdk/bin/java -jar"  这样是不支持的, 这个传参方式不能加参数 ,而dockerfile里则可以
  • 正确姿势3: 挂脚本方式
$ cat /tmp/entry.sh
#!/bin/bash
java -jar $1

docker run -it --rm -v /tmp/:/tmp/ --entrypoint "/tmp/entry.sh" jdk-ori /tmp/sms.jar

也可以指定这些:
https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime

CMD (Default Command or Options)
ENTRYPOINT (Default Command to Execute at Runtime)
EXPOSE (Incoming Ports)
ENV (Environment Variables)
HEALTHCHECK
VOLUME (Shared Filesystems)
USER
WORKDIR

如何在k8s里指定docker run -w 的workdir

思路: 可以通过env方式

[root@k8s-master01 ma]# cat centos.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: my-centos
  labels:
    app: centos
spec:
  containers:
  - name: my-centos
    image: centos:6.8
    imagePullPolicy: IfNotPresent
    command: ["top","-b"]
    env:
      - name: PWD
        value: "/tmp"

一个计时的pod

apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
  - name: count
    image: busybox
    args: [/bin/sh, -c,
            'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']

参考: https://www.ibm.com/developerworks/community/blogs/132cfa78-44b0-4376-85d0-d3096cd30d3f/entry/RUN_vs_CMD_vs_ENTRYPOINT_%E6%AF%8F%E5%A4%A95%E5%88%86%E9%92%9F%E7%8E%A9%E8%BD%AC_Docker_%E5%AE%B9%E5%99%A8%E6%8A%80%E6%9C%AF_17?lang=en

https://k8smeetup.github.io/docs/concepts/cluster-administration/logging/

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐