Image Pulling issue in Kubernetes deployment from Dockerhub registry

10/30/2019

I am currently trying to implement the CI/CD pipeline using docker , Kubernetes and Jenkins. When I created the pipeline deployment Kubernetes deployment YAML file, I was not included the time stamp. Only I was using the imagePullPolicy as latest in YAML file. Regarding with latest pull I had already one discussion here, The following is the link for that discussion,

Docker image not pulling latest from dockerhub.com registry

After This discussion , I included the time stamp in my deployment YAML like the following,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-kube-deployment
  labels:
   app: test-kube-deployment
spec:
 replicas: 3
 selector:
  matchLabels:
    app: test-kube-deployment
 template:
  metadata:
    labels:
      app: test-kube-deployment
    annotations: 
     date: "+%H:%M:%S %d/%m/%y"
  spec:
    imagePullSecrets:
      - name: "regcred"
    containers:
     - name: test-kube-deployment-container
       image: spacestudymilletech010/spacestudykubernetes:latest
       imagePullPolicy: Always
       ports:
         - name: http
           containerPort: 8085
           protocol: TCP

Here I modified my script to include the time stamp by adding the following in template,

annotations: 
    date: "+%H:%M:%S %d/%m/%y"

My service file like following,

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  ports:
   - port: 8085
     targetPort: 8085
     protocol: TCP
     name: http
 selector:
    app: test-kube-deployment

My jenkinsfile conatining the following,

stage ('imagebuild')
            {
                steps
                    {                               
 sh 'docker build -f /var/lib/jenkins/workspace/jpipeline/pipeline/Dockerfile -t spacestudymilletech010/spacestudykubernetes:latest /var/lib/jenkins/workspace/jpipeline/pipeline'
 sh 'docker login --username=<my-username> --password=<my-password>' 
 sh 'docker push spacestudymilletech010/spacestudykubernetes:latest'
                    }
            }

  stage ('Test Deployment')
            {
                 steps
                    {
                        sh 'kubectl apply -f deployment/testdeployment.yaml'
                        sh 'kubectl apply -f deployment/testservice.yaml'
                    }
            }

But still the deployment not pulling the latest one from Dockerhub registry. How I can modify these script for resolving the latest pulling problem?

-- Jacob
docker
dockerhub
kubernetes

2 Answers

11/15/2019

There is many articles and docs that explain how to properly build and publish the docker image using Jenkins. You should first read Using Docker with Pipeline which shows you an example with environment variable ${env.BUILD_ID}

node {
   checkout scm

   docker.withRegistry('https://registry.example.com', 'credentials-id') {

       def customImage = docker.build("my-image:${env.BUILD_ID}")

       /* Push the container to the custom Registry */
       customImage.push()
   }
}

Or to put it as a stage:

    stage('Push image') {
        docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
            app.push("${env.BUILD_NUMBER}")
            app.push("latest")
        }
    }

I really do recommend reading Building your first Docker image with Jenkins 2: Guide for developers, which I think will answer many if not all of your questions.

-- Crou
Source: StackOverflow

10/30/2019
The default pull policy is IfNotPresent which causes the Kubelet to skip pulling an image if it already exists. If you would like to always force a pull, you can do one of the following:

set the imagePullPolicy of the container to Always.

omit the imagePullPolicy and use :latest as the tag for the image to use.

omit the imagePullPolicy and the tag for the image to use.

enable the AlwaysPullImages admission controller.

Basically, either use :latest or then use imagePullPolicy: Always

Try it and let me know how it goes!

Referenced from here

-- DuDoff
Source: StackOverflow