Pod

Pod #

操作 #

删除被驱逐的 pod

kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod

Infra 容器 #

使用镜像:k8s.gcr.io/pause

这个镜像是一个用汇编语言编写的、永远处于 “暂停” 状态的容器,解压后的大小也只有 100~200 KB 左右。

Init 容器 #

以 Init: 开始的 Pod 状态概括表示 Init 容器的执行状态。

下表展示了在调试 Init 容器时可能见到的状态值。

状态 含义
Init:N/M Pod 中有 M 个 Init 容器,其中 M 已经完成
Init:Error Init 容器执行错误
Init:CrashLoopBackOff Init 容器已经失败多次
Pending Pod 还没有开始执行 Init 容器
PodInitializing or Running Pod 已经完成执行 Init 容器

如果一个 Pod 停滞在 Pending 状态,表示 Pod 没有被调度到节点上。通常这是因为 某种类型的资源不足导致无法调度。 查看上面的 kubectl describe … 命令的输出,其中应该显示了为什么没被调度的原因。

常见原因:

  • 资源不足
  • 使用了 hostPort

参考:

PodStatus #

type PodStatus struct {
	// The phase of a Pod is a simple, high-level summary of where the Pod is in its lifecycle.
	// The conditions array, the reason and message fields, and the individual container status
	// arrays contain more detail about the pod's status.
	// There are five possible phase values:
	//
	// Pending: The pod has been accepted by the Kubernetes system, but one or more of the
	// container images has not been created. This includes time before being scheduled as
	// well as time spent downloading images over the network, which could take a while.
	// Running: The pod has been bound to a node, and all of the containers have been created.
	// At least one container is still running, or is in the process of starting or restarting.
	// Succeeded: All containers in the pod have terminated in success, and will not be restarted.
	// Failed: All containers in the pod have terminated, and at least one container has
	// terminated in failure. The container either exited with non-zero status or was terminated
	// by the system.
	// Unknown: For some reason the state of the pod could not be obtained, typically due to an
	// error in communicating with the host of the pod.
	//
	// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#pod-phase
	// +optional
	Phase PodPhase `json:"phase,omitempty" protobuf:"bytes,1,opt,name=phase,casttype=PodPhase"`
	// Current service state of pod.
	// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#pod-conditions
	// +optional
	// +patchMergeKey=type
	// +patchStrategy=merge
	Conditions []PodCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,2,rep,name=conditions"`
	// A human readable message indicating details about why the pod is in this condition.
	// +optional
	Message string `json:"message,omitempty" protobuf:"bytes,3,opt,name=message"`
	// A brief CamelCase message indicating details about why the pod is in this state.
	// e.g. 'Evicted'
	// +optional
	Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"`
	// nominatedNodeName is set only when this pod preempts other pods on the node, but it cannot be
	// scheduled right away as preemption victims receive their graceful termination periods.
	// This field does not guarantee that the pod will be scheduled on this node. Scheduler may decide
	// to place the pod elsewhere if other nodes become available sooner. Scheduler may also decide to
	// give the resources on this node to a higher priority pod that is created after preemption.
	// As a result, this field may be different than PodSpec.nodeName when the pod is
	// scheduled.
	// +optional
	NominatedNodeName string `json:"nominatedNodeName,omitempty" protobuf:"bytes,11,opt,name=nominatedNodeName"`

	// IP address of the host to which the pod is assigned. Empty if not yet scheduled.
	// +optional
	HostIP string `json:"hostIP,omitempty" protobuf:"bytes,5,opt,name=hostIP"`
	// IP address allocated to the pod. Routable at least within the cluster.
	// Empty if not yet allocated.
	// +optional
	PodIP string `json:"podIP,omitempty" protobuf:"bytes,6,opt,name=podIP"`

	// RFC 3339 date and time at which the object was acknowledged by the Kubelet.
	// This is before the Kubelet pulled the container image(s) for the pod.
	// +optional
	StartTime *metav1.Time `json:"startTime,omitempty" protobuf:"bytes,7,opt,name=startTime"`

	// The list has one entry per init container in the manifest. The most recent successful
	// init container will have ready = true, the most recently started container will have
	// startTime set.
	// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#pod-and-container-status
	InitContainerStatuses []ContainerStatus `json:"initContainerStatuses,omitempty" protobuf:"bytes,10,rep,name=initContainerStatuses"`

	// The list has one entry per container in the manifest. Each entry is currently the output
	// of `docker inspect`.
	// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#pod-and-container-status
	// +optional
	ContainerStatuses []ContainerStatus `json:"containerStatuses,omitempty" protobuf:"bytes,8,rep,name=containerStatuses"`
	// The Quality of Service (QOS) classification assigned to the pod based on resource requirements
	// See PodQOSClass type for available QOS classes
	// More info: https://git.k8s.io/community/contributors/design-proposals/node/resource-qos.md
	// +optional
	QOSClass PodQOSClass `json:"qosClass,omitempty" protobuf:"bytes,9,rep,name=qosClass"`
}

Pod phase(运行阶段) #

Pod 的 status 定义在 PodStatus 对象中,其中有一个 Phase(运行阶段) 字段。

下面是 phase 可能的值:

状态 涵义 备注
Pending Pod 被系统接受,至少有一个容器未被创建
Running Pod 已绑定 Node,所有容器已创建,至少一个容器在运行(或处于正在启动/重启)
Succeeded Pod 中所有容器被成功终止,不再重启
Failed Pod 中所有容器被终止,至少有一个容器是因为失败终止(非 0 退出或被系统终止)
Unknown 无法获取 Pod 状态(与 Pod 所在主机通信失败)

PodCondition #

Pod 有一个 PodStatus 对象,其中包含一个 PodCondition 数组。

PodCondition 数组的每个元素都有一个 type 字段和一个 status 字段。

  • type 字段是字符串,可能的值有
    • PodScheduled
    • Ready
    • Initialized
    • Unschedulable
  • status 字段是一个字符串,可能的值有
    • True
    • False
    • Unknown

Terminating #

ContainerCreating #


资源限制 #

request #

  • requests 用于 schedule 阶段,在调度 pod 保证所有 pod 的 requests 总和小于 node 能提供的计算能力
  • requests.cpu 被转成 docker 的 --cpu-shares 参数,与 cgroup cpu.shares 功能相同
    • 设置容器的 cpu 的相对权重
    • 该参数在 CPU 资源不足时生效,根据容器 requests.cpu 的比例来分配 cpu 资源
    • CPU 资源充足时,requests.cpu 不会限制 container 占用的最大值,container 可以独占 CPU
  • requests.memory 没有对应的 docker 参数,作为 k8s 调度依据
  • 使用 requests 来设置各容器需要的最小资源

limit #

  • limits 限制运行时容器占用的资源
  • limits.cpu 会被转换成 docker 的–cpu-quota 参数。与 cgroup cpu.cfs_quota_us 功能相同
    • 限制容器的最大 CPU 使用率。
    • cpu.cfs_quota_us 参数与 cpu.cfs_period_us 结合使用,后者设置时间周期
    • k8s 将 docker 的–cpu-period 参数设置 100 毫秒。对应着 cgroup 的 cpu.cfs_period_us
    • limits.cpu 的单位使用 m,千分之一核
  • limits.memory 会被转换成 docker 的–memory 参数。用来限制容器使用的最大内存
  • 当容器申请内存超过 limits 时会被终止

问答 #

为什么要有 pod #


参考 #


本文访问量

本站总访问量

本站总访客数