問題的前提是這樣的,我們修改了平台某一個模塊的代碼,想直接將修改放到docker鏡像中,以便我們的k8s環境可以直接使用這個鏡像來啟動容器;
步 驟:① 將需要修改的鏡像先運行起來,我們的這個鏡像會依賴平台的其他配置,無法單獨運行,可以這樣僅僅把容器運行起來,不啟動里面的服務;
docker run -d --name needModifyImage nbiot.com/needModifyImage:1.0.0 /bin/bash -c "tail -f /dev/null"
② 將需要修改的文件或者jar包,復制到已經運行起來的needModifyImage容器中(直接替換掉需要修改的文件或者jar);
docker cp needModifyImage_20210120.jar needModifyImage:/opt/nbiot/config/jar/
(切記,在將jar包copy到容器后,注意修改jar文件的操作權限,以防在運行的時候,無read權限出現報錯,chmod +x needModifyImage_20210120.jar)
③ 將修改后的needModifyImage容器,做成快照,保存到本地目錄;
docker export needModifyImage > needModifyImage_20200120.tar
④ 將快照(needModifyImage_20200120.tar)恢復成指定名字的新的鏡像;(在哪台服務器運行此命令,就會在該服務器上生成鏡像)
cat needModifyImage_20200120.tar |docker import - nbiot.com/needModifyImage:2.0.0
⑤ 將啟動k8s - pod 的配置helm chart中的鏡像版本修改成新的nbiot.com/needModifyImage:2.0.0,並啟動;
helm install -n myServiceName myServiceName/
⑥ 查看啟動的pod信息;
kubectl describe pod myServiceName
⑦ 發現啟動報錯
Warning Failed 8s (x2 over 9s) kubelet, worknode2 Error: Error response from daemon: No command specified
若無執行命令,則會報上面的錯誤,因為我們是在pod啟動的服務,沒辦法像docker那樣直接再啟動的命令行里添加可執行的指令,只能像下面的做法,將需 要執行的命令,添加在yaml文件中;
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10555
fsGroup: 10555
containers:
- name: server
image: xxxxxx-xxxxxx:xxxx
command: ["java", "-XX:MaxRAM=1825361100", "-XX:MaxRAMFraction=1", "-Djava.security.egd=file:///dev/./urandom", "-Dlog4j2.configurationFile=/staging/jars/log4j_server.xml", "-DCONFIGDIR=/appl/config", "-Dname=ServerId", "-jar", "/staging/jars/server.jar"]
imagePullPolicy: Always
command: ["java","-XX:MaxRAM=1825361100", "-XX:MaxRAMFraction=1", "-Djava.security.egd=file:///dev/./urandom", "-Dlogback.configurationFile=/staging/jars/log4j_server.xml", "-DCONFIGDIR=/appl/config", "-Dname=SerId", "-jar", "/opt/nbiot/config/jar/needModifyImage_20210120.jar"]