0

Let's say I have a Pod with 2 containers: App and Database. I want to run a Pod that executes a command in App and then terminates.

I have set up my App container to run that command, and then it succesully runs and terminates which is great. But now my Database container is still running, so the Pod is not marked as complete.

How can I get the Pod to be marked as complete when the App container is completed?

1 Answer 1

1

You can make a call to the Kubernetes API server to accomplish this. Consider the following example:

---
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-completion
spec:
  containers:
    - name: long-running-process
      image: fbgrecojr/office-hours:so-47848488
      command: ["sleep", "1000"]
    - name: short-running-process
      image: fbgrecojr/office-hours:so-47848488
      command: ["sleep", "1"]
      lifecycle:
        preStop:
          exec:
            command: ["/pre-stop.sh"]

pre-stop.sh

#!/bin/bash
curl \
  -X DELETE \
  -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
  --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
  https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)/pods/$HOSTNAME

Dockerfile for fbgrecojr/office-hours:so-47848488

FROM centos:latest
COPY pre-stop.sh /
RUN chmod +x /pre-stop.sh

NOTE: I was not able to properly test this because preStop hooks do not seem to be working for my local Minikube setup. In case this issue is not localized to me, the corresponding issue can be tracked here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.