云其它

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > 云其它 > K8s基本单元Pod

K8s基本单元Pod解读

作者:大新屋

Kubernetes中Pod是调度单元,包含多个容器及Pause容器,提供共享网络、存储等资源,状态分为Pending、Running、Failed等,支持存活、就绪、启动探针检测容器健康

什么是Pod?

Pod是Kubernetes中的最小调度单元,k8s是通过定义一个Pod的资源,Pod资源里运行一组、一个或多个容器,每个Pod还包含一个Pause容器。

Pause容器是Pod的父容器,它主要负责全局进程的回收管理,同时通过Pause容器可以使用一个Pod里面的不同容器共享存储、网络、PID、IPC等,容器之间可以使用localhost:port相互访问,可以使用volume等实现数据共享。根据Docker的构造,Pod可被建模为一组具有共享命令空间、卷、IP地址和Port端口的容器。Pod对外提供一个IP让其它Pod访问到Pod资源里面的指定容器的端口。

提示:Kubernetes官方Pod说明文档 https://kubernetes.io/docs/concepts/workloads/pods/

如下图所示,可以通俗理解Pod,可以把Pod看做一个豌豆荚,而豌豆荚里的每个豆子都是Pod中的容器。

一、Pod运行方式

1、查询Pod

### 查询Pod
kubectl get pods -A
kubectl get pods -n kube-system

### 查询Containerd镜像与容器信息
crictl images
crictl ps

### 查询有关Pod官方说明
kubectl explain pod

2、命令方式创建Pod

### kubectl run命令参数说明:
# nginx自定义Pod名称
# --image指定nginx镜像下载地址
# --port指定nginx容器运行端口
# --namespace 指定Pod的命名空间

### 创建Pod
kubectl run nginx --image=registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24 --port 80 --namespace=default

### 查看Pod信息
kubectl get pods -n default
kubectl get pods -n default -owide
kubectl logs -f nginx -n default
kubectl logs -f nginx -n default

### 删除Pod
kubectl delete pods nginx

3、命令方式仅打印创建Pod的yaml文件但不创建Pod

