'setting image pull policy using kubectl

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?



Solution 1:[1]

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>

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 laxman
Solution 2