Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

cloud - How to pull images on kubernetes on every deployment

Hi i am trying to deploy on my gke cluster through cloud build.I am able to deploy. But every time i am pushing new images.My cluster is not picking up the new image but deploy the pod with the old image only(nothing is changed).When i am deleting my pod and triggering the cloudbuild then it is picking the new image. I have also added ImagePullPolicy= Always. Below is my cloudbuild.yaml file.

  - id: 'build your instance'
name: 'maven:3.6.0-jdk-8-slim'
entrypoint: mvn
args: ['clean','package','-Dmaven.test.skip=true']
- id: "docker build"  
name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/PID/test', '.']
name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/PID/TEST']
- id: 'Deploy image to kubernetes'
name: 'gcr.io/cloud-builders/gke-deploy'
args:
- run
- --filename=./run/helloworld/src
- --location=us-central1-c
- --cluster=cluster-2

My pod manifest looks like this.

apiVersion: v1
kind: Pod
metadata:
  name: Test
  labels:
     app: hello
spec:
  containers:
   - name: private-reg-containers
     image: gcr.io/PID/test
     imagePullPolicy: "Always"

Any help is appreciated.

question from:https://stackoverflow.com/questions/65889046/how-to-pull-images-on-kubernetes-on-every-deployment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is an expected behavior and you may be confusing the usage of imagePullPolicy: "Always". This is well explanined in this answer:

Kubernetes is not watching for a new version of the image. The image pull policy specifies how to acquire the image to run the container. Always means it will try to pull a new version each time it's starting a container. To see the update you'd need to delete the Pod (not the Deployment) - the newly created Pod will run the new image.

There is no direct way to have Kubernetes automatically update running containers with new images. This would be part of a continuous delivery system (perhaps using kubectl set image with the new sha256sum or an image tag - but not latest).

This is why when you recreate the pods, those get the newest image. So the answer to your question is to explicitly tell K8s to get the newest image. In the example I share with you I use two tags, the clasic latest which is more used to share the image with a friendly name and the tag using the $BUILD_ID which is used to update the image in GKE. In this example I update the image for a deployment so you only change it for updating an standalone pod which should be your little "homework".

steps:

#Building Image
- name: 'gcr.io/cloud-builders/docker'
  id: build-loona
  args: 
  - build
  - --tag=${_LOONA}:$BUILD_ID
  - --tag=${_LOONA}:latest
  - .
  dir: 'loona/'
  waitFor: ['-']

#Pushing image (this pushes the image with both tags)
- name: 'gcr.io/cloud-builders/docker'
  id: push-loona
  args:
  - push
  - ${_LOONA}
  waitFor:
    - build-loona

#Deploying to GKE
- name: "gcr.io/cloud-builders/gke-deploy"
  id: deploy-gke
  args:
  - run
  - --filename=k8s/
  - --location=${_COMPUTE_ZONE}
  - --cluster=${_CLUSTER_NAME}

#Update Image
- name: 'gcr.io/cloud-builders/kubectl'
  id: update-loona
  args:
  - set
  - image
  - deployment/loona-deployment
  - loona=${_LOONA}:$BUILD_ID
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_COMPUTE_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLUSTER_NAME}'
  waitFor:
    - deploy-gke

substitutions:
    _CLUSTER_NAME: my-cluster
    _COMPUTE_ZONE: us-central1
    _LOONA: gcr.io/${PROJECT_ID}/loona


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...