<p>I have an application that uses an install token that is required for each new install (token can't be used more than once). I have an api I created that I can curl to, that will recreate my <code>secret.yaml</code> file with a new install token when I run it.</p>
<p>Is there a way to trigger the curl command to execute when I run</p>
<pre><code>kubectl scale deployment --replicas=x
</code></pre>
<p>so that the <code>secret.yaml</code> will update before it creates the new pod?</p>
<p>I know I can run a cronjob every minute, but I would like for this to be able to autoscale eventually, so it would be ideal if I could get the secret file updated just before the new pods are created from the deployment yaml.</p>

pbay12345
<p>Since @Lei Yang has already proposed a solution to this issue, I decided to provide a Community Wiki answer just for better visibility to other community members.</p>
<p>Using <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">init containers</a> is a good way to trigger the <code>curl</code> command every time a Deployment is scaled. As described in the <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">documentation</a> - init containers can contain utilities or setup scripts not present in an app image.</p>
<hr />
<p>I've create a simple example to illustrate how init containers can work with the <code>curl</code> command.</p>
<p>First, I created an <code>app-1</code> Deployment which contains an <code>init-curl</code> init container:<br />
<strong>NOTE:</strong> I used the <a href="https://hub.docker.com/r/curlimages/curl" rel="nofollow noreferrer">curlimages/curl</a> image, it's the official curl image generated by the curl docker team.</p>
<pre><code>$ cat app-1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app-1
  name: app-1
spec:
  selector:
    matchLabels:
      app: app-1
  template:
    metadata:
      labels:
        app: app-1
    spec:
      initContainers:
      - name: init-curl
        image: curlimages/curl
        command: ['sh', '-c', 'curl example.com']
      containers:
      - image: nginx
        name: nginx
</code></pre>
<p>After creating the Deployment we can check if the <code>init-curl</code> works correctly:</p>
<pre><code>$ kubectl apply -f app-1.yaml
deployment.apps/app-1 created

$ kubectl get pods | grep app-1
app-1-f6d7cdd68-hfp78                                    1/1     Running   0          12s

$ kubectl logs -f app-1-f6d7cdd68-hfp78 init-curl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Example Domain&lt;/title&gt;
...
</code></pre>
<p>Now let's try to scale the <code>app-1</code> Deployment:</p>
<pre><code>$ kubectl scale deployment app-1 --replicas=2
deployment.apps/app-1 scaled

$ kubectl get pods | grep app-1
app-1-f6d7cdd68-hfp78                                    1/1     Running   0          6m1s
app-1-f6d7cdd68-wbchx                                    1/1     Running   0          5s

$ kubectl logs -f app-1-f6d7cdd68-wbchx init-curl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   2043      0 --:--:-- --:--:-- --:--:--  2042
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Example Domain&lt;/title&gt;
... 
</code></pre>
<p>As we can see it works as expected, the <code>curl</code> command is executed every time the Deployment is scaled.</p>
