1Pod生命周期

Pod遵循定义的生命周期,从该Pending 阶段开始,Running 阶段其主容器中至少有一个开始运行OK,Succeeded或 Failed阶段,根据Pod中的任何容器是否以失败终止。

1.1 Pod生命状态含义

1.1.1 Pending 该Pod已被Kubernetes集群接受,但是尚未设置一个或多个容器并使其可以运行。这包括Pod等待排定所花费的时间,以及通过网络下载容器映像所花费的时间。

1.1.2 Running Pod已绑定到一个节点,并且所有容器都已创建。至少一个容器仍在运行,或者正在启动或重新启动。

1.1.3 Succeeded Pod中的所有容器已成功终止,因此不会重新启动。

1.1.4 Failed Pod中的所有容器均已终止,并且至少一个容器因故障而终止。也就是说,容器要么以非零状态退出,要么被系统终止。

1.1.5 Unknown 由于某种原因,无法获得Pod的状态。此阶段通常是由于与Pod应该在其中运行的节点通信时发生错误而发生的。

1.1.6

Pod 的详细状态

CrashLoopBackOff: 容器退出,kubelet正在将它重启

InvalidImageName: 无法解析镜像名称

ImageInspectError: 无法校验镜像

ErrImageNeverPull: 策略禁止拉取镜像

ImagePullBackOff: 正在重试拉取

RegistryUnavailable: 连接不到镜像中心

ErrImagePull: 通用的拉取镜像出错

CreateContainerConfigError: 不能创建kubelet使用的容器配置

CreateContainerError: 创建容器失败

m.internalLifecycle.PreStartContainer 执行hook报错

RunContainerError: 启动容器失败

PostStartHookError: 执行hook报错

ContainersNotInitialized: 容器没有初始化完毕

ContainersNotReady: 容器没有准备完毕

ContainerCreating:容器创建中

PodInitializing:pod 初始化中

DockerDaemonNotReady:docker还没有完全启动

NetworkPluginNotReady: 网络插件还没有完全启动

2 Pod生成过程(一般通过yaml脚本创建)

Pods被认为是相对短暂的(而不是持久的)实体。创建Pod,为其分配一个唯一的ID(UID),并将其调度到保留它们的节点,直到终止(根据重新启动策略)或删除为止。如果一个节点死掉后,安排到该节点的Pod计划在超时后删除。

3 Pod重启策略(一般重启通过删除Pod,下面的重启策略是在触发某种条件而重启。)

支持三种RestartPolicy:

Always:只要退出就重启。

OnFailure:失败退出(exit code不等于0)时重启。

Never:只要退出就不再重启。

资源清单脚本配置重启策略

apiVersion: v1
kind: Pod
metadata:
 name: test-pd
spec:
 containers:
  - image: tomcat:8.0
    name: test-container
    ports:
   - containerPort: 8080
     protocol: TCP
  restartPolicy: OnFailure

4 Pod中容器镜像拉取策略

支持三种ImagePullPolicy:

Always:不管镜像是否存在都会进行一次拉取。

Never:不管镜像是否存在都不会进行拉取。

IfNotPresent:只有镜像不存在时,才会进行镜像拉取。

资源清单配置容器拉取策略

apiVersion: v1
kind: Pod
metadata:
 name: test-pd
spec:
 containers:
  - image: tomcat:8.0
    imagePullPolicy: IfNotPresent
    name: test-container
    ports:
   - containerPort: 8080
     protocol: TCP
  restartPolicy: OnFailure

5 Pod指定节点运行

K8s 能根据实际情况自动为Pod分配节点,也可以指定Pod运行在某个节点上。非常灵活,也很符合实际项目。

5.1 在yaml脚本上指定要运行的节点名。

apiVersion: v1
kind: Pod
metadata:
 name: test-pd
spec:
 nodeName: node1
 containers:
  - image: tomcat:8.0
    name: test-container
    ports:
   - containerPort: 8080
     protocol: TCP

5.2 在yaml脚本上指定节点的label,通过节点的label进行匹配。

apiVersion: v1
kind: Pod
metadata:
 name: test-pd
spec:
 nodeSelector:
    type: node1
 containers:
  - image: tomcat:8.0
    name: test-container
    ports:
   - containerPort: 8080
     protocol: TCP

5.3 查看Pod节点上label值,没有label的话,为Pod添加label命令。

kubectl get nodes --show-labels
kubectl label node k8s-node1 type=node1

6 Pod资源分配。计算资源为CPU和内存。

资源合理分配,才能让系统整体更加流畅。同时也防止某些需要少资源应用就可运行占用太多资源。可通过下面的脚步命令配置。requests 要分分配的资源,limits为最高请求的资源值。可以理解为初始值和最大值

spec.containers[].resources.limits.cpu
spec.containers[].resources.limits.memory
spec.containers[].resources.requests.cpu
spec.containers[].resources.requests.memory

6.1 资源yaml脚本配置

apiVersion: v1
kind: Pod
metadata:
 name: test-pd
spec:
 containers:
  - image: tomcat:8.0
    name: test-container
    ports:
   - containerPort: 8080
     protocol: TCP
    resources:
          limits:
            cpu: "4"
            memory: 2Gi
          requests:
            cpu: 250m
            memory: 250Mi

7 Pod中容器存活性探测。检测的目的是对非正常状态的容器进行一些处理(例如不健康则删除重建改容器或者如果不正常则更新容器的状态)。

7.1 探针种类与作用

LivenessProbe:探测应用是否处于健康状态,如果不健康则删除重建改容器

ReadinessProbe:探测应用是否启动完成并且处于正常服务状态,如果不正常则更新容器的状态

7.2 7.1两探针的探测方法 。支持exec、tcp和httpGet方式

7.3 yaml资源清单脚本配置httpGet

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: nginx
  name: nginx
spec:
    containers:
    - image: nginx
      imagePullPolicy: Always
      name: http
      livenessProbe:
        httpGet:
        path: /
        port: 80
        initialDelaySeconds: 15
        timeoutSeconds: 1
      readinessProbe:
        httpGet:
        path: /ping
        port: 80
        initialDelaySeconds: 5
        timeoutSeconds: 1

7.4 yaml资源清单脚本配置tcp socket

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: nginx
  name: nginx
spec:
    containers:
    - image: nginx
      imagePullPolicy: Always
      name: http
      livenessProbe:
        tcpSocket:
        port: 80
        initialDelaySeconds: 30
        timeoutSeconds: 1
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