1. 程式人生 > >Kubernetes通過yaml配置檔案建立例項時不使用本地映象的原因

Kubernetes通過yaml配置檔案建立例項時不使用本地映象的原因

原文:https://www.58jb.com/html/154.html  

在筆記本上做實驗的時候,沒有把Docker的內網倉庫主機啟動,導致了Kubernetes在建立pod例項時失敗。狀態為ImagePullBackOff,我本機已經在上次操作時已經自動下載到有映象了,為何還提示拉取映象呢?

通過檢視日誌就可以知道問題大致是什麼。

  1. Jun 12 17:02:03 k8s ntpd[789]: Listen normally on 18 veth5737c59 fe80::c4e0:e4ff:fe31:a648 UDP 123 
  2. Jun 12 17:02:03 k8s ntpd[789]: new interface(s) found: waking up resolver 
  3. Jun 12 17:02:03 k8s dockerd-current[1164]: time="2017-06-12T17:02:03.659830673+08:00"level=warningmsg="Error getting v2 registry: Get https://reg.docker.lc/v2/: dial tcp 10.0.10.9:443: getsockopt: no route to host"
  4. Jun 12 17:02:03 k8s dockerd-current[1164]: time="2017-06-12T17:02:03.659899809+08:00"level=errormsg="Attempting next endpoint for pull after error: Get https://reg.docker.lc/v2/: dial tcp 10.0.10.9:443: getsockopt: no route to host"
  5. Jun 12 17:02:06 k8s kube-scheduler[6154]: I0612 17:02:06.544109    6154 leaderelection.go:247] lock is held by k8s-node and has not yet expired 
  6. Jun 12 17:02:06 k8s dockerd-current[1164]: time="2017-06-12T17:02:06.667426272+08:00"level=errormsg="Attempting next endpoint for pull after error: Get https://reg.docker.lc/v1/_ping: dial tcp 10.0.10.9:443: getsockopt: no route to host"
  7. Jun 12 17:02:06 k8s kubelet[1345]: E0612 17:02:06.668855    1345 docker_manager.go:2295] container start failed: ErrImagePull: image pull failed for reg.docker.lc/share/nginx:latest, this may be because there are no credentials on this request.  details: (Get https://reg.docker.lc/v1/_ping: dial tcp 10.0.10.9:443: getsockopt: no route to host) 
  8. Jun 12 17:02:06 k8s kubelet[1345]: E0612 17:02:06.668956    1345 pod_workers.go:184] Error syncing pod cb61d30a-4f4d-11e7-8ea8-000c29e9277a, skipping: failed to "StartContainer" for "nginx" with ErrImagePull: "image pull failed for reg.docker.lc/share/nginx:latest, this may be because there are no credentials on this request.  details: (Get https://reg.docker.lc/v1/_ping: dial tcp 10.0.10.9:443: getsockopt: no route to host)" 
  9. Jun 12 17:02:06 k8s kubelet[1345]: E0612 17:02:06.942588    1345 docker_manager.go:2295] container start failed: ImagePullBackOff: Back-off pulling image "reg.docker.lc/share/nginx:latest" 
  10. Jun 12 17:02:06 k8s kubelet[1345]: E0612 17:02:06.942643    1345 pod_workers.go:184] Error syncing pod cb61d30a-4f4d-11e7-8ea8-000c29e9277a, skipping: failed to "StartContainer" for "nginx" with ImagePullBackOff: "Back-off pulling image \"reg.docker.lc/share/nginx:latest\"" 

        後來對比了幾個網上下載回來的yaml配置檔案,有一個引數選項:imagePullPolicy: Always ,映象的拉取策略,總是拉取;但是我的配置檔案中並沒有新增這個選項,根據這樣可以想象到,預設就可能是Always的,於是網上搜了一下,同樣有網友遇到這樣的情況,都是會自動到遠端拉取映象,並不使用本地的映象。

那麼這個引數的可選項有哪些呢?

官方其實已經說明了,只是沒有詳細看文件;https://kubernetes.io/docs/concepts/containers/images/

By default, the kubelet will try to pull each image from the specified registry. However, if the imagePullPolicy property of the container is set to IfNotPresent or Never, then a local image is used (preferentially or exclusively, respectively).

#預設情況是會根據配置檔案中的映象地址去拉取映象,如果設定為IfNotPresent 和Never就會使用本地映象。

IfNotPresent :如果本地存在映象就優先使用本地映象。

Never:直接不再去拉取映象了,使用本地的;如果本地不存在就報異常了。

引數的作用範圍:

  1. spec: 
  2.   containers: 
  3.     - name: nginx 
  4.       image: image: reg.docker.lc/share/nginx:latest 
  5.       imagePullPolicy: IfNotPresent   #或者使用Never 

因為此引數預設為:imagePullPolicy: Always ,如果你yaml配置檔案中沒有定義那就是使用預設的。