<p>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.</p>
<p>Is there any way to achieve this?</p>
<p>This is my YAML.</p>
<pre><code>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
</code></pre>

Mayank Singh Rathore
<p>Thanks for your comments.</p>
<p>The issue is resolved now.</p>
<p>Allow me to elaborate it and explain the solution.</p>
<h2>Problem Statement</h2>
<p>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.</p>
<p>Initially, my approach was to run a termination script in container B from container A. However, this approach didn't help.</p>
<h2>Solution</h2>
<p>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.</p>
<p>Job done.</p>
<p>Below is the working yaml.</p>
<pre><code>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
</code></pre>
