pod被"正式的使用"之前需要做一些简单的检测pod的服务可用性,就此,readiness probe
就派上用场
readiness probe
当使用kubectl get pods
,在READY中的1/1,右边的1是POD内容器数量,而左边的1是就绪的个数。倘若不定义,那么在启动时候,就会立刻就绪
[root@linuxea linuxea]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-linuxea 1/1 Running 0 3d
linuxea.com-httpget 1/1 Running 1 3h
nginx-linuxea-5786698598-dx2jr 1/1 Running 0 1d
nginx-linuxea-5786698598-stttv 1/1 Running 0 1d
nginx-linuxea-5786698598-t6lpx 1/1 Running 0 1d
pod-demo-linuxea 3/3 Running 0 1d
但是,这样启动就会存在问题。在就绪探测中,倘若一个pod在启动时立刻就绪,那么可能会出现问题,在容器内的程序就绪之前有很多过程可能尚未准备完成。如:展开文件,启动程序如上图所示,service通过标签选择器关联各个pod。此刻,pod_NEW符合选择器的选择条件,pod_NEW将会立刻成为service标签选择器都后端pod之一。此刻,pod_NEW并未就绪,自我初始化尚未完成。那么此刻调度时,service倘若请求到pod_NEW上就会出错。
当然,期望是在调度时,pod_NEW的应用程序已经就绪,也就说以及初始化完成为此,使用readiness probe来做就绪探测。做一个测试
测试
[root@linuxea linuxea]# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: linuxea.com-httpget-readiness
namespace: default
spec:
containers:
- name: readiness-httpd-linuxea
image: marksugar/nginx:1.14.a
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
readinessProbe:
httpGet:
port: http
path: /linuxea.html
initialDelaySeconds: 1
periodSeconds: 3
开始创建
[root@linuxea linuxea]# kubectl create -f readiness-httpget.yaml
pod/linuxea.com-httpget-readiness created
已经创建成功
[root@linuxea linuxea]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE
client-linuxea 1/1 Running 0 3d
linuxea.com-httpget 1/1 Running 2 4h
linuxea.com-httpget-readiness 1/1 Running 0 5s
而后删除掉/data/wwwroot/linuxea.html
[root@linuxea linuxea]# kubectl exec -it linuxea.com-httpget-readiness -- /bin/sh
/ # ls /data/wwwroot/
index.html linuxea.html
/ # rm -rf /data/wwwroot/linuxea.html
/ #
紧接着就绪检测失败,不在就绪状态
[root@linuxea linuxea]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE
client-linuxea 1/1 Running 0 3d
linuxea.com-httpget 1/1 Running 2 4h
linuxea.com-httpget-readiness 0/1 Running 0 52s
此刻,可以使用kubectl describe pods linuxea.com-httpget-readiness
查看
- 恢复
在将文件补齐echo `date`-linuxea >> /data/wwwroot/linuxea.html
[root@linuxea linuxea]# kubectl exec -it linuxea.com-httpget-readiness -- /bin/sh
/ # ls /data/wwwroot/
index.html linuxea.html
/ # rm -rf /data/wwwroot/linuxea.html
/ # echo `date`-linuxea >> /data/wwwroot/linuxea.html
READY又恢复就绪状态
[root@linuxea linuxea]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-linuxea 1/1 Running 0 3d
linuxea.com-httpget 1/1 Running 2 4h
linuxea.com-httpget-readiness 1/1 Running 0 6m
curl
[root@linuxea linuxea]# curl 172.16.3.30/linuxea.html
Mon Aug 27 13:21:06 UTC 2018-linuxea
[root@linuxea linuxea]#
钩子
PostStart :创建容器后,立即执行的操作,如果执行失败,容器终止,并且重启。重启与否取决于重启策略prestop:容器终止前执行命令,执行完成后终止都有三种行为:exec,httpget,tcpSocketexec:commamd命令httpget: get请求tcpsocket: socket请求
PostStart
[root@linuxea linuxea]# cat poststart-linuxea.yaml
apiVersion: v1
kind: Pod
metadata:
name: linuxea.com.poststart
namespace: default
spec:
containers:
- name: poststart-linuxea-com
image: marksugar/nginx:1.14.a
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
lifecycle:
postStart:
exec:
command:
- "/bin/sh"
- "-c"
- >
if [ ! -d /data/wwwroot/linuxea.com/ ]; then
mkdir -p /data/wwwroot/linuxea.com;
echo `date`-linuxea.com >>/data/wwwroot/linuxea.com/index.html;
sed -i 's#/data/wwwroot#/data/wwwroot/linuxea.com#g' /etc/nginx/nginx.conf;
fi;
if [ -s /data/wwwroot/linuxea.com/index.html ]; then
chown -R www.www /data/wwwroot;
fi;
create
[root@linuxea linuxea]# kubectl create -f poststart-linuxea.yaml
pod/linuxea.com.poststart created
[root@linuxea linuxea]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-linuxea 1/1 Running 0 4d
linuxea.com-httpget 1/1 Running 2 21h
linuxea.com-httpget-readiness 1/1 Running 0 17h
linuxea.com.poststart 1/1 Running 0 3s
[root@linuxea linuxea]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
client-linuxea 1/1 Running 0 4d 172.16.2.252 linuxea.node-2.com <none>
linuxea.com-httpget 1/1 Running 2 21h 172.16.2.12 linuxea.node-2.com <none>
linuxea.com-httpget-readiness 1/1 Running 0 17h 172.16.3.30 linuxea.node-3.com <none>
linuxea.com.poststart 1/1 Running 0 10s 172.16.1.32 linuxea.node-1.com <none>
[root@linuxea linuxea]# curl 172.16.1.32
Tue Aug 28 06:17:41 UTC 2018-linuxea.com
通过sh登陆到pod容器内
[root@linuxea linuxea]# kubectl exec -it linuxea.com.poststart -- /bin/sh
/ # cat /data/wwwroot/linuxea.com/index.html
Tue Aug 28 06:17:41 UTC 2018-linuxea.com
/ # cat /etc/nginx/nginx.conf|grep root
root /data/wwwroot/linuxea.com;
root /data/wwwroot/linuxea.com;