<p>I have a service which runs in apache. The container status is showing as completed and restarting. Why container is not maintaining its state as running even though the arguments passed does not have issues?</p>

<pre><code>apiVersion: apps/v1
kind: Deployment

metadata:
  name: ***
spec:
  selector:
    matchLabels:
      app: ***
  replicas: 1
  template:
    metadata:
      labels:
        app: ***
    spec:
      containers:
      - name: ***
        image: ****
        command: ["/bin/sh", "-c"]
        args: ["echo\ sid\ |\ sudo\ -S\ service\ mysql\ start\ &amp;&amp;\ sudo\ service\ apache2\ start"]
        volumeMounts:
        - mountPath: /var/log/apache2/
          name: apache
        - mountPath: /var/log/***/
          name: ***
      imagePullSecrets:
      - name: regcred
      volumes:
      - name: apache
        hostPath:
          path: "/home/sandeep/logs/apache"
      - name: vusmartmaps
        hostPath:
          path: "/home/sandeep/logs/***"
</code></pre>

<p>Soon after executing this arguments it is showing its status as completed and going to a loop. What we can do to maintain it status as running.</p>

<p><strong>Please be advised this is not a good practice.</strong></p>

<p>If you really want this working that way your last process must not end.
For example add <code>sleep 9999</code> to your <code>container.args</code></p>

<p><strong>Best options would be splitting those into 2 separate <code>Deployments</code>.</strong></p>

<p>First, would be easy to scale them independently.</p>

<p>Second, image would be smaller for each <code>Deployment</code>.</p>

<p>Third, Kubernetes would have a full control over those <code>Deployments</code> and you could utilize <a href="https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/#why-you-need-kubernetes-and-what-can-it-do" rel="nofollow noreferrer">self-healing</a> and <a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/update/update-intro/" rel="nofollow noreferrer">rolling-updates</a>.</p>

<p>There is a really good guide and examples on <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">Deploying WordPress and MySQL with Persistent Volumes</a>, which I think would be perfect for you.</p>

<p>But if you prefer to use just one <code>pod</code>  then you would need to split you image or using official <a href="https://hub.docker.com/" rel="nofollow noreferrer">Docker</a> images and your <code>pod</code> might look like this:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: app
  labels:
    app: test
spec:
  containers:
  - name: mysql
    image: mysql:5.6
  - name: apache
    image: httpd:alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - name: apache
      mountPath: /var/log/apache2/
  volumes:
      - name: apache
        hostPath:
          path: "/home/sandeep/logs/apache"
</code></pre>

<p>You would need to expose the <code>pod</code> using <code>Service</code>:</p>

<pre><code>$ kubectl expose pod app --type=NodePort --port=80
service "app" exposed
</code></pre>

<p>Checking what port it has:</p>

<pre><code>$ kubectl describe service app
...
NodePort:   &lt;unset&gt; 31418/TCP
...
</code></pre>

<p>Also you should read <a href="https://kubernetes.io/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/" rel="nofollow noreferrer">Communicate Between Containers in the Same Pod Using a Shared Volume</a>.</p>
