Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes run shell command in deployment container

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?

like image 984
ph_anvin Avatar asked Sep 28 '26 16:09

ph_anvin


1 Answers

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.

like image 166
Fritz Duchardt Avatar answered Oct 01 '26 07:10

Fritz Duchardt