Kubernetes----Pod中的初始化容器

网友投稿 267 2022-09-11

Kubernetes----Pod中的初始化容器

一、初始化容器简介与应用场景

1.1 初始化容器简介

初始化容器是在pod的主容器启动之前要运行的容器,主要是做一些主容器的前置工作,它有两大特征:

1、初始化容器必须运行完成直到结束,若某初始化容器运行失败,那么kubernetes需要重启它直到成功完成2、初始化容器必须按照定义的顺序执行,当且仅当前一个成功之后,后一个才能执行

1.2 初始化容器使用场景

初始化容器有很多应用场景,如下为最常见的几个:

提供主容器镜像中不具备的工具程序或者自定义代码初始化容器要先于应用容器串行启动并运行完成,因此可用于延后应用容器的启动直至其依赖的条件得到满足

二、创建初始化容器

2.1 编写yaml文件

如下,编写pod_init_container.yaml文件,内容如下,设置一个初始化容器,用于ping 192.168.16.41,如果ping通了则继续,如果ping不通,则继续等待直到ping通

apiVersion: v1kind: Namespacemetadata: name: dev---apiVersion: v1kind: Podmetadata: name: pod-init-container namespace: dev labels: user: redrose2100spec: containers: - name: nginx image: nginx:1.17.1 ports: - name: nginx-port containerPort: 80 protocol: TCP initContainers: - name: test-ping image: busybox:1.30 command: ["sh","-c","until ping 192.168.16.41 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

2.2 创建资源

使用如下命令创建资源

[root@master pod]# kubectl apply -f pod_init_container.yamlnamespace/dev createdpod/pod-init-container created[root@master pod]#

2.3 查看资源创建状态

如下,可以看到pod创建过程中,首先创建并执行的初始化容器

[root@master pod]# kubectl get pod -n devNAME READY STATUS RESTARTS AGEpod-init-container 1/1 Running 0 2m15s[root@master pod]# kubectl describe pod pod-init-container -n devName: pod-init-containerNamespace: devPriority: 0Node: node2/192.168.16.42Start Time: Tue, 22 Mar 2022 17:02:15 +0800Labels: user=redrose2100Annotations: Status: RunningIP: 10.244.2.37IPs: IP: 10.244.2.37Init Containers: test-ping: Container ID: docker://ad5638803015fa3aec476117828a68561c3b11e478e3ea50c036700871a6e318 Image: busybox:1.30 Image ID: docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d Port: Host Port: Command: sh -c until ping 192.168.16.41 -c 1 ; do echo 'waiting for ping...'; sleep 2; done; State: Terminated Reason: Completed Exit Code: 0 Started: Tue, 22 Mar 2022 17:02:15 +0800 Finished: Tue, 22 Mar 2022 17:02:15 +0800 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-z8w87 (ro)Containers: nginx: Container ID: docker://e1f0e2f09720dcd0f467288e8984b7741f2e6d73825428e50c9b025779a307cc Image: nginx:1.17.1 Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb Port: 80/TCP Host Port: 0/TCP State: Running Started: Tue, 22 Mar 2022 17:02:16 +0800 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-z8w87 (ro)Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled TrueVolumes: kube-api-access-z8w87: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: DownwardAPI: trueQoS Class: BestEffortNode-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m29s default-scheduler Successfully assigned dev/pod-init-container to node2 Normal Pulled 2m29s kubelet Container image "busybox:1.30" already present on machine Normal Created 2m29s kubelet Created container test-ping Normal Started 2m29s kubelet Started container test-ping Normal Pulled 2m28s kubelet Container image "nginx:1.17.1" already present on machine Normal Created 2m28s kubelet Created container nginx Normal Started 2m28s kubelet Started container nginx[root@master pod]#

2.4 删除资源

使用如下命令删除资源

[root@master pod]# kubectl delete -f pod_init_container.yamlnamespace "dev" deletedpod "pod-init-container" deleted[root@master pod]#

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小麦岛火爆全网!网红营销助力下,青岛旅游如何火“出圈”?
下一篇:Kubernetes----Pod生命周期简介
相关文章

 发表评论

暂时没有评论,来抢沙发吧~