提示:使用命令方式添加参数–dry-run=client仅运行打印创建Pod信息但不创建Pod,适用于快速创建一个Pod的yaml文件模`

mkdir -p /data/yaml/pod
kubectl run nginx --image=registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24 --port 80 --namespace=default --dry-run=client -oyaml > /data/yaml/pod/nginx.yaml

4、使用yaml文件创建Pod

提示:yaml文件是一种轻量级的数据序列化标准,易于阅读和编写,非常适合用来定义Kubernetes的资源对象,如Pod、Service、Deployment等

### yaml文件参数说明:
apiversion: v1                                              # 填写API资源版本号,通过kubectl api-resources | grep pods命令查询API资源版本号
kind: Pod      # 类型Pod,可选 Deployment、StatefuSet、Service等,根据需要选择
metadata:                                                   # 资源的元数据或属性
  labels:                                                   # 设置Pod标签
app: nginx-app                                              # 自定义Pod标签名称
  name: nginx-pod                                           # 自定义Pod名称
  namespace: default                                        # 设置Pod所属命名空间,默认default命名空间
spec:                                                       # 定义Pod的资源行为
  containers:   # 定义容器规格,包含一个或多个容器规格
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24   # 指定容器拉取镜像地址
    imagePullPolicy: IfNotPresent # 镜像拉取策略,Always每次启动都通过--image参数拉取镜像(默认值);IfNotPresent本地有镜像就使用本地的,没有再通过--image参数拉取;Never 不拉取--image参数的镜像
    name: nginx01             # 自定义容器名称,用于区分Pod资源的多个容器
ports:	                      # 指定容器端口列表,可以定义一个或多个端口
	- containerPort:80        # 指定容器需要暴露的端口
  restartPolicy: Always       # 容器重启策略,Always容器失效时,自动重启容器(默认值),OnFailure容器不为0状态码终止时,自动重启容器;Never无论容器任何状态都不会重启
  
### 编写创建Pod的yaml文件
cat > /data/yaml/pod/nginx.yaml <<  'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod并查询Pod信息
kubectl create -f /data/yaml/pod/nginx.yaml
kubectl get pods -n default
kubectl get pods -n default –owide

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx.yaml

二、Pod Phase变化阶段

ValueDescription
Pending(挂起)Pod已被Kubnetes系统接受,但是一个或多个容器还没有设置好并准备好运行。这包括Pod等待调度的时间,以及通过网络下载容器映像所花费的时间,可以通过kubectl describe pods查看处于Pending状态的原因。
Running(运行中)Pod已经被绑定到一个节点上,并且所有的容器都已经被创建,而且至少有一个是运行状态,或者是正在启动或重启,可以通过kubectl logs查看Pod的日志。
Successed(成功)Pod中的所有容器都已成功终止,并且不会重新启动,可以通过kubectl logs查看Pod日志。
Failed(失败)Pod中的所有容器都已终止,并且至少有一个容器因失败而终止。也就是说,容器要么以非零状态退出,要么被系统终止,并且没有设置为自动重新启动,可以通过kubectl logs和kubectl describe查看Pod日志和状态。
Unknown(未知)由于某些原因,无法获得Pod的状态。此阶段通常是由于与运行Pod的节点通信时出现错误而发生的。
Unknown(未知)包含以下状态Description
InagePullBackOff(ErrImagePulll)镜像拉取失败,一般由于镜像不存在、网络不通或者需要登录认证引起的,可以使用kubectl describe pods 命令或者kubectl logs命令查看具体原因。
CrashLoopBackOff容器启动失败,可以通过logs命令查看具体原因,一般为启动命令不正确,健康检查不通过等。
OOMKilled容器内存溢出,一般是容器的内存Limit设置过小或者程序本身有内存溢出,可以通过kubectl logs查看程序启动日志。
TeminatingPod正在被删除,可以通过kubectl escribe查看状态。
SysctlForbiddenPod自定义了内核配置,但kubelet没有添加内核配置或配置的内核参数不支持,可以通过kubectl describe pods 查看具体原因。
Completed容器内部主进程退出,一般计划任务执行结束会显示该状态,此时可以通过kubectl logs查看容器日志。
ContainnerCreatingPod正在创建,一般为正在下载镜像或有配置不当的地方,可以通过kubectl describe pods查看具体原因。

三、Pod三种探针

1、什么是探针

Kubernetes(K8s)中的探针(Probe)主要用于检测和管理容器的健康状态,确保服务的稳定性和高可用性。

探针主要有三种类型:存活探针(Liveness Probe)、就绪探针(Readiness Probe)和启动探针(Startup Probe),每种探针都有其特定的作用和应用场景。

存活探针(Liveness Probe)

就绪探针(Readiness Probe)

启动探针(Startup Probe)

2、Pod启动过程

创建Pod — Pending — ContainerCreate — Running — InitContainer — Container — StartupProbe(执行第一个探针) — LivenessProbe(执行第二个探针)/ReadinessProbe(执行第三个探针) — Endpoin 添加Pod IP

3、Pod退出过程

删除Pod — Dead(30s宽限期内) — Terminating — PreStop(宽限期结束PreStop未结束,再次获得2秒的宽限期,宽限期结束,Pod收到SIGKILL信息) — EndPoints 删除Pod IP(kubelet请求APIServer,将宽限期设置为0,然后进行删除操作)

4、Pod 探针四钟检查机制

提示:Kubernetes官方关掉探针检查机制说明 https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes

5、创建Pod 包含就绪探针Readiness Probe并测试

(1)、就绪探针Readiness Probe参数说明

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    readinessProbe:             # 选择就绪探针Readiness Probe类型
      httpGet:                  # 选择探针httpGet检查机制,HTTP GET请求方式
        path: /index.html       # 请求路径
        port: 8080              # 请求端口
        scheme: HTTP            # 请求的协议,HTTP协议或HTTPS协议
      initialDelaySeconds: 10   # 容器启动后等待多少秒开始进行首次探测,默认值0秒
      timeoutSeconds: 2         # 探测请求超时时间,默认值1秒
      periodSeconds: 5          # 执行探测的频率,表示每5秒探测一次。对于Liveness Probe和Readiness Probe,默认值都是10秒。对于Startup Probe默认值没有设置,必须显式指定
      successThreshold: 1       # 在探测成功前需要连续探测多少次才算成功。默认值为1。这通常在Startup Probe中使用以避免过早的成功判断
      failureThreshold: 2       # 在探测失败后需要连续探测多少次才算失败。对于Liveness Probe,默认值为3;对于Readiness Probe,默认值为3;对于Startup Probe,默认值没有设置,必须显式指定
    ports:
    - containerPort: 80
  restartPolicy: Always

(2)、创建一个诊断成功的Pod包含就绪探针Readiness Proe(httpGet检查规则)

### 编写Pod 包含就绪探针Readiness Probe的yaml文件(指定一个容器真实存在的端口,例如80端口)
cat > /data/yaml/pod/nginx-ReadinessProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    readinessProbe:
      httpGet:
        path: /index.html
        port: 80
        scheme: HTTP
      initialDelaySeconds: 10
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含就绪探针Readiness Probe并检查
kubectl create -f /data/yaml/pod/nginx-ReadinessProbe.yaml
kubectl get pods -n default -owide

### 查看创建的Pod是否已生成探针配置
root@k8s-master01:~# kubectl get pods -n default -oyaml | grep -A 9 "readinessProbe"
      readinessProbe:
        failureThreshold: 2
        httpGet:
          path: /index.html
          port: 80
          scheme: HTTP
        initialDelaySeconds: 10
        periodSeconds: 5
        successThreshold: 1
        timeoutSeconds: 2

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-ReadinessProbe.yaml

(3)、创建一个诊断失败的Pod包含就绪探针Readiness Proe(httpGet检查规则)

### 编写Pod 包含就绪探针Readiness Probe的yaml文件(指定一个容器不存在的端口,例如8080端口)
cat > /data/yaml/pod/nginx-ReadinessProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    readinessProbe:
      httpGet:
        path: /index.html
        port: 8080
        scheme: HTTP
      initialDelaySeconds: 10
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含就绪探针Readiness Probe
kubectl create -f /data/yaml/pod/nginx-ReadinessProbe.yaml

### 查看已创建的Pod,虽然Pod显示SATUS显示Running,但READY显示0/1是代表容器运行失败的
root@k8s-master01:~# kubectl get pods -n default -owide
NAME        READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nginx-pod   0/1     Running   0          64s   172.30.135.135   k8s-node03   <none>           <none>

### 就绪探针(Readiness Probe)如果检查失败,容器Service流量就会被切断,但K8s集群内所有节点仍可以访问nginx服务
root@k8s-master01:~# kubectl describe pods nginx-pod -n default
………………………..
Events:
  Type     Reason     Age                     From               Message
  ----     ------     ----                    ----               -------
  Normal   Scheduled  7m42s                   default-scheduler  Successfully assigned default/nginx-pod to k8s-node03
  Normal   Pulled     7m41s                   kubelet            Container image "registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24" already present on machine
  Normal   Created    7m41s                   kubelet            Created container: nginx01
  Normal   Started    7m41s                   kubelet            Started container nginx01
  Warning  Unhealthy  2m36s (x64 over 7m31s)  kubelet            Readiness probe failed: Get "http://172.30.135.135:8080/index.html": dial tcp 172.30.135.135:8080: connect: connection refused

root@k8s-master01:~# curl -I 172.30.135.135
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 21 Apr 2025 09:30:26 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 11 Apr 2023 01:45:34 GMT
Connection: keep-alive
ETag: "6434bbbe-267"
Accept-Ranges: bytes


### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-ReadinessProbe.yaml

6、创建Pod 包含存活探针Liveness Probe并测试

(1)、存活探针Liveness Probe参数说明

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    livenessProbe:                  # 选择存活探针Liveness Probe类型
      tcpSocket:                    # 选择探针tcpSocket检查机制,检查端口是否存活
        port: 80                    # 设置检查的存活端口80
      initialDelaySeconds: 10       # 容器启动后等待多少秒开始进行首次探测,默认值0秒
      timeoutSeconds: 2             # 探测请求超时时间,默认值1秒
      periodSeconds: 5              # 执行探测的频率,表示每5秒探测一次。对于Liveness Probe和Readiness Probe,默认值都是10秒。对于Startup Probe默认值没有设置,必须显式指定
      successThreshold: 1           # 在探测成功前需要连续探测多少次才算成功。默认值为1。这通常在Startup Probe中使用以避免过早的成功判断
      failureThreshold: 2           # 在探测失败后需要连续探测多少次才算失败。默认值依赖于探针类型。对于Liveness Probe,默认值为3;对于Readiness Probe,默认值为3;对于Startup Probe,默认值没有设置,必须显式指定
    ports:
    - containerPort: 80
  restartPolicy: Always

(2)、创建一个诊断成功的Pod包含存放探针Liveness Proe(tcpSocket检查规则)

### 编写Pod 包含就绪探针Readiness Probe的yaml文件(指定一个容器真实存在的端口,例如80端口)
cat > /data/yaml/pod/nginx-LivenessProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含存活探针Liveness Probe并检查
kubectl create -f /data/yaml/pod/nginx-LivenessProbe.yaml
kubectl get pods -n default -owide

### 查看创建的Pod是否已生成探针配置
root@k8s-master01:~# kubectl get pods -n default -oyaml | grep -A 7 "livenessProbe"
      livenessProbe:
        failureThreshold: 2
        initialDelaySeconds: 10
        periodSeconds: 5
        successThreshold: 1
        tcpSocket:
          port: 80
        timeoutSeconds: 2

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-LivenessProbe.yaml

(3)、创建一个诊断失败的Pod包含存活探针Liveness Proe(tcpSocket检查规则)

### 编写Pod 包含存活探针Liveness Probe的yaml文件(指定一个容器不存在的端口,例如8080端口)
cat > /data/yaml/pod/nginx-LivenessProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 10
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含存活探针Liveness Probe
kubectl create -f /data/yaml/pod/nginx-ReadinessProbe.yaml

### 查看Pod自动重启次数RESTARTS显示为3次,存活探针Liveness Probe只要探测失败就会重启Pod的容器
root@k8s-master01:~# kubectl get pod -n default
NAME        READY   STATUS    RESTARTS     AGE
nginx-pod   1/1     Running   3 (2s ago)   62s

### 查看Pod运行详细描述,存活探针Liveness Probe诊断失败,容器发生重启
root@k8s-master01:~# kubectl describe pod nginx-pod -n default
…………………
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  93s                default-scheduler  Successfully assigned default/nginx-pod to k8s-node01
  Normal   Created    32s (x4 over 92s)  kubelet            Created container: nginx01
  Normal   Started    32s (x4 over 92s)  kubelet            Started container nginx01
  Warning  Unhealthy  13s (x8 over 78s)  kubelet            Liveness probe failed: dial tcp 172.30.85.203:8080: connect: connection refused
  Normal   Killing    13s (x4 over 73s)  kubelet            Container nginx01 failed liveness probe, will be restarted
  Normal   Pulled     12s (x5 over 92s)  kubelet            Container image "registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24" already present on machine

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-LivenessProbe.yaml

7、创建Pod 包含启动探针Startup Probe并测试

(1)、启动探针Startup Probe参数说明

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    startupProbe:                 # 选择启动探针Startup Probe类型
      exec:                      # 选择探针exec检查机制,检查该文件是否存在容器中
        command:               # 指令方式运行,需要确认容器中有该指令,否则容器会被启动探针判定成失败
        - ls
        - /etc/nginx/nginx.conf
      initialDelaySeconds: 10      # 容器启动后等待多少秒开始进行首次探测,默认值0秒,根据实际情况设置,如果该容器启动非常缓慢,时间设置长些
      timeoutSeconds: 2         # 探测请求超时时间,默认值1秒
      periodSeconds: 5          # 执行探测的频率,表示每5秒探测一次。对于Liveness Probe和Readiness Probe,默认值都是10秒。对于Startup Probe默认值没有设置,必须显式指定
      successThreshold: 1        # 在探测成功前需要连续探测多少次才算成功。默认值为1。这通常在Startup Probe中使用以避免过早的成功判断
      failureThreshold: 2         # 在探测失败后需要连续探测多少次才算失败。默认值依赖于探针类型。对于Liveness Probe,默认值为3;对于Readiness Probe,默认值为3;对于Startup Probe,默认值没有设置,必须显式指定
    ports:
    - containerPort: 80
  restartPolicy: Always

(2)、创建一个诊断成功的Pod包含启动探针Startup Probe(exec检查规则)

### 编写Pod 包含启动探针Startup Probe的yaml文件(ls命令和/etc/nginx/nginx.conf文件已存在该容器中)
cat > /data/yaml/pod/nginx-StartupProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    startupProbe:
      exec:
        command:
        - ls
        - /etc/nginx/nginx.conf
      initialDelaySeconds: 60
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含启动探针Startup Probe并检查
kubectl create -f /data/yaml/pod/nginx-StartupProbe.yaml
kubectl get pods -n default -owide

### 查看创建的Pod是否已生成探针配置
root@k8s-master01:~# kubectl get pods -n default -oyaml | grep -A 9 "startupProbe"
      startupProbe:
        exec:
          command:
          - ls
          - /etc/nginx/nginx.conf
        failureThreshold: 2
        initialDelaySeconds: 60
        periodSeconds: 5
        successThreshold: 1
        timeoutSeconds: 2

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-StartupProbe.yaml

(3)、创建一个诊断失败的Pod包含启动探针Startup Probe(exec检查规则)

### 编写Pod 包含启动探针Startup Probe的yaml文件(ls命令已存在该容器中,但指定的/usr/local/nginx.conf不存在该容器中)
cat > /data/yaml/pod/nginx-StartupProbe.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-pod
  namespace: default
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    imagePullPolicy: IfNotPresent
    name: nginx01
    startupProbe:
      exec:
        command:
        - ls
        - /usr/local/nginx.conf
      initialDelaySeconds: 60
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    ports:
    - containerPort: 80
  restartPolicy: Always
EOF

### 创建Pod 包含启动探针Startup Probe
kubectl create -f /data/yaml/pod/nginx-StartupProbel.yaml

### 查看Pod自动重启次数RESTARTS显示为3次,启动探针Startup Probe只要探测失败就会重启Pod的容器
root@k8s-master01:~# kubectl get pods -n default
NAME        READY   STATUS    RESTARTS      AGE
nginx-pod   0/1     Running   3 (29s ago)   3m59s

### 查看Pod运行详细描述,启动探针Startup Probe诊断失败,容器发生重启
root@k8s-master01:~# kubectl describe pods nginx-pod -n default
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  4m14s                default-scheduler  Successfully assigned default/nginx-pod to k8s-node03
  Normal   Pulled     44s (x4 over 4m13s)  kubelet            Container image "registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24" already present on machine
  Normal   Created    44s (x4 over 4m13s)  kubelet            Created container: nginx01
  Warning  Unhealthy  44s (x6 over 3m9s)   kubelet            Startup probe failed: ls: cannot access '/usr/local/nginx.conf': No such file or directory
  Normal   Killing    44s (x3 over 3m4s)   kubelet            Container nginx01 failed startup probe, will be restarted
  Normal   Started    43s (x4 over 4m13s)  kubelet            Started container nginx01

### 删除Pod
kubectl delete -f /data/yaml/pod/nginx-StartupProbel.yaml

8、Pod平滑退出

### 参数说明
    lifecycle:
       postStart:             ### 容器创建完成后执行指令(要保证容器内必须有mkdir命令,否则容器创建失败),可以是exec httpGet TCPSocket
         exec:
            command:
            - sh
            - -c
            - 'mkdir /data'
       preStop:               ### 设置容器停止前的宽限时间或者执行退出服务命令
         exec:
           command:
           - sh
           - -c
           - sleep 10

### yam文件方式
cat > /data/yaml/pod/nginx.yaml << 'EOF'
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: registry.cn-shenzhen.aliyuncs.com/dockerghost/nginx:1.24
    name: nginx
    lifecycle:
       postStart:
         exec:
            command:
            - sh
            - -c
            - 'mkdir /data'
       preStop:
         exec:
           command:
           - sh
           - -c
           - sleep 10
    startupProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 5
      timeoutSeconds: 2
      periodSeconds: 10
      successThreshold: 1
      failureThreshold: 5
    readinessProbe:
      httpGet:
        path: /index.html
        port: 80
        scheme: HTTP
      initialDelaySeconds: 2
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 2
      timeoutSeconds: 2
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 2
    command:
    - sh
    - -c
    - nginx -g "daemon off;"
  restartPolicy: OnFailure
EOF

cd /data/yaml/
kubectl delete -f nginx.yaml
kubectl create -f nginx.yaml
kubectl get pods
kubectl describe pods nginx
kubectl exec -it nginx -- sh
kubectl delete -f nginx.yaml

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文