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

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

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文件。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-redis-bundle
  labels:
    app: app-redis-bundle
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app-redis-bundle
  template:
    metadata:
      labels:
        app: app-redis-bundle
    spec:
      containers:
        - name: cache-store-2
          image: redis
          resources:
            limits:
              cpu: 500m
              memory: 1500Mi
            requests:
              cpu: 250m
              memory: 1Gi
          ports:
            - containerPort: 6379
          livenessProbe:
            tcpSocket:
              port: 6379
        
        - name: app-server-2
          image: 'app:latest'
          resources:
            limits:
              cpu: '1'
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 512Mi
          ports:
            - containerPort: 8443
          livenessProbe:
            tcpSocket:
              port: 8443
      imagePullSecrets:
        - 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.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-redis-bundle
  labels:
    app: app-redis-bundle
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app-redis-bundle
  template:
    metadata:
      labels:
        app: app-redis-bundle
    spec:
      containers:
        - name: cache-store-2
          image: redis
          resources:
            limits:
              cpu: 500m
              memory: 1500Mi
            requests:
              cpu: 250m
              memory: 1Gi
          ports:
            - containerPort: 6379
          livenessProbe:
            tcpSocket:
              port: 6379
        
        - name: app-server-2
          image: 'app:latest'
          resources:
            limits:
              cpu: '1'
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 512Mi
          ports:
            - containerPort: 8443
          livenessProbe:
            tcpSocket:
              port: 8443
      imagePullSecrets:
        - name: mysecret

答案1

得分: 1

感谢您的评论。

问题现已解决。

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

问题描述

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

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

解决方案

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

任务完成。

以下是可用的YAML配置。

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: live
spec:
  restartPolicy: Never
  containers:
  - name: nginx
    image: nginx:latest
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600;
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

  - name: liveness
    image: registry.k8s.io/busybox
    ports:
    - containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 5
      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.

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: live
spec:
  restartPolicy: Never
  containers:
  - name: nginx
    image: nginx:latest
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600;
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

  - name: liveness
    image: registry.k8s.io/busybox
    ports:
    - containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 5
      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:

确定