sharif9876
sharif9876

Reputation: 680

Kubernetes - Mark Pod completed when container completes

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?

Upvotes: 0

Views: 1077

Answers (1)

frankgreco
frankgreco

Reputation: 1526

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.

Upvotes: 1

Related Questions