k8s中初始化容器(init container)的作用及其使用方法 (2)

查看初始化容器的日志

[root@nccztsjb-node-11 ~]# kubectl logs initnginx -c install Connecting to (103.235.46.39:443) wget: note: TLS certificate validation not implemented saving to '/work-dir/index.html' index.html 100% |********************************| 2443 0:00:00 ETA '/work-dir/index.html' saved [root@nccztsjb-node-11 ~]#

OK,通过日志发现是向/work-dir/目录中写了index.html文件。

以上就是初始化容器的基本的用法。

查看pod的事件也可以看到pod中容器运行的顺序

[root@nccztsjb-node-11 ~]# kubectl describe pod initnginx Name: initnginx Namespace: default Priority: 0 Node: 172.20.59.57/172.20.59.57 Start Time: Tue, 11 Jan 2022 13:49:57 +0800 Labels: <none> Annotations: cni.projectcalico.org/podIP: 172.23.29.31/32 cni.projectcalico.org/podIPs: 172.23.29.31/32 Status: Running IP: 172.23.29.31 IPs: IP: 172.23.29.31 Init Containers: install: Container ID: docker://9d2aec37255a02a2b13abced64717057eb100dd329bc6b6529211e30595f1c93 Image: busybox Image ID: docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678 Port: <none> Host Port: <none> Command: wget -O /work-dir/index.html https://www.baidu.com State: Terminated Reason: Completed Exit Code: 0 Started: Tue, 11 Jan 2022 13:50:05 +0800 Finished: Tue, 11 Jan 2022 13:50:08 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-8ss8s (ro) /work-dir from workdir (rw) Containers: nginx: Container ID: docker://9f088e80c72e6691ea48bf3a9edf3983ccf8e8d32dd17e27425916efdfec4ff4 Image: 172.20.45.174:81/base/nginx:1.15-alpine Image ID: docker-pullable://172.20.45.174:81/base/nginx@sha256:478a73bcec93acc3e814ddd6fb2f95c6f6b4b0d0f168a4feaa039513d260a5d9 Port: 80/TCP Host Port: 0/TCP State: Running Started: Tue, 11 Jan 2022 13:50:08 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /usr/share/nginx/html from workdir (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-8ss8s (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: workdir: Type: EmptyDir (a temporary directory that shares a pod's lifetime) Medium: SizeLimit: <unset> default-token-8ss8s: Type: Secret (a volume populated by a Secret) SecretName: default-token-8ss8s Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 6m27s default-scheduler Successfully assigned default/initnginx to 172.20.59.57 Normal Pulling 6m26s kubelet Pulling image "busybox" Normal Pulled 6m20s kubelet Successfully pulled image "busybox" in 6.378365555s Normal Created 6m20s kubelet Created container install Normal Started 6m19s kubelet Started container install Normal Pulled 6m16s kubelet Container image "172.20.45.174:81/base/nginx:1.15-alpine" already present on machine Normal Created 6m16s kubelet Created container nginx Normal Started 6m16s kubelet Started container nginx [root@nccztsjb-node-11 ~]#

即先创建初始化容器,运行结束后,才是运行nginx这个应用容器。

初始化容器使用的说明

关于初始化容器的使用,有几点说明

在pod中可以有一个或者多个初始化容器

初始化容器执行成功后,应用的容器才能被执行

初始化容器是仅运行一次的任务

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggpyd.html