How can I use a local docker image in Kubernetes?

2019-07-05 05:00发布

问题:

I have this basic Dockerfile:

FROM nginx
RUN apt-get -y update && apt install -y curl

In the master node of my Kubernetes cluster I build that image:

docker build -t cnginx:v1 . 

docker images shows that the image has been correctly generated:

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
cgninx                                     v1                  d3b1b19d069e        39 minutes ago      141MB

I use this deployment referencing this custom image:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 
  template: 
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: cnginx
        image: cnginx:v1
        imagePullPolicy: Never
        ports:
        - containerPort: 80
      nodeSelector:
        nodetype: webserver

However the image is not found:

NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-7dd98bd746-lw6tp   0/1       ErrImageNeverPull   0          4s
nginx-deployment-7dd98bd746-szr9n   0/1       ErrImageNeverPull   0          4s

Describe pod info:

Events:
  Type     Reason                 Age              From                Message
  ----     ------                 ----             ----                -------
  Normal   Scheduled              1m               default-scheduler   Successfully assigned nginx-deployment-7dd98bd746-szr9n to kubenode2
  Normal   SuccessfulMountVolume  1m               kubelet, kubenode2  MountVolume.SetUp succeeded for volume "default-token-bpbpl"
  Warning  ErrImageNeverPull      9s (x9 over 1m)  kubelet, kubenode2  Container image "cnginx:v1" is not present with pull policy of Never
  Warning  Failed                 9s (x9 over 1m)  kubelet, kubenode2  Error: ErrImageNeverPull

I have also tried using the default imagePullPolicy, and some other things such as tagging the image with latest...

So, how can I make Kubernetes use a locally generated docker image?

回答1:

Your PODs are scheduled on your worker nodes. Since you set imagePullPolicy to never you need to make your image available to both nodes. In other words, you need to build it on both nodes as you did on the master.

As a sidenote, it would be probably easier in the long term if you setup a custom docker registry and push your images there.