今天实践了k8s 对image 的拉取的实验:
--------------------------------------
还是先看一下kubectl 对镜像的管理:
k8s的配置文件中经常看到有imagePullPolicy属性,这个属性是描述镜像的拉取策略
Always 总是拉取镜像
IfNotPresent 本地有则使用本地镜像,不拉取
Never 只使用本地镜像,从不拉取,即使本地没有
如果省略imagePullPolicy 镜像tag为 :latest 策略为always ,否则 策略为 IfNotPresent
参考
https://kubernetes.io/docs/concepts/containers/images/
https://kubernetes.io/docs/concepts/configuration/overview/
--------------------------------------
好,下面实验开始:
1: 先列一下pods:
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-59f87c8677-d6f9m 1/1 Running 8 49d
tomcat-59f87c8677-mv9w2 1/1 Running 7 49d
tomcat-59f87c8677-psctz 1/1 Running 7 49d
tomcat-59f87c8677-scv6g 1/1 Running 7 49d
tomcat-59f87c8677-vwmsh 1/1 Running 7 49d
2: 在看一下pod: tomcat-59f87c8677-d6f9m 是run 在哪个node 上的:
kubectl get pod -o wide, 可以看到这个pod 是运行在k8s-node 上的
[root@k8s-node ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 8-alpine 8b8b1eb786b5 7 months ago 106MB
tomcat 7-alpine 81cd9536b1e6 7 months ago 147MB
3:下面看一下pod 对应的image 的内容:
root@k8s-master ~]# kubectl get pod tomcat-59f87c8677-d6f9m -o json
"containers": [
{
"image": "tomcat:8-alpine",
"imagePullPolicy": "IfNotPresent",
"name": "tomcat",
4: 下面对这个image 进行修改:
kubectl edit pod tomcat-59f87c8677-d6f9m
"containers": [
{
"image": "tomcat:9-alpine",
"imagePullPolicy": "IfNotPresent",
"name": "tomcat",
然后保存后,可以看到详细:
[root@k8s-master ~]# kubectl describe pod tomcat-59f87c8677-d6f9m
Normal Killing 3m12s (x2 over 9m15s) kubelet, k8s-node Container tomcat definition changed, will be restarted
Normal Pulling 3m10s kubelet, k8s-node Pulling image "tomcat:9-alpine"
Normal Created 2m36s (x3 over 32m) kubelet, k8s-node Created container tomcat
Normal Pulled 2m36s kubelet, k8s-node Successfully pulled image "tomcat:9-alpine"
Normal Started 2m34s (x3 over 32m) kubelet, k8s-node Started container tomcat
好,我们再看到pod 已经重起了:
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-59f87c8677-d6f9m 1/1 Running 9 49d
tomcat-59f87c8677-mv9w2 1/1 Running 7 49d
tomcat-59f87c8677-psctz 1/1 Running 7 49d
tomcat-59f87c8677-scv6g 1/1 Running 7 49d
tomcat-59f87c8677-vwmsh 1/1 Running 7 49d
再到node: k8s-node 上可以看到新的image 已经拉取到本地啦:
[root@k8s-node ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 8-alpine 8b8b1eb786b5 7 months ago 106MB
tomcat 7-alpine 81cd9536b1e6 7 months ago 147MB
tomcat 9-alpine 58d02ef62ed2 23 months ago 117MB
OK, 可以看到这个image 已经拉取到本地,这个imagepullpolicy 的设置是: IfNotPresent. 如果一定要拉取本地的image 的话,要改成Nevel.