title: update kubernetes deployment with jenkins
body: <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>