8

Following the docs and this question, I am trying to pull a image that I created locally with docker while creating deployment with kubectl. I am looking for something like this,

kubectl create deployment first-k8s-deploy --image="laxman/nodejs/express-app" --image-pull-policy="never"

Looking into kubectl create deployment --help doesn't provide any --image-pull-policy option.

Is there any global config to set imagePullPolicy and how can I set this flag for some specific deployments only?

laxman
  • 1,781
  • 4
  • 14
  • 32

2 Answers2

13

You might have gone past what can be done with the command line. See Creating a Deployment for how to specify a deployment in a yaml file.

The imagePullPolicy is part of the Container definition.

You can get the yaml for any kubectl command by adding -o yaml --dry-run to the command. Using your example deployment:

kubectl create deployment first-k8s-deploy \
  --image="laxman/nodejs/express-app" \
  -o yaml \
  --dry-run

Gives you:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: first-k8s-deploy
  name: first-k8s-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: first-k8s-deploy
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: first-k8s-deploy
    spec:
      containers:
      - image: laxman/nodejs/express-app
        name: express-app
        resources: {}

Then add the imagePullPolicy property into a container in the list:

    spec:
      containers:
      - image: laxman/nodejs/express-app
        name: express-app
        resources: {}
        imagePullPolicy: Never

The yaml file you create can then be deployed with the following command

kubectl apply -f <filename>

laxman
  • 1,781
  • 4
  • 14
  • 32
Matt
  • 68,711
  • 7
  • 155
  • 158
  • thanks @Matt, but don't you think if something that can be achieved with yaml file must also have support with the cli ? – laxman Jan 30 '20 at 07:20
  • 1
    The full data model is way too complex to represent as cli options. – Matt Jan 30 '20 at 07:32
  • you must be careful, to apply the deploment at the end. if you apply the deployment and then add the setting ,it will have a downside: first time the container is created using the old image ( imagepullpolicy off),then a new container is created,with the new image. – iliefa Apr 01 '22 at 06:11
7

It's possible to specify --image-pull-policy for a single pod using cli.

So you can create and run a pod using:

kubectl run PODNAME --image='laxman/nodejs/express-app' --image-pull-policy='never'

You can see other exampled and detailed explanation by doing kubectl run --help and documentation is available here.

Like I said this applied to pods if you add option --generator=deployment/v1beta1 it will create a Deployment. This is going to be Deprecated starting from Kubernetes 1.18 pull request was approved and merged.

Crou
  • 10,232
  • 2
  • 26
  • 31