Kubernetes----Pod配置容器重启策略

【原文链接】

一、重启策略

pod的重启策略有3种,如下:

  • Always:容器失效时,自动重启该容器,这是默认值
  • OnFailure:容器停止运行且退出码不为0时重启
  • Never:不论状态为何,都不重启该容器

重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长为10s,20s,40s,80s,160s,300s,300s是最大延迟时长

如下,修改pod_restartPolicy.yaml文件,其中restartPolicy字段即设置重启策略,这里需要注意的是,restartPolicy字段跟containers字段是平行的,是对所有容器生效的

apiVersion: v1
kind: Namespace
metadata:
  name: dev

---

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports:
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /hello
  restartPolicy: Never

使用如下命令创建pod

[root@master resource_manage]# kubectl apply -f pod_restartPolicy.yaml
namespace/dev created
pod/pod-nginx created
[root@master resource_manage]#

通过如下命令查询,可以发现这里因为设置Never重启策略,所以这里没有重启痕迹

[root@master resource_manage]# kubectl get pod -n dev
NAME        READY   STATUS      RESTARTS   AGE
pod-nginx   0/1     Completed   0          25m
[root@master resource_manage]# kubectl describe pod pod-nginx -n dev
Name:         pod-nginx
Namespace:    dev
Priority:     0
Node:         node2/192.168.16.42
Start Time:   Thu, 24 Mar 2022 15:35:21 +0800
Labels:       user=redrose2100
Annotations:  <none>
Status:       Succeeded
IP:           10.244.2.42
IPs:
  IP:  10.244.2.42
Containers:
  nginx:
    Container ID:   docker://4203b0f3ccd87a1df0012c08ada6fb6d1cbac583da1b63dffc5d2ceb194c0255
    Image:          nginx:1.17.1
    Image ID:       docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Thu, 24 Mar 2022 15:35:21 +0800
      Finished:     Thu, 24 Mar 2022 15:35:51 +0800
    Ready:          False
    Restart Count:  0
    Liveness:       http-get http://:80/hello delay=0s timeout=1s period=10s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7sn8d (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-7sn8d:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  25m                default-scheduler  Successfully assigned dev/pod-nginx to node2
  Normal   Pulled     25m                kubelet            Container image "nginx:1.17.1" already present on machine
  Normal   Created    25m                kubelet            Created container nginx
  Normal   Started    25m                kubelet            Started container nginx
  Warning  Unhealthy  24m (x3 over 25m)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    24m                kubelet            Stopping container nginx
[root@master resource_manage]#

使用如下命令可以删除资源

[root@master resource_manage]# kubectl delete -f pod_restartPolicy.yaml
namespace "dev" deleted
pod "pod-nginx" deleted
[root@master resource_manage]#

二、重启策略设置建议

因为重启策略默认的就是Always,这也是合理的,因此在一般情况下,重启策略不需要设置,这里仅仅是作为知识点拿出来展示一下,在实际使用中,在大多数情况下都不需要进行重启策略配置

你可能感兴趣的:(Kubernetes,kubernetes,docker,容器)