知识点记录-K8S中POD的周期
目录
- 运行过程:(没画图)
- 状态:
- 测试
- pod-initcontainer.yaml
- 周期函数拦截
- pod-hook-exec.yaml
- 容器探测
- pod-livenessprobe-exec.yaml
- 重启策略
- pod-restartpolicy.yaml
运行过程:(没画图)
多个INIT容器串行执行->运行MAIN容器(POST START -> RUNNING(LIVNESS PROBE READINESS PROBE) -> PRE STOP)
状态:
PENDIND 已创建POD 未调度或镜像下载中
RUNNING: 已调度某节点, KUBELET已创建容器
SUCCEEDED: POD中容器已停止不重启
FAILED: 有>=1的POD中容器已停止异常
UNKNOWN: 网络调用状态异常
测试
pod-initcontainer.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-initcontainer
namespace: dev
spec:
containers:
- name: main-container
image: nginx:1.18.1
ports:
- name: nginx-port
containerPort: 80
initContainers: # 顺序执行启动initContainers1->启动initContainers2->启动main-container
- name: initContainers1
image: busybox:1.30
command: ['sh', '-c', 'until ping 192.168.1.201 -c 1 ; do echo waiting; sleep 5; done;']
- name: initContainers2
image: busybox:1.30
command: ['sh', '-c', 'until ping 192.168.1.200 -c 1 ; do echo waiting ; sleep 5; done;']
周期函数拦截
exec方式
lifecycle:
postStart:
exec:
command:
- ls
- /
tcpSocke方式
lifecycle:
postStart:
tcpSocket:
port: 8080
httpGet方式
lifecycle:
postStart:
httpGet:
path: / #URI地址
port: 80 #端口号
host: 192.168.1.100 #主机地址
scheme: HTTP #支持的协议,http或者https
pod-hook-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-hook-exec
namespace: dev
spec:
containers:
- name: main-container
image: nginx:1.18.1
ports:
- name: nginx-port
containerPort: 80
lifecycle:
postStart: # 启动时执行
exec:
command: ["/bin/sh", "-c", "echo postStart"]
preStop: # 停止时执行
exec:
command: ["/bin/sh", "-c", "echo preStop"]
容器探测
有2个类型livenessProbe readinessProbe支持exec tcpSocket httpGet方式
pod-livenessprobe-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-livenessprobe-exec
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.18.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
exec:
command: ["/bin/ls","/"]
initialDelaySeconds: 20 # 容器启动后20s开始探测
timeoutSeconds: 1 # 探测超时时间为1s
重启策略
容器探测执行异常的重启策略,延迟递增为10s、20s、40s、80s、160s和300s
支持Always(默认) OnFailure Never
pod-restartpolicy.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-restartpolicy
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.18.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
httpGet:
scheme: HTTP
port: 80
path: /
restartPolicy: Never # 设置容器重启