<p>I have a k8s deployment - I often deploy a new version to the docker repo - change the image tag - and try to replace the deployment using <code>kubectl replace -f file.yaml</code>. My replicas are set to 1 - I have only 1 pod of the deployment running at a time.</p>

<p>When I change the image tag (e.g changing v1 to v2) and try to replace it - it creates a new pod, but it remains in the 'pending' state indefinitely, while the old pod stays in 'Running' state.</p>

<p>I think the new pod waits for the old pod to be terminated - but it won't terminate by itself. I need it to be deleted by k8s so the new pod can take its place.</p>

<p>Using <code>replace --force</code> fixes this issue - but I'd like it to work using just <code>replace -f</code>. Any ideas how to achieve this?</p>

<p>The issue you see has nothing to do with kubectl replace/apply. The real reason is that deployments by default use RollingUpdate strategy which by default waits for new pod to be Running and only then kills old pod. The reason why new pod is in Pending state is unclear from your question but in most cases this indicates lack of compute resources for new pod.</p>

<p>You may do two different things: </p>

<p>Use RollingUpdate strategy with maxUnavailable=1. This will do what you want - it will kill old pod and then create a new one.</p>

<pre><code>spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
       maxUnavailable: 1
</code></pre>

<p>OR you can specify Recreate strategy which effectively does the same:</p>

<pre><code>spec:
  strategy:
    type: Recreate
</code></pre>

<p>Read more about deployment strategies here: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy</a></p>
