如何在Kubernetes中使用本地docker镜像?

时间:2018-06-03 22:33:15

标签: docker kubernetes

我有这个基本的Dockerfile:

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

在我的Kubernetes集群的主节点中,我构建了该图像:

docker build -t cnginx:v1 . 

docker images表示图片已正确生成:

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

我使用此部署引用此自定义映像:

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

但是找不到图像:

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

描述pod信息:

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

我也尝试过使用默认的imagePullPolicy,还有一些其他的东西,例如用最新的标记图像......

那么,我怎样才能让Kubernetes使用本地生成的docker镜像?

1 个答案:

答案 0 :(得分:3)

您的POD安排在您的工作节点上。由于您将imagePullPolicy设置为never,因此您需要将图像提供给两个节点。换句话说,您需要像在主服务器上一样在两个节点上构建它。

作为旁注,如果您设置自定义docker注册表并将图像推送到那里,那么从长远来看可能会更容易。