0

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
  • What do you mean by follow? – Andromeda Mar 15 '23 at 11:35
  • By follow I mean the other container should also exit and terminate the pod. – Mayank Singh Rathore Mar 15 '23 at 15:24
  • Is your issue resolved? Could you please elaborate your issue as the information provided is not sufficient – Fariya Rahmat Mar 16 '23 at 08:22
  • Hi @FariyaRahmat. Thanks for your comment. The issue is resolved now. However, allow me to elaborate it and explain the solution. I have 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. 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. – Mayank Singh Rathore Mar 16 '23 at 15:55

2 Answers2

1

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
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.

akathimi
  • 1,393
  • 11
  • Yes. But how to manage the pod status from the container? Can you please be more specific? – Mayank Singh Rathore Mar 15 '23 at 12:41
  • I explored the preStop hook for container but seems like they can only control the current container in the pod where the hook is mentioned and not in the other one. https://stackoverflow.com/questions/70007478/kubernetes-multi-container-pod-termination-process – Mayank Singh Rathore Mar 15 '23 at 16:08