Pod的生命周期
1. 初始化容器
2. 两个钩子
3. 两个探针
初始化容器 (initContainers)
## 初始化容器:容器启动之前,我们可以让他做一些准备工作,初始化容器执行完成后就结束退出
## 启动一个容器 挂载相同的目录 ,对目录进行操作
apiVersion: "v1"
kind: "Pod"
metadata:
name: initv2
namespace: default
labels:
app: nginx
spec:
volumes:
- name: nginx-data
hostPath:
path: /data/nginx
initContainers:
- name: init-c
image: centos:7
imagePullPolicy: IfNotPresent
command: ["sh", "-c", "echo 'initContainers' > /usr/share/nginx/html/index.html"]
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
### 运行pod资源清单 查询pod详细信息
[root@master kubernetes]# kubectl apply -f initv2.yaml
pod/initv2 created
NAME READY STATUS RESTARTS AGE IP NODE
initv2 1/1 Running 0 38s 10.2.3.20 node03
nginx-565785f75c-hn9ql 1/1 Running 0 22h 10.2.3.13 node03
### 进入node 查询挂载目录情况
[root@node03 ~]# ll /data/nginx/
total 4
-rw-r--r-- 1 root root 15 Sep 20 14:30 index.html
### 检查网页
[root@master kubernetes]# curl 10.2.3.20
initContainers
钩子 (postStart)(preStop)
#### postStart ####
启动之后 执行
#### preStop ####
结束之前 执行
#### 选项 ####
exec:执行命令
httpGet:检测HTTP
tcpSocket:检测端口
postStart (启动之后执行)
apiVersion: "v1"
kind: "Pod"
metadata:
name: portstart
namespace: default
labels:
os: c7
spec:
conatiners:
- name: c7-container
image: centos:7
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","tail -f /etc/hosts"]
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","groupadd www -g 666 && useradd www -u 666 -g 666 "]
#### 运行 检查
[root@master kubernetes]# kubectl apply -f portstart.yaml
pod/portstart created
[root@master kubernetes]# kubectl exec -it portstart -- /bin/bash
[root@portstart /]# id 666
uid=666(www) gid=666(www) groups=666(www)
preStop (结束之前)
apiVersion: "v1"
kind: "Pod"
metadata:
name: prestop
namespace: default
labels:
os: c7
spec:
volumes:
- name: test-data
hostPath:
path: /data/test/
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","tail -f /etc/hosts"]
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo helloword > /data/1.txt"]
preStop:
exec:
command: ["/bin/sh","-c","echo byebye~ > /data/1.txt"]
volumeMounts:
- name: nginx-data
mountPath: /data
#### 运行 详细信息 检查
[root@master kubernetes]# kubectl apply -f prestop.yaml
pod/prestop created
[root@master kubernetes]# kubectl exec -it prestop -- /bin/bash
[root@prestop /]# cat /data/1.txt
helloword
[root@master kubernetes]# kubectl get pod prestop -owide
NAME READY STATUS RESTARTS AGE IP NODE
prestop 1/1 Running 0 16s 10.2.3.23 node03
----- 关闭后查看 ----
[root@master kubernetes]# kubectl delete -f prestop.yaml
pod "prestop" deleted
[root@node03 ~]# cat /data/test/1.txt
byebye~
探针 (livenessprobe) (readnissprobe)
## 存活性探针(存活态探针)livenessprobe
用于定义容器内,应用是否满足探针指定状态,如果不满足,则删除POD重新拉起一个新的POD
存活探针简单来说就是用来检测容器的应用程序是否还正常工作,如果应用程序不正常,即使容器还活着也没
有意义了,所以这时候就可以使用存活探针来探测,如果应用程序不正常,就重启POD。
exec:执行命令
httpGet:检测HTTP
tcpSocket:检测端口
## 就绪性探针(就绪态探针)readnissprobe
有时候我们Pod本身已经起来了,但是pod的容器还没有完全准备好对外提供服务,那么这时候流量进来就会造
成请求失败的情况出现,针对这种情况k8s有一种探针叫就绪探针,他的作用就是让k8s知道你的Pod内应用是
否准备好为请求提供服务。只有就绪探针ok了才会把流量转发到pod上。
exec:执行命令
httpGet:检测HTTP
tcpSocket:检测端口
#### 参数
initialDelaySeconds: # 第一次执行探针需要在容器启动后等待的时候时间
periodSeconds: # 容器启动后每隔多少秒执行一次存活探针 默认10s
timeoutSeconds: # 探针超时时间,默认1秒,最小1秒
successThreshold: # 探针失败后最少连续探测成功多少次才被认定成功,默认1次,如果是liveness必须为1
failureThreshold: # 探针成功后被视为失败的探测的最小连续失败次数。默认3次。最小值为1
存活性探针 (livenessprobe)
livenessprobe -- exec
### 判断返回值 $? 是否为0
##### exec(执行命令判断返回值) #####
apiVersion: "v1"
kind: "Pod"
metadata:
name: livenessprobe
namespace: default
labels:
app: nginx
spec:
volumes:
- name: nginx-data
hostPath:
path: /data/nginx
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo '存活性探针检查' >> /usr/share/nginx/html/index.html"]
livenessProbe:
exec:
command: ["/bin/sh","-c","cat /usr/share/nginx/html/index.html"]
initialDelaySeconds: 3
periodSeconds: 1
timeoutSeconds: 1
failureThreshold: 3
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
##### 运行 检查 详细信息 ####
[root@master kubernetes]# kubectl apply -f exec-livenessprobe.yaml
pod/livenessprobe created
[root@master kubernetes]# kubectl get pod livenessprobe -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
livenessprobe 1/1 Running 0 11s 10.2.3.27 node03 <none> <none>
[root@master kubernetes]# curl 10.2.3.27
存活性探针检查
--------------------------
--------------------------
#### 测试
[root@node03 ~]# rm /data/nginx/* -rf
[root@master kubernetes]# curl 10.2.3.27 (要快不然看不到)
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.27.1</center>
</body>
</html>
[root@master kubernetes]# curl 10.2.3.27
存活性探针检查
livenessprobe -- httpGet
#### 判断网页的状态码 是否为 200
apiVersion: "v1"
kind: "Pod"
metadata:
name: livenessprobe
namespace: default
labels:
app: nginx
spec:
volumes:
- name: nginx-data
hostPath:
path: /data/nginx
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo '存活性探针检查' >> /usr/share/nginx/html/index.html"]
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 1
timeoutSeconds: 1
failureThreshold: 3
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
#### 运行 详细信息 检查 ####
[root@master kubernetes]# kubectl apply -f httpGet-livenessprobe.yaml
pod/livenessprobe created
[root@master kubernetes]# kubectl get pod livenessprobe -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
livenessprobe 1/1 Running 0 36s 10.2.3.28 node03 <none> <none>
[root@master kubernetes]# curl 10.2.3.28
存活性探针检查
存活性探针检查
------
#### 测试
[root@node03 nginx]# rm index.html -f
[root@master kubernetes]# curl 10.2.3.28
存活性探针检查
livenessprobe -- tcpSocket
#### 检查端口是否存活
apiVersion: "v1"
kind: "Pod"
metadata:
name: livenessprobe
namespace: default
labels:
app: nginx
spec:
volumes:
- name: nginx-data
hostPath:
path: /data/nginx
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo '存活性探针检查' >> /usr/share/nginx/html/index.html"]
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 1
timeoutSeconds: 1
failureThreshold: 3
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
#### 运行 详细信息 检查
[root@master kubernetes]# kubectl apply -f tcpSocket-livenessprobe.yaml
pod/livenessprobe created
[root@master kubernetes]# kubectl get pod livenessprobe -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
livenessprobe 1/1 Running 0 10s 10.2.3.29 node03 <none> <none>
[root@master kubernetes]# curl 10.2.3.29
存活性探针检查
存活性探针检查
------------------------------
------------------------------
#### 测试
[root@master ~]# kubectl exec -it livenessprobe -- sh
/ # vi /etc/nginx/conf.d/default.conf (把80 端口更改为81 端口)
/ # nginx -s reload
2024/09/20 09:21:59 [notice] 42#42: signal process started
/ # command terminated with exit code 137
[root@master ~]# kubectl describe pod livenessprobe
Liveness probe failed: dial tcp 10.2.3.29:80: connect: connection refused
Container nginx failed liveness probe, will be restarted
[root@master kubernetes]# curl 10.2.3.29
存活性探针检查
存活性探针检查
存活性探针检查
就绪性探针(就绪性探针)readnissprobe
readnissprobe -- exec
apiVersion: "v1"
kind: "Pod"
metadata:
name: livenessprobe
namespace: default
labels:
app: nginx
spec:
volumes:
- name: nginx-data
hostPath:
path: /data/nginx
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo '存活性探针检查' >> /usr/share/nginx/html/index.html"]
readinssProbe:
exec:
command: ["/bin/sh","-c","cat /usr/share/nginx/html/001.txt"]
initialDelaySeconds: 3
periodSeconds: 1
timeoutSeconds: 1
failureThreshold: 3
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html/
##### 运行 详细信息 检查
[root@master kubernetes]# kubectl apply -f exec-readnissprobe.yaml
pod/livenessprobe created
[root@master kubernetes]# kubectl get pod livenessprobe -owide
NAME READY STATUS RESTARTS AGE IP NODE
livenessprobe 0/1 CrashLoopBackOff 4 45s 10.2.3.31 node03
[root@master kubernetes]# curl 10.2.3.31
curl: (7) Failed connect to 10.2.3.31:80; Connection refused
[root@master kubernetes]# kubectl describe pod livenessprobe
eadiness probe failed: cat: can't open '/usr/share/nginx/html/001.txt': No such file or director
---------------------------------
---------------------------------
### 测试
[root@node03 nginx]# touch 001.txt
[root@master kubernetes]# kubectl get pod livenessprobe -owide (要快)
NAME READY STATUS RESTARTS AGE IP
livenessprobe 0/1 CrashLoopBackOff 5 2m39s 10.2.3.31
[root@master kubernetes]# kubectl get pod livenessprobe -owide
NAME READY STATUS RESTARTS AGE IP NODE
livenessprobe 1/1 Running 6 3m39s 10.2.3.31 node03
[root@master kubernetes]# curl 10.2.3.31
存活性探针检查
存活性探针检查
存活性探针检查
存活性探针检查
存活性探针检查
存活性探针检查
存活性探针检查