<p>I'm using <a href="https://wiki.jenkins.io/display/JENKINS/Kubernetes+Continuous+Deploy+Plugin" rel="nofollow noreferrer">Kubernetes Continuous Deploy Plugin</a> to deploy and upgrade a Deployment on my Kubernetes Cluster.
I'm using pipeline and this is the Jenkinsfile:</p>

<pre><code>pipeline {
    environment {
        JOB_NAME = "${JOB_NAME}".replace("-deploy", "")
        REGISTRY = "my-docker-registry"
    }
    agent any
    stages {
        stage('Fetching kubernetes config files') {
            steps {
                git 'git_url_of_k8s_configurations'
            }
        }
        stage('Deploy on kubernetes') {
            steps {
                kubernetesDeploy(
                    kubeconfigId: 'k8s-default-namespace-config-id',
                    configs: 'deployment.yml',
                    enableConfigSubstitution: true
                )
            }
        }
    }
}
</code></pre>

<p>Deployment.yml instead is:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ${JOB_NAME}
spec:
  replicas: 1
  template:
    metadata:
      labels:
        build_number: ${BUILD_NUMBER}
        app: ${JOB_NAME}
        role: rolling-update
    spec:
      containers:
      - name: ${JOB_NAME}-container        
        image: ${REGISTRY}/${JOB_NAME}:latest
        ports:
        - containerPort: 8080
        envFrom:
            - configMapRef:
                name: postgres
      imagePullSecrets:
      - name: regcred
  strategy:
    type: RollingUpdate
</code></pre>

<p>In order to let Kubernetes understand that Deployment is changed ( so to upgrade it and pods ) I used the Jenkins build number as annotation:</p>

<pre><code>...
metadata:
  labels:
    build_number: ${BUILD_NUMBER}
...
</code></pre>

<p><strong>The problem or my misunderstanding:</strong></p>

<p>If Deployment does not exists on Kubernetes, all works good, creating one Deployment and one ReplicaSet.</p>

<p>If Deployment still exists and an upgrade is applied, Kubernetes creates a new ReplicaSet:</p>

<p><strong>Before first deploy</strong></p>

<p><a href="https://i.stack.imgur.com/8Kky8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Kky8.png" alt="before first deploy"></a></p>

<p><strong>First deploy</strong></p>

<p><a href="https://i.stack.imgur.com/LJ2V7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LJ2V7.png" alt="first deploy"></a></p>

<p><strong>Second deploy</strong></p>

<p><a href="https://i.stack.imgur.com/K3w19.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K3w19.png" alt="second deploy"></a></p>

<p><strong>Third deploy</strong></p>

<p><a href="https://i.stack.imgur.com/ci0nI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ci0nI.png" alt="enter image description here"></a></p>

<p>As you can see, each new Jenkins deploy will update corretly the deployment but creates a new ReplicaSet without removing the old one.</p>

<p>What could be the issue?</p>

<p>This is expected behavior. Every time you update a Deployment a new ReplicaSet will be created. But, old ReplicaSet will be kept so that you can roll-back to previous state in case of any problem in your updated Deployment.</p>

<p>Ref: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment" rel="noreferrer">Updating a Deployment</a></p>

<p>However, you can limit how many ReplicaSet should be kept through <code>spec.revisionHistoryLimit</code> field. Default value is 10. Ref: <a href="https://github.com/kubernetes/api/blob/b7bd5f2d334ce968edc54f5fdb2ac67ce39c56d5/apps/v1/types.go#L299" rel="noreferrer">RevisionHistoryLimit</a></p>
