赞
踩
目录
我们一般将pod对象从创建至终的这段时间范围内称为pod生命周期
Pod 遵循一个预定义的生命周期,起始于 Pending 阶段,如果至少 其中有一个主要容器正常启动,则进入 Running,之后取决于 Pod 中是否有容器以 失败状态结束而进入 Succeeded 或者 Failed 阶段。
Pod 可以包含多个容器,同时 Pod 也可以有一个或多个先于应用容器启动的 Init 容器。Init 容器和普通容器区别不大,主要是init优先运行,init成功运行完成后,才会启动主容器,所以Init 容器不支持 Readiness。如果 Pod 的 Init 容器启动失败,Kubernetes 会不断地重启该 Pod,直到 Init 容器成功为止。
init容器是在pod的生命周期,保证该pod运行的一些前置条件满足之后才开始运行这个pod,例如需要依赖一些其他的pod,服务等,可以去对这些服务的状态进行检测,等所有前置的pod都好了才开始运行主容器
从名字上来看,也能看出是的用途就是运行一些初始化任务,来保证应用容器运行环境。
[root@k8s2 pod]# vim init-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
拉起pod,可以看到初始化无法完成,在init容器没有成功运行之前,主容器不会被运行
[root@k8s2 pod]# kubectl apply -f init-pod.yaml
[root@k8s2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/1 0 41s
继续修改init.yaml文件,在下面添加init要满足的服务
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
再次拉起pod,可以看到成功running
[root@k8s2 pod]# kubectl apply -f init-pod.yaml
[root@k8s2 pod]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
myservice ClusterIP 10.101.80.32 <none> 80/TCP 15s
svc解析成功后,init容器退出,主容器运行
[root@k8s2 pod]# kubectl exec -it myapp-pod -- sh
/ # nslookup myservice.default.svc.cluster.local.
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: myservice.default.svc.cluster.local
Address: 10.101.80.32
我们都知道在k8s中,最小的单位叫作pod,在pod中运行着一个个容器,那么在k8s中是如何得知pod中的容器是否正常运行呢,所以k8s就引入了探针的概念
就绪探针(readinessProbe)
主要用来检测应用容器是否允许完成
只有通过了就绪探针的检测,pod才会变为ready或running状态
所以当有pod一直处于init状态时,可以尝试去看看资源清单中的就绪探针来排查问题
存活探针(livenessProbe)
这里就和心态检测机制类似
去判断应用容器是否存活,如果检测失败,kubelet就会杀死容器,杀死后,容器将会受到重启策略的影响
只有存活探针通过时,容器会运行,但只能内部运行,无法对外访问。只有两个探针都运行成功,才可以对外访问。
存活探针
编辑pod.yaml ,加入存活探针
[root@k8s2 pod]# vim liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe: #存活探针,通过监测8080端口判断是否存活
tcpSocket:
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
运行探针
[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml
yong命令查询容器状态,此时发现容器无法启动,报错显示端口问题,因为改变了端口,所以监测不到,所以容器一直无法启动。
[root@k8s2 pod]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
liveness-http 1/1 Running 0 5s
liveness-http 1/1 Running 1 (0s ago) 13s
liveness-http 1/1 Running 2 (0s ago) 22s
[root@k8s2 pod]# kubectl describe pod liveness-http

删掉存活探针
[root@k8s2 pod]# kubectl delete -f liveness-pod.yaml
就绪探针
表示容器是否准备好服务请求(ready),就绪探针就绪后,svc暴露端口
编辑pod.yaml 文件,添加就绪探针
[root@k8s2 pod]# vim liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe: #存活探针,通过监测80端口判断是否存活
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe: #就绪探针, 通过监测/test.html这个文件判断是否就绪
httpGet:
path: /test.html
port: 80
initialDelaySeconds: 5
periodSeconds: 5
运行容器探针
[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml
就绪探针失败导致容器一直未就绪
[root@k8s2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-http 0/1 Running 0 34s
[root@k8s2 pod]# kubectl describe pod liveness-http
THTTP404显示,那就是没找到发布文件了

创建测试页面
[root@k8s2 pod]# kubectl exec liveness-http -- touch /usr/share/nginx/html/test.html
就绪探针成功
[root@k8s2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-http 1/1 Running 0 100s
创建svc
[root@k8s2 pod]# kubectl expose pod liveness-http --port 80 --target-port 80
就绪容器自动上线
[root@k8s2 pod]# kubectl describe svc liveness-http
Name: liveness-http
Namespace: default
Labels: test=liveness
Annotations: <none>
Selector: test=liveness
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.108.21.178
IPs: 10.108.21.178
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.33:80
Session Affinity: None
Events: <none>
删除测试页面,此时容器不健康了
[root@k8s2 pod]# kubectl exec liveness-http -- rm /usr/share/nginx/html/test.html
就绪探针失败,容器未就绪
[root@k8s2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-http 0/1 Running 0 6m15s
在svc中容器自动下线
[root@k8s2 pod]# kubectl describe svc liveness-http
Name: liveness-http
Namespace: default
Labels: test=liveness
Annotations: <none>
Selector: test=liveness
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.108.21.178
IPs: 10.108.21.178
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints:
Session Affinity: None
Events: <none>
回收容器
[root@k8s2 pod]# kubectl delete -f liveness-pod.yaml
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。