<p>I'm running a deployment in Kubernetes with a ConfigMap, including a set of configuration commands as a script. It works as expected when there's only 1 replica.</p>
<p>Is there a way where I can create a second pod that executes a different set of commands from the first pod?</p>
<p>For example when the first pod is being created, it will execute <code>script-1.sh</code>. But I want to execute a different script (maybe like <code>script-2.sh</code>) in the second pod and so on. Only for the first time pod I need to execute <code>script-1.sh</code>. For the next pods which are created, it should execute <code>script-2.sh</code> not <code>script-1.sh</code>.</p>
<p>Is there a way I can do this?</p>
<pre class="lang-yaml prettyprint-override"><code>volumeMounts:
  - name: config-script
    mountPath: /scripts
volumes:
  - name: config-script
    configMap:
      name: config-script-1
      defaultMode: 0744
</code></pre>

Dusty
<p>Without the specification on what exactly your scripts are doing, it could be hard to point you to the most viable and correct solution.</p>
<p>As stated previously in my comment, I do agree with answer provided by Krishna Chaurasia. It's not intended use to run a <code>Deployment</code> when each of the replicas has some differences from one another.</p>
<p>By not taking into consideration:</p>
<ul>
<li>What exactly this scripts are doing.</li>
<li>The amount of replicas in each <code>Deployment</code>.</li>
<li>If/how they are connected to each other.</li>
<li>etc.</li>
</ul>
<p>You can create a Helm chart that would iterate over certain values and produce either:</p>
<ul>
<li><code>X</code> amount of <code>Pods</code> that are running with their desired scripts.</li>
<li><code>X</code> amounts of <code>Deployments</code>/<code>StatefulSets</code> (<strong>with single <code>replica</code></strong> each) that are running with their desired scripts.</li>
</ul>
<hr />
<h3><strong>Please do consider this as a workaround of how it can be done, not acknowledging the use case!</strong></h3>
<p>With following setup of a <code>Helm</code> Chart:</p>
<pre class="lang-sh prettyprint-override"><code>test-directory: 
├── Chart.yaml
├── templates
│   ├── configmap.yaml
│   └── deployment.yaml
└── values.yaml

1 directory, 4 files
</code></pre>
<p>Where the contents of the following files are:</p>
<ul>
<li><code>Chart.yaml</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v2
name: ubuntu-scripts
description: A Helm chart for Kubernetes
version: 0.1.0
</code></pre>
<ul>
<li><code>values.yaml</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>scripts: 
  - script-one # see the configmap, what it does
  - script-two # see the configmap, what it does
</code></pre>
<ul>
<li><code>configmap.yaml</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: scripts-configmap
data:
  script-one: |
    echo &quot;Hello there!&quot; 
  script-two: |
    echo &quot;General Kenobi!&quot; 
</code></pre>
<ul>
<li><code>deployment.yaml</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>{{- range .Values.scripts }}
apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-{{ . }}
  labels:
    app: ubuntu-{{ . }} 
spec:
  restartPolicy: Never
  containers:
  - name: ubuntu
    image: ubuntu 
    imagePullPolicy: Always
    command: 
    - sleep 
    - infinity
    volumeMounts:
    - name: placeholder
      mountPath: /scripts
  volumes:
  - name: placeholder
    configMap:
      name: scripts-configmap
      items:
      - key: {{ . }}
        path: script.sh 
      defaultMode: 0744
---
{{- end }} 
</code></pre>
<p>After applying above resources with:</p>
<ul>
<li><code>helm install NAME . </code> ( being in the <code>Helm</code> Chart directory)</li>
</ul>
<blockquote>
<p>A side note!</p>
<p><code>Helm</code> will iterate over the <code>Values.scripts</code> (included in <code>values.yaml</code>) running a loop for each of the items in it. It will also parse the value that is been iterating over in place of <code>{{ . }}</code>.</p>
<p>You can see how it will behave by running:</p>
<ul>
<li><code>helm install ubuntu . --dry-run --debug</code></li>
</ul>
</blockquote>
<p>You will see:</p>
<pre class="lang-sh prettyprint-override"><code>NAME                READY   STATUS    RESTARTS   AGE
ubuntu-script-one   1/1     Running   0          4m12s
ubuntu-script-two   1/1     Running   0          4m12s
</code></pre>
<p>And checking the mounted scripts:</p>
<ul>
<li><code>kubectl exec -it ubuntu-script-one -- sh /scripts/script.sh</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>Hello there!
</code></pre>
<ul>
<li><code>kubectl exec -it ubuntu-script-two -- sh /scripts/script.sh</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>General Kenobi!
</code></pre>
<blockquote>
<p>Disclaimer!</p>
<p>You can also substitute the <code>Pod</code> for <code>Deployment</code>/<code>StatefulSet</code> (adding additional fields) but only with: <code>replicas: 1</code>.</p>
</blockquote>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">Kubernetes.io: Docs: Deployment</a></em></li>
<li><em><a href="https://helm.sh/" rel="nofollow noreferrer">Helm.sh</a></em></li>
</ul>
