关键词:
Init 容器的介绍
Pod能够具有多个容器,应用运行在容器里面,但是它也可能有一个或多个先于应用容器启动的 Init容器
Init 容器与普通的容器非常像,除了如下两点:
c Init 容器总是运行到成功完成为止
2) 每个 Init 容器都必须在下一个 Init 容器启动之前成功完成
如果 Pod 的 Init 容器失败, Kubernetes 会不断地重启该 Pod ,直到 Init 容器成功为止。然而,如果 Pod 对应的 restartPolicy 为 Never。
Init 容器的作用
因为 Init 容器具有与应用程序容器分离的单独镜像,所以它们的启动相关代码具有如下优势:
1)它们可以包含并运行实用工具,但是出于安全考虑,是不建议在应用程序容器镜像中包含这些实用工具的
2) 它们可以包含使用工具和定制化代码来安装,但是不能出现在应用程序镜像中。例如,创建镜像没必要 FROM 另一个镜像,只需要在安装过程中使用类似 sed 、 awk 、 python 或 dig
这样的工具。
3) 应用程序镜像可以分离出创建和部署的角色,而没有必要联合它们构建一个单独的镜像。
4) Init 容器使用 Linux Namespace ,所以相对应用程序容器来说具有不同的文件系统视图。因此,它们能够具有访问 Secret 的权限,而应用程序容器则不能。
5) 它们必须在应用程序容器启动之前运行完成,而应用程序容器是并行运行的,所以 Init 容器能够提供了一种简单的阻塞或延迟应用容器的启动的方法,直到满足了一组先决条件。
测试
说明:主要是在启动Pod,有2个initc,一开始是没有准备的,所以现象会显示初始化0/2
init.yaml
kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: [‘sh‘, ‘-c‘, ‘echo The app is running! && sleep 3600‘] initContainers: - name: init-myservice image: busybox command: [‘sh‘, ‘-c‘, ‘until nslookup myservice; do echo waiting for myservice; sleep 2; done;‘] - name: init-mydb image: busybox command: [‘sh‘, ‘-c‘, ‘until nslookup mydb; do echo waiting for mydb; sleep 2; done;‘]
[root@k8s-master mnt]# kubectl create -f init.yaml pod/myapp-pod created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 8s [root@k8s-master mnt]# kubectl describe myapp-pod error: the server doesn‘t have a resource type "myapp-pod" [root@k8s-master mnt]# kubectl describe pod myapp-pod Name: myapp-pod Namespace: default Priority: 0 Node: k8s-node02/192.168.180.134 Start Time: Wed, 18 Dec 2019 22:02:57 +0800 Labels: app=myapp Annotations: <none> Status: Pending IP: 10.244.1.9 IPs: IP: 10.244.1.9 Init Containers: init-myservice: Container ID: docker://3c0e850042efab506f95737adfd3dc6ef2da9218ce51eb5eb4e94573a657fd2b Image: busybox Image ID: docker-pullable://busybox@sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Port: <none> Host Port: <none> Command: sh -c until nslookup myservice; do echo waiting for myservice; sleep 2; done; State: Running Started: Wed, 18 Dec 2019 22:03:03 +0800 Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) init-mydb: Container ID: Image: busybox Image ID: Port: <none> Host Port: <none> Command: sh -c until nslookup mydb; do echo waiting for mydb; sleep 2; done; State: Waiting Reason: PodInitializing Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) Containers: myapp-container: Container ID: Image: busybox Image ID: Port: <none> Host Port: <none> Command: sh -c echo The app is running! && sleep 3600 State: Waiting Reason: PodInitializing Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) Conditions: Type Status Initialized False Ready False ContainersReady False PodScheduled True Volumes: default-token-gx2h8: Type: Secret (a volume populated by a Secret) SecretName: default-token-gx2h8 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned default/myapp-pod to k8s-node02 Normal Pulling 22s kubelet, k8s-node02 Pulling image "busybox" Normal Pulled 18s kubelet, k8s-node02 Successfully pulled image "busybox" Normal Created 18s kubelet, k8s-node02 Created container init-myservice Normal Started 17s kubelet, k8s-node02 Started container init-myservice
查看myservice
[root@k8s-master mnt]# kubectl logs myapp-pod -c init-myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can‘t find myservice.default.svc.cluster.local: NXDOMAIN *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer *** Can‘t find myservice.default.svc.cluster.local: No answer *** Can‘t find myservice.svc.cluster.local: No answer *** Can‘t find myservice.cluster.local: No answer *** Can‘t find myservice.localdomain: No answer waiting for myservice
myservice.yaml
[root@k8s-master mnt]# cat myservice.yaml kind: Service apiVersion: v1 metadata: name: myservice spec: ports: - protocol: TCP port: 80 targetPort: 9376 [root@k8s-master mnt]#
[root@k8s-master mnt]# vim myservice.yaml [root@k8s-master mnt]# kubectl create -f myservice.yaml service/myservice created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m23s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m25s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m27s [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h myservice ClusterIP 10.102.35.5 <none> 80/TCP 49s [root@k8s-master mnt]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-58cc8c89f4-pzbrd 1/1 Running 23 10h coredns-58cc8c89f4-vmhl2 1/1 Running 23 10h etcd-k8s-master 1/1 Running 4 10h kube-apiserver-k8s-master 1/1 Running 4 10h kube-controller-manager-k8s-master 1/1 Running 21 10h kube-flannel-ds-amd64-c4fs4 1/1 Running 2 9h kube-flannel-ds-amd64-ct6mc 1/1 Running 2 9h kube-flannel-ds-amd64-mtzz9 1/1 Running 5 9h kube-proxy-9bdql 1/1 Running 2 9h kube-proxy-cv8lk 1/1 Running 2 9h kube-proxy-h8jk8 1/1 Running 4 10h kube-scheduler-k8s-master 1/1 Running 21 10h [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 5m58s [root@k8s-master mnt]# vim myservice.yaml [root@k8s-master mnt]# kubectl create -f myservice.yaml service/myservice created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m23s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m25s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m27s [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h myservice ClusterIP 10.102.35.5 <none> 80/TCP 49s [root@k8s-master mnt]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-58cc8c89f4-pzbrd 1/1 Running 23 10h coredns-58cc8c89f4-vmhl2 1/1 Running 23 10h etcd-k8s-master 1/1 Running 4 10h kube-apiserver-k8s-master 1/1 Running 4 10h kube-controller-manager-k8s-master 1/1 Running 21 10h kube-flannel-ds-amd64-c4fs4 1/1 Running 2 9h kube-flannel-ds-amd64-ct6mc 1/1 Running 2 9h kube-flannel-ds-amd64-mtzz9 1/1 Running 5 9h kube-proxy-9bdql 1/1 Running 2 9h kube-proxy-cv8lk 1/1 Running 2 9h kube-proxy-h8jk8 1/1 Running 4 10h kube-scheduler-k8s-master 1/1 Running 21 10h [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 5m58s
现象:发现变成1/2了
[root@k8s-master mnt]# cat mydb.yaml kind: Service apiVersion: v1 metadata: name: mydb spec: ports: - protocol: TCP port: 80 targetPort: 9377 [root@k8s-master mnt]#
[root@k8s-master mnt]# vim mydb.yaml [root@k8s-master mnt]# kubectl create -f mydb.yaml service/mydb created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 11m [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 11m [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 1/1 Running 0 12m [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h mydb ClusterIP 10.104.158.92 <none> 80/TCP 3m24s myservice ClusterIP 10.102.35.5 <none> 80/TCP 10m [root@k8s-master mnt]#
现象:myapp-pod起来了
说明
- 在 Pod 启动过程中, Init 容器会按顺序在网络和数据卷初始化之后启动。每个容器必须在下一个容器启动之前成功退出
- 如果由于运行时或失败退出,将导致容器启动失败,它会根据 Pod 的 restartPolicy 指定的策略进行重试。然而,如果 Pod 的 restartPolicy 设置为 Always , Init 容器失败时会使用RestartPolicy 策略
- 在所有的 Init 容器没有成功之前, Pod 将不会变成 Ready 状态。 Init 容器的端口将不会在Service 中进行聚集。 正在初始化中的 Pod 处于 Pending 状态,但应该会将 Initializing 状态设置为 true
- 如果 Pod 重启,所有 Init 容器必须重新执行
- 对 Init 容器 spec 的修改被限制在容器 image 字段,修改其他字段都不会生效。更改 Init容器的 image 字段,等价于重启该 Pod
- Init 容器具有应用容器的所有字段。除了 readinessProbe ,因为 Init 容器无法定义不同于完成( completion )的就绪( readiness )之外的其他状态。这会在验证过程中强制
- 在 Pod 中的每个 app 和 Init 容器的名称必须唯一;与任何其它容器共享同一个名称,会在验证时抛出错误
pod进阶(代码片段)
一、Pod生命周期initcontainer:初始化容器,可以有多个maincontainer:主容器poststart:在初始化容器执行完后,主容器启动前的操作prestop:结束前的清理操作livenessprobe:在主容器运行是进行存活性检测redinessprobe:就绪状态检测1、Pod... 查看详情
精品必知pod生命周期(代码片段)
...建过程(通过管理进程提交创建请求)运行初始化容器(initcontainer)过程白话解释:主容器来运行nginx,但是要求在运行nginx之前先要能够连接上mysql和redis所在服务器。应用场景:提供主容器镜像中不具备的工具程序或自定义代... 查看详情
pod详解之资源拉取策略容器分类(代码片段)
...的分类2.1pause基础容器(infrastructurecontainer)2.2init初始化容器(initcontainers)2.3业务容器(Mainconta 查看详情
pod详解之资源拉取策略容器分类(代码片段)
...的分类2.1pause基础容器(infrastructurecontainer)2.2init初始化容器(initcontainers)2.3业务容器(Mainconta 查看详情
k8s之pod详解(代码片段)
...理的PodPod容器的分类1、基础容器(infrastructure.container)2、初始化容器(initcontainers)Init的容器作用3、应用容器(Maincontainer 查看详情
k8s之pod详解(代码片段)
...理的PodPod容器的分类1、基础容器(infrastructure.container)2、初始化容器(initcontainers)Init的容器作用3、应用容器(Maincontainer 查看详情
k8s之pod详解(代码片段)
...理的PodPod容器的分类1、基础容器(infrastructure.container)2、初始化容器(initcontainers)Init的容器作用3、应用容器(Maincontainer 查看详情
云原生之kuberneteskubernetes集群下初始化容器的使用方法(代码片段)
【云原生之kubernetes】kubernetes集群下初始化容器的使用方法一、初始化容器介绍1.初始化容器简介2.初始化容器特点二、检查本地集群状态1.检查工作节点状态2.检查系统pod状态三、运行一个初始化容器的pod1.编辑web_init.yaml文件2.运... 查看详情
kubernetes(k8s)之pod生命周期(代码片段)
Pod生命周期什么是Pod生命周期Init初始化容器Init容器能干的事Init容器特点init初始化实例探针livenessProbe存活探针readinessProbe就绪探针什么是Pod生命周期和一个独立的应用容器一样,Pod也被认为是相对临时性(而不是长期存... 查看详情
pod详解之资源拉取策略容器分类(代码片段)
...的分类2.1pause基础容器(infrastructurecontainer)2.2init初始化容器(initcontainers)2.3业务容器(Maincontainer)看总结Pod容器分类Pod资源分类Pod的重启策略Pod镜像拉取策略仓库:Pod生命周期前言一、Pod基础概念1.1介绍... 查看详情
k8s五pod生命周期(代码片段)
...;Succeeded)失败(Failed)未知(Unknown)InitContainersInitContianers的作用InitContianers实验特殊说明总结写在后面前言前面我们部署了k8s容器环境,也知道了怎么书写数据自己的Pod,那本节我们就一起来学习Pod... 查看详情
k8s之pod详解(代码片段)
...理的PodPod容器的分类1、基础容器(infrastructure.container)2、初始化容器(initcontainers)Init的容器作用3、应用容器(Maincontainer)镜像拉取策略(imagePullPolicy)重启策略(restartPolicy):Pod在遇到故障之后重启的动作部署harbor创建私有项目... 查看详情
k8s之pod详解(代码片段)
...理的PodPod容器的分类1、基础容器(infrastructure.container)2、初始化容器(initcontainers)Init的容器作用3、应用容器(Maincontainer)镜像拉取策略(imagePullPolicy)重启策略(restartPolicy):Pod在遇到故障之后重启的动作部署harbor创建私有项目... 查看详情
python3-k8s之深入理解pod(代码片段)
...PodHook2.1环境准备2.2优雅删除资源对象2.3Pod健康检查三、初始化容器3.1init-pod3.2初始化配置pod四、常见对象操作4.1使用ReplicationController、ReplicaSe 查看详情
#我的新年flag#pod生命周期
...期流程创建主容器(containers)是必现的操作;初始化容器(initContainers)容器启动后钩子启动探测、存活性探测(livenessprobe)、就绪性探测(readnessprobe)容器终止前钩子Pod声明周期几个状态Pending:创建pod资源并存入etcd中,尚未完成调... 查看详情
k8s之pod(代码片段)
...之Pod一、概述在Kubrenetes集群中Pod有如下两种使用方式pause容器使得Pod中的所有容器可以共享两种资源:网络和存储。二、Pod分类1.自主式Pod2.控制器管理的Podkubernetes中的pause容器主要为每个业务容器提供以下功能:·Kubernetes设计这样... 查看详情
k8s之pod(代码片段)
...之Pod一、概述在Kubrenetes集群中Pod有如下两种使用方式pause容器使得Pod中的所有容器可以共享两种资源:网络和存储。二、Pod分类1.自主式Pod2.控制器管理的Podkubernetes中的pause容器主要为每个业务容器提供以下功能:·Kubernetes设计这样... 查看详情
k8s之pod(代码片段)
...元,是K8S集群中的一个应用实例。Pod中包含了一个或多个容器,还包括了存储,网络等各个容器共享的资源。Pod支持多种容器环境,Docker则是最流行的容器环境。二.Pod容器生命周期三.Init容器Pod能够具有多个容器,应用运用在容... 查看详情