<p>My deployment <code>yml</code> file tags my image with the build version.</p>

<p>So everytime I run <code>kubectl apply</code> from my release pipeline, it pulls the image and deploys it properly.</p>

<p>My question is about the <code>replicaset</code>: when I run <code>kubectl get all</code>, I see orphan replicasets from the pods that were terminated from the previous images. (At least, that's my understanding.) The <code>desired</code>, <code>current</code> and <code>ready</code> properties of these orphan replicasets are 0.</p>

<p>Will this lead to some sort of memory leak? Should I run any other command before <code>kubectl apply</code>?</p>

user11081980
<p>When you upgrade your deployments from version 1 to version 2, the Deployment creates a new ReplicaSet and increases the count of replicas while the previous count goes to 0. Details <a href="https://v1-17.docs.kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment_" rel="nofollow noreferrer">here</a></p>

<p>If you try to execute another rolling update from version 2 to version 3, you might notice that at the end of the upgrade, you have two ReplicaSets with a count of 0.</p>

<p><strong>How this benefit us?</strong></p>

<p>Imagine that current version of the pod introduces any problem and you might want to rollback to previous version. If you have old ReplicaSet, you could scale current to 0 and increment the old ReplicaSet count. See how  <a href="https://v1-17.docs.kubernetes.io/docs/concepts/workloads/controllers/deployment/#rolling-back-to-a-previous-revision" rel="nofollow noreferrer">Rolling Back to a Previous Revision</a>.</p>

<p>By default Kubernetes stores the last 10 ReplicaSets and lets you rollback to any of them. But you can change this by changing the <code>spec.revisionHistoryLimit</code> in your Deployment. Ref: <a href="https://v1-17.docs.kubernetes.io/docs/concepts/workloads/controllers/deployment/#clean-up-policy" rel="nofollow noreferrer">Clean up Policy</a></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  revisionHistoryLimit: 3
...
</code></pre>
