英文:
Kubernetes pod status running and going back to completed state
问题
-
我正在 EKS 集群内运行我的 POC Java 应用程序。以下是我迁移应用程序到容器化应用程序的步骤。
-
将 Java 应用程序源代码检出到我的本地工作站。
-
执行 Maven 命令生成包:mvn package -DskipTests
-
通常情况下,mvn package 命令会生成包,并将它们存储在目标文件夹中。我可以看到所有依赖项,包括用于运行测试用例的 Selenium JAR 文件。
-
使用 Docker 构建命令,我创建了一个 Docker 镜像:docker build -t myapp .
-
然后,我使用以下命令进入 Docker 容器,我可以看到我的测试用例成功执行,没有任何问题。
FROM openjdk:8u191-jre-alpine
# Workspace
WORKDIR /usr/share/selenium_docker
# ADD jar files and any other dependencies from HOST
ADD target/selenium-docker.jar selenium-docker.jar
ADD target/selenium-docker-tests.jar selenium-docker-tests.jar
ADD target/libs libs
# add TestNG suite files
ADD duck_search_tests.xml duck_search_tests.xml
ADD saucedemo_tests.xml saucedemo_tests.xml
# run tests using provided browser/hub address/test suite module
ENTRYPOINT java -cp selenium-docker.jar:selenium-docker-tests.jar:libs/* -DBROWSER=$BROWSER -DHUB_HOST=$HUB_HOST org.testng.TestNG $MODULE
docker run -e HUB_HOST=192.168.1.1 -e MODULE=saucedemo_tests.xml -v /c/Users/ /workspace/delete/JavaSeleniumDocker/output:/usr/share/selenium_docker/test-output localhost/myapp
Jul 17, 2023 4:41:44 AM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Jul 17, 2023 4:41:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Logging in to Sauce Demo site...
Logged In!
Home page loaded with page header: Products
Added item to cart
Price of item is: $29.99
Logging in to Sauce Demo site...
Logged In!
===============================================
check_price
Total tests run: 2, Failures: 0, Skips: 0
===============================================
接下来,我将我的 Docker 镜像移到 Docker Hub 注册表,并尝试部署该镜像到 EKS 集群。当我进行部署时,我可以看到 Pod 正在运行,并且我运行 kubectl logs -f pod 命令以验证测试用例执行。我能够看到与上面相同的消息,如总共运行的测试用例数:2,失败:0,跳过:0,但问题是每次测试用例执行完成后 Pod 都会重新启动,状态不断变为 CrashLoopBackOff 并一直在运行。
这里是 K8s YAML 文件:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app-deployment-deployment
namespace: devops-java-selenium
labels:
app: sample-app-deployment
name: sample-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: sample-app-deployment
template:
metadata:
labels:
app: sample-app-deployment
name: sample-app-deployment
spec:
containers:
- name: sample-app-deployment
image: myapp
env:
- name: HUB_HOST
value: "10.1.2.3"
- name: BROWSER
value: chrome
- name: MODULE
value:
- name: selenium_grid_host
value: "4444"
volumeMounts:
- mountPath: /usr/share/selenium/test-output
name: devops-caas-java-selenium-pv
volumes:
- name: devops-caas-java-selenium-pv
hostPath:
path: /tmp/palani
请问是否有人可以帮助我解决这个问题?
英文:
I’m running my POC java application inside the EKS cluster. Here the steps I followed to move my application into containerized application.
- Java application source code is checkout into my local workstation.
- Executed the maven command to generate the package mvn package -DskipTests
- As usual mvn package command will generate the packages inside the target folder I can see all the dependencies including jar file which has selenium jar to run the test cases.
- Using docker build command I have created a docker image out of the source code docker build -t myapp .
- Then I have entered into docker container using below command I can see my test cases are executed successfully without any issue.
dockerfile
FROM openjdk:8u191-jre-alpine
# Workspace
WORKDIR /usr/share/selenium_docker
# ADD jar files and any other dependencies from HOST
ADD target/selenium-docker.jar selenium-docker.jar
ADD target/selenium-docker-tests.jar selenium-docker-tests.jar
ADD target/libs libs
# add TestNG suite files
ADD duck_search_tests.xml duck_search_tests.xml
ADD saucedemo_tests.xml saucedemo_tests.xml
# run tests using provided browser/hub address/test suite module
ENTRYPOINT java -cp selenium-docker.jar:selenium-docker-tests.jar:libs/* -DBROWSER=$BROWSER -DHUB_HOST=$HUB_HOST org.testng.TestNG $MODULE
docker run -e HUB_HOST=192.168.1.1 -e MODULE=saucedemo_tests.xml -v /c/Users/ /workspace/delete/JavaSeleniumDocker/output:/usr/share/selenium_docker/test-output localhost/myapp
Jul 17, 2023 4:41:44 AM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Jul 17, 2023 4:41:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Logging in to Sauce Demo site...
Logged In!
Home page loaded with page header: Products
Added item to cart
Price of item is: $29.99
Logging in to Sauce Demo site...
Logged In!
===============================================
check_price
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Next i'm moving my docker image into docker hub registry and i'm trying to deploy the image inside the EKS cluster, when i'm doing deployment i can see the pod is up and running and also i run the kubectl logs -f pod command to verify the test case execution i can able to see same like above message like Total tests run: 2, Failures: 0, Skips: 0, but problem is pod is restarting every time when the test case execution completed, its again and again pod status is going to CrashLoopBackOff and running .....
here the K8s yaml file
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app-deployment-deployment
namespace: devops-java-selenium
labels:
app: sample-app-deployment
name: sample-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: sample-app-deployment
template:
metadata:
labels:
app: sample-app-deployment
name: sample-app-deployment
spec:
containers:
- name: sample-app-deployment
image: myapp
env:
- name: HUB_HOST
value: "10.1.2.3"
- name: BROWSER
value: chrome
- name: MODULE
value:
- name: selenium_grid_host
value: "4444"
volumeMounts:
- mountPath: /usr/share/selenium/test-output
name: devops-caas-java-selenium-pv
volumes:
- name: devops-caas-java-selenium-pv
hostPath:
path: /tmp/palani
Can you please someone help me on this.
答案1
得分: 1
这就是部署的性质。当Pod完成其任务时,Pod就完成了。但是,部署的目的是保持一定数量的Pod副本,因此它会一次又一次地重新创建Pod以实现其目的。这导致了您的CrashLoopBackOff。我认为也许您应该在您的特定用例中使用一个带有restartPolicy: OnFailure的Kubernetes任务。
英文:
That is the nature of a Deployment. When The pod finishes its task, the pod completes. But the Deployment has the purpose to keep up a certain number of pods (replicas) so it will recreate the pod again and again to fullfill its purpose. This is leading to your CrashLoopBackOff. I think maybe you should use a kubernetes job with the restartPolicy: OnFailure for your specific use case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论