Kubernetes pod 状态正在运行,并返回到已完成状态。

huangapple go评论82阅读模式
英文:

Kubernetes pod status running and going back to completed state

问题

  1. 我正在 EKS 集群内运行我的 POC Java 应用程序。以下是我迁移应用程序到容器化应用程序的步骤。

  2. 将 Java 应用程序源代码检出到我的本地工作站。

  3. 执行 Maven 命令生成包:mvn package -DskipTests

  4. 通常情况下,mvn package 命令会生成包,并将它们存储在目标文件夹中。我可以看到所有依赖项,包括用于运行测试用例的 Selenium JAR 文件。

  5. 使用 Docker 构建命令,我创建了一个 Docker 镜像:docker build -t myapp .

  6. 然后,我使用以下命令进入 Docker 容器,我可以看到我的测试用例成功执行,没有任何问题。

  1. FROM openjdk:8u191-jre-alpine
  2. # Workspace
  3. WORKDIR /usr/share/selenium_docker
  4. # ADD jar files and any other dependencies from HOST
  5. ADD target/selenium-docker.jar selenium-docker.jar
  6. ADD target/selenium-docker-tests.jar selenium-docker-tests.jar
  7. ADD target/libs libs
  8. # add TestNG suite files
  9. ADD duck_search_tests.xml duck_search_tests.xml
  10. ADD saucedemo_tests.xml saucedemo_tests.xml
  11. # run tests using provided browser/hub address/test suite module
  12. ENTRYPOINT java -cp selenium-docker.jar:selenium-docker-tests.jar:libs/* -DBROWSER=$BROWSER -DHUB_HOST=$HUB_HOST org.testng.TestNG $MODULE
  1. 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
  2. Jul 17, 2023 4:41:44 AM org.openqa.selenium.remote.DesiredCapabilities chrome
  3. INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
  4. Jul 17, 2023 4:41:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
  5. INFO: Detected dialect: W3C
  6. Logging in to Sauce Demo site...
  7. Logged In!
  8. Home page loaded with page header: Products
  9. Added item to cart
  10. Price of item is: $29.99
  11. Logging in to Sauce Demo site...
  12. Logged In!
  13. ===============================================
  14. check_price
  15. Total tests run: 2, Failures: 0, Skips: 0
  16. ===============================================

接下来,我将我的 Docker 镜像移到 Docker Hub 注册表,并尝试部署该镜像到 EKS 集群。当我进行部署时,我可以看到 Pod 正在运行,并且我运行 kubectl logs -f pod 命令以验证测试用例执行。我能够看到与上面相同的消息,如总共运行的测试用例数:2,失败:0,跳过:0,但问题是每次测试用例执行完成后 Pod 都会重新启动,状态不断变为 CrashLoopBackOff 并一直在运行。

这里是 K8s YAML 文件:

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: sample-app-deployment-deployment
  6. namespace: devops-java-selenium
  7. labels:
  8. app: sample-app-deployment
  9. name: sample-app-deployment
  10. spec:
  11. replicas: 1
  12. selector:
  13. matchLabels:
  14. app: sample-app-deployment
  15. template:
  16. metadata:
  17. labels:
  18. app: sample-app-deployment
  19. name: sample-app-deployment
  20. spec:
  21. containers:
  22. - name: sample-app-deployment
  23. image: myapp
  24. env:
  25. - name: HUB_HOST
  26. value: "10.1.2.3"
  27. - name: BROWSER
  28. value: chrome
  29. - name: MODULE
  30. value:
  31. - name: selenium_grid_host
  32. value: "4444"
  33. volumeMounts:
  34. - mountPath: /usr/share/selenium/test-output
  35. name: devops-caas-java-selenium-pv
  36. volumes:
  37. - name: devops-caas-java-selenium-pv
  38. hostPath:
  39. 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.

  1. Java application source code is checkout into my local workstation.
  2. Executed the maven command to generate the package mvn package -DskipTests
  3. 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.
  4. Using docker build command I have created a docker image out of the source code docker build -t myapp .
  5. Then I have entered into docker container using below command I can see my test cases are executed successfully without any issue.

dockerfile

  1. FROM openjdk:8u191-jre-alpine
  2. # Workspace
  3. WORKDIR /usr/share/selenium_docker
  4. # ADD jar files and any other dependencies from HOST
  5. ADD target/selenium-docker.jar selenium-docker.jar
  6. ADD target/selenium-docker-tests.jar selenium-docker-tests.jar
  7. ADD target/libs libs
  8. # add TestNG suite files
  9. ADD duck_search_tests.xml duck_search_tests.xml
  10. ADD saucedemo_tests.xml saucedemo_tests.xml
  11. # run tests using provided browser/hub address/test suite module
  12. ENTRYPOINT java -cp selenium-docker.jar:selenium-docker-tests.jar:libs/* -DBROWSER=$BROWSER -DHUB_HOST=$HUB_HOST org.testng.TestNG $MODULE
  1. 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
  2. Jul 17, 2023 4:41:44 AM org.openqa.selenium.remote.DesiredCapabilities chrome
  3. INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
  4. Jul 17, 2023 4:41:51 AM org.openqa.selenium.remote.ProtocolHandshake createSession
  5. INFO: Detected dialect: W3C
  6. Logging in to Sauce Demo site...
  7. Logged In!
  8. Home page loaded with page header: Products
  9. Added item to cart
  10. Price of item is: $29.99
  11. Logging in to Sauce Demo site...
  12. Logged In!
  13. ===============================================
  14. check_price
  15. Total tests run: 2, Failures: 0, Skips: 0
  16. ===============================================

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

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: sample-app-deployment-deployment
  6. namespace: devops-java-selenium
  7. labels:
  8. app: sample-app-deployment
  9. name: sample-app-deployment
  10. spec:
  11. replicas: 1
  12. selector:
  13. matchLabels:
  14. app: sample-app-deployment
  15. template:
  16. metadata:
  17. labels:
  18. app: sample-app-deployment
  19. name: sample-app-deployment
  20. spec:
  21. containers:
  22. - name: sample-app-deployment
  23. image: myapp
  24. env:
  25. - name: HUB_HOST
  26. value: "10.1.2.3"
  27. - name: BROWSER
  28. value: chrome
  29. - name: MODULE
  30. value:
  31. - name: selenium_grid_host
  32. value: "4444"
  33. volumeMounts:
  34. - mountPath: /usr/share/selenium/test-output
  35. name: devops-caas-java-selenium-pv
  36. volumes:
  37. - name: devops-caas-java-selenium-pv
  38. hostPath:
  39. 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.

huangapple
  • 本文由 发表于 2023年7月17日 12:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定