<p>I have kubernetes job defined as following:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: redis-master
  namespace: sandbox
  labels:
    app: redis
spec:
  parallelism: 1
  ttlSecondsAfterFinished: 10
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: master
          image: redis
          command: [&quot;sh&quot;, &quot;-c&quot;]

          args:
          - redis-server
          - sleep 10
          - redis-cli RPUSH codes $(cat /data/codes)
          env:
            - name: MASTER
              value: &quot;true&quot;
          ports:
            - containerPort: 6379
          volumeMounts:
            - name: codes
              mountPath: /data/codes
              subPath: codes
              readOnly: true
      volumes:
        - name: codes
          configMap:
            name: codes
      restartPolicy: Never
</code></pre>
<p>when pod is up and running redis is empty:</p>
<pre><code>/deploy/base$ kubectl exec -it -n box redis-master-mbhrk -- /bin/sh
# redis-cli lrange codes 0 -1
(empty array)
#
</code></pre>
<p>this pod should kill him self when the queue codes is empty.</p>
<p>also, i get an error in pod console</p>
<pre><code>can't access tty; job control turned off
</code></pre>
<p>how can I load a codes to redis ???</p>

a11eksandar
<p>A container only runs one command.  You've set that command to <code>/bin/sh -c</code> and passed it a command string to run (<code>redis-server</code>), along with two additional parameters which would be visible inside that command string as <code>$0</code> and <code>$1</code>.</p>
<p>The first thing you need to do is split the Redis server proper into a separate container, probably managed by a StatefulSet (or perhaps a Deployment if persistence is disabled).  You might use something like a prepackaged Helm chart to simplify deploying this.  We'll assume this already exists:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: redis-server
...
</code></pre>
<p>Now your Job needs to <em>only</em> run the <code>redis-cli RPUSH</code> command.  Because of the way you use a command substitution, you do need a shell here.  However, since you're not running the Redis server here, you don't need various bits of machinery around that.</p>
<p>You could probably write the Job as:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: load-codes
  namespace: sandbox
  # labels:
  #   app: redis
spec:
  template:
    # metadata:
    #   labels:
    #     app: redis
    spec:
      containers:
        - name: loader
          image: redis
          command: 
            - /bin/sh
            - -c
            - redis-cli -h redis-server RPUSH codes $(cat /data/codes)
          volumeMounts:
            - name: codes
              mountPath: /data
              readOnly: true
      volumes:
        - name: codes
          configMap:
            name: codes
      restartPolicy: OnFailure
</code></pre>
<p>You'll probably need to set the labels so that they're not picked up by the main Service (since the pod isn't running a Redis server).  I've also set the restart policy to <code>OnFailure</code>: if the pod does fail for some reason, maybe because the Redis server isn't available yet, this will automatically restart it, but if it succeeds, the Job will be marked &quot;completed&quot;.</p>
