Kubernetes run shell command in deployment container

5/5/2020

It is possible to define commands in the Kubernetes for a Deployment that are to be executed immediately after creating the deployment?

for example:

cd opt/
wget xxxxxx
mkdir new/

I have not found any solution to this problem so far.

Is there any other method to get this effect?

-- ph_anvin
kubernetes
kubernetes-pod
yaml

1 Answer

5/5/2020

This could be done like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: example
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  strategy: {}
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - image: busybox
          command: ["/bin/sh"]
          args: ["-c", "cd opt/ &&  wget xxxxxx && mkdir new/ && process-that-keeps-container-running"]
          name: busybox

It's a bit tricky, since at the end of the command arguments, you will have to place the command that keeps the containers running. If you don't know which one this is, you will have to look at CMD and ENTRYPOINT of the Docker image you are using.

-- Fritz Duchardt
Source: StackOverflow