How to define a readiness probe on a container for checking another containers health in the same pod?

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

How to define a readiness probe on a container for checking another containers health in the same pod?

问题

我在OpenShift中有一个包含Container A和Container B的部署对象。在这里的要求是,每当Container B退出时,Container A应该跟随,否则整个Pod应该被替换。

是否有办法实现这个要求?

这是我的YAML文件。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: app-redis-bundle
  5. labels:
  6. app: app-redis-bundle
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. app: app-redis-bundle
  12. template:
  13. metadata:
  14. labels:
  15. app: app-redis-bundle
  16. spec:
  17. containers:
  18. - name: cache-store-2
  19. image: redis
  20. resources:
  21. limits:
  22. cpu: 500m
  23. memory: 1500Mi
  24. requests:
  25. cpu: 250m
  26. memory: 1Gi
  27. ports:
  28. - containerPort: 6379
  29. livenessProbe:
  30. tcpSocket:
  31. port: 6379
  32. - name: app-server-2
  33. image: 'app:latest'
  34. resources:
  35. limits:
  36. cpu: '1'
  37. memory: 1Gi
  38. requests:
  39. cpu: 500m
  40. memory: 512Mi
  41. ports:
  42. - containerPort: 8443
  43. livenessProbe:
  44. tcpSocket:
  45. port: 8443
  46. imagePullSecrets:
  47. - name: mysecret
英文:

I have a Deployment object with Container A and Container B in OpenShift.
The requirement here is that whenever container B exits, container A should follow or otherwise the entire pod should be replaced.

Is there any way to achieve this?

This is my YAML.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: app-redis-bundle
  5. labels:
  6. app: app-redis-bundle
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. app: app-redis-bundle
  12. template:
  13. metadata:
  14. labels:
  15. app: app-redis-bundle
  16. spec:
  17. containers:
  18. - name: cache-store-2
  19. image: redis
  20. resources:
  21. limits:
  22. cpu: 500m
  23. memory: 1500Mi
  24. requests:
  25. cpu: 250m
  26. memory: 1Gi
  27. ports:
  28. - containerPort: 6379
  29. livenessProbe:
  30. tcpSocket:
  31. port: 6379
  32. - name: app-server-2
  33. image: 'app:latest'
  34. resources:
  35. limits:
  36. cpu: '1'
  37. memory: 1Gi
  38. requests:
  39. cpu: 500m
  40. memory: 512Mi
  41. ports:
  42. - containerPort: 8443
  43. livenessProbe:
  44. tcpSocket:
  45. port: 8443
  46. imagePullSecrets:
  47. - name: mysecret

答案1

得分: 1

感谢您的评论。

问题现已解决。

请允许我详细说明并解释解决方案。

问题描述

我需要在OpenShift中部署一个Pod,其中包含两个容器A和B,使得当容器A终止时,容器B也应自动终止。

最初,我的方法是从容器A中运行一个终止脚本来终止容器B。然而,这个方法没有起作用。

解决方案

使用一个活跃探针在容器B中发送从B到A的运行端口的TCP Socket连接就解决了问题,现在只要A退出,B就会立即终止。

任务完成。

以下是可用的YAML配置。

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. labels:
  5. test: liveness
  6. name: live
  7. spec:
  8. restartPolicy: Never
  9. containers:
  10. - name: nginx
  11. image: nginx:latest
  12. args:
  13. - /bin/sh
  14. - -c
  15. - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600;
  16. livenessProbe:
  17. exec:
  18. command:
  19. - cat
  20. - /tmp/healthy
  21. initialDelaySeconds: 5
  22. periodSeconds: 5
  23. - name: liveness
  24. image: registry.k8s.io/busybox
  25. ports:
  26. - containerPort: 80
  27. livenessProbe:
  28. tcpSocket:
  29. port: 80
  30. initialDelaySeconds: 5
  31. periodSeconds: 5
英文:

Thanks for your comments.

The issue is resolved now.

Allow me to elaborate it and explain the solution.

Problem Statement

I was needed to deploy a Pod in OpenShift with 2 containers A and B in a manner such that whenever container A terminates, container B should automatically kill itself as well.

Initially, my approach was to run a termination script in container B from container A. However, this approach didn't help.

Solution

Using a liveness probe to send a TCP Socket connection from B to A on the running port did the magic and now B is terminating as soon as A exits.

Job done.

Below is the working yaml.

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. labels:
  5. test: liveness
  6. name: live
  7. spec:
  8. restartPolicy: Never
  9. containers:
  10. - name: nginx
  11. image: nginx:latest
  12. args:
  13. - /bin/sh
  14. - -c
  15. - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600;
  16. livenessProbe:
  17. exec:
  18. command:
  19. - cat
  20. - /tmp/healthy
  21. initialDelaySeconds: 5
  22. periodSeconds: 5
  23. - name: liveness
  24. image: registry.k8s.io/busybox
  25. ports:
  26. - containerPort: 80
  27. livenessProbe:
  28. tcpSocket:
  29. port: 80
  30. initialDelaySeconds: 5
  31. periodSeconds: 5

答案2

得分: 0

both containers use the same ephemeral network, which means container A can communicate with container B on "localhost:port".

This multi-container pod is explained here in a clear way.

英文:

both containers use the same ephemeral network, which means container A can communicate with container B on "localhost:port".

This multi-container pod is explained here in a clear way.

huangapple
  • 本文由 发表于 2023年3月15日 19:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744096.html
匿名

发表评论

匿名网友

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

确定