前言:

pod的重启策略restartpolicy:pod在遇到故障之后重启的动作有:

1.always:当容器退出时,总是重启容器,默认策略

2.onfailure:当容器异常退出(退出状态码非0)时,重启容器

3.nerver:当容器退出时,从不重启容器

备注:k8s中不支持重启pod资源,这里说的重启指的是删除重建pod

一:查看现有pod资源的重启策略

查看重启策略使用kubectl edit 查看

或者将pod资源–export -o yaml 查看

[root@master1 ~]# kubectl edit pod frontend
	imagePullPolicy: Always		#这是镜像拉取策略
  restartPolicy: Always

删除测试pod2.yaml文件生成的资源

[root@master1 ~]# kubectl delete -f pod2.yaml 

pod "frontend" deleted

二:测试重启策略

2.1 在不指定重启策略时,默认使用always

[root@master1 ~]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ceshichongqicelue
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 30;exit 3

busybox可以视为linux内核版本

args 参数

- /bin/sh	在/bin/sh环境
- -c 执行命令

2.2 创建pod,查看状态

[root@master1 ~]# kubectl apply -f pod3.yaml 
pod/ceshichongqicelue created
[root@master1 ~]# kubectl get pods -w	#多等一会
NAME                                READY   STATUS    RESTARTS   AGE
ceshichongqicelue                   1/1     Running   1          56s
ceshichongqicelue   0/1   Error   1     68s
ceshichongqicelue   0/1   CrashLoopBackOff   1     78s
ceshichongqicelue   1/1   Running   2     82s
ceshichongqicelue   0/1   Error   2     112s
ceshichongqicelue   0/1   CrashLoopBackOff   2     2m6s
ceshichongqicelue   1/1   Running   3     2m24s

2.3 此时是重启3次的状态

^C[root@master1 ~]# kubectl edit pod ceshichongqicelue
  restartPolicy: Always

三:修改pod3文件,添加重启策略参数为Never

3.1 首先先把pod3生成的pod删掉,然后再编辑

[root@master1 ~]# kubectl delete -f pod3.yaml 
pod "ceshichongqicelue" deleted
[root@master1 ~]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ceshichongqicelue
spec:
  restartPolicy: Never
  #添加这个
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 30;exit 3

3.2 按照策略来说,nerver代表不会进行重启

[root@master1 ~]# kubectl apply -f pod3.yaml 
pod/ceshichongqicelue created
[root@master1 ~]# kubectl get pods -w
NAME                                READY   STATUS              RESTARTS   AGE
ceshichongqicelue                   0/1     ContainerCreating   0          3s
ceshichongqicelue   1/1   Running   0     4s
ceshichongqicelue   0/1   Error   0     34s
^C[root@master1 ~]kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
ceshichongqicelue                   0/1     Error     0          95s

因为返回状态码为3,所以状态是error

3.3 再修改一下执行动作参数

[root@master1 ~]# kubectl delete -f pod3.yaml 
pod "ceshichongqicelue" deleted
[root@master1 ~]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ceshichongqicelue
spec:
  restartPolicy: Never
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10
[root@master1 ~]# kubectl apply -f pod3.yaml 
pod/ceshichongqicelue created
[root@master1 ~]# kubectl get pods -w
NAME                                READY   STATUS              RESTARTS   AGE
ceshichongqicelue                   0/1     ContainerCreating   0          4s
ceshichongqicelue                   1/1     Running   0          9s
ceshichongqicelue   0/1   Completed   0     14s

3.4 不会执行重启

Logo

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

更多推荐