您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
22、Kubernetes - 资源清单 - 探针
发布时间:2022-12-15 22:56:15编辑:雪饮阅读()
Step1
接续上篇,上篇最后那个不是只是创建了,但是运行结果我们不得而知。
可以看到的是它于是运行的状态的
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 2 23h
等待它一分钟后
[root@k8s-master01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 3 23h
可以看到有重启的情况
重点在这里
[root@k8s-master01 ~]# cat liveness-exec-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-containers
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/live ; sleep 60; rm -rf /tmp/live; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/live"]
initialDelaySeconds: 1
periodSeconds: 3
command在容器启动时候创建了/tmp/live的文件,并休眠60秒后将其删除,然后再次休眠1小时。
而livenessProbe里面的exec里面的command则是在容器启动后1秒就开始检测这个/tmp/live文件是否存在,然后周期性检测则是每3秒检查一下。
那么当第一个command在60秒(说是60秒,不一定,因为还有延时和启动耗时等情况)后就删除了/tmp/live,所以会导致容器被livenessProbe检测为有问题了,于是就据说是删除了这个容器,然后pod据说就会自动触发重启了。
Step2
存活检测-http
用这个模板再创建一个pod
[root@k8s-master01 ~]# cat livenessProbe-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.atguigu.com/library/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
当然,我们先将上面那个pod干掉了
[root@k8s-master01 ~]# kubectl delete pod liveness-exec-pod
pod "liveness-exec-pod" deleted
不然看起来有点乱。
可见新的pod仍然是可以运行并且可以访问的
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 0 3m22s
[root@k8s-master01 ~]#
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
liveness-httpget-pod 1/1 Running 0 3m49s 10.224.2.26 k8s-node02 <none> <none>
[root@k8s-master01 ~]# curl 10.224.2.26
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master01 ~]# curl 10.224.2.26/index.html
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
那么我们删除该新pod中的这个可访问的文件
[root@k8s-master01 ~]# kubectl exec liveness-httpget-pod -it -- /bin/sh
/ # rm -rf /usr/share/nginx/html/index.html
/ # exit
持续一小下,就发现又重启了
[root@k8s-master01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 6m3s
这个存活检测,也是检测path中对应的文件是否存在的。
道理和上面的其实差不多,那么我们也可以这样进行删除,更便捷,且也可以看到pod又一次被重启了。
[root@k8s-master01 ~]# kubectl exec liveness-httpget-pod -it -- rm -rf /usr/share/nginx/html/index.html
[root@k8s-master01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 8m19s
liveness-httpget-pod 1/1 Running 2 8m24s
Step3
再次将step2的容器也干掉
kubectl delete pod liveness-httpget-pod
这次我们直接测试tcp的存活检测,据说直接用80端口不好测试,说是如果直接干掉nginx,则会什么什么之类的退出。
然后创建下tcp的存活检测yaml模板
[root@k8s-master01 ~]# cat livenessProbe-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
name: probe-tcp
spec:
containers:
- name: nginx
image: hub.atguigu.com/library/myapp:v1
livenessProbe:
initialDelaySeconds: 5
timeoutSeconds: 1
tcpSocket:
port: 8080
periodSeconds: 3
然后同样创建之
kubectl create -f livenessProbe-tcp.yaml
同样的道理,但这里是因为这个容器中没有默认运行的8080端口哈
[root@k8s-master01 ~]# kubectl create -f livenessProbe-tcp.yaml
pod/probe-tcp created
[root@k8s-master01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
probe-tcp 1/1 Running 0 2s
probe-tcp 1/1 Running 1 14s
Step3
LivenessProbe与readinessProbe合并探测
同样先是创建yaml
[root@k8s-master01 ~]# cat liveness-httpget-pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: hub.atguigu.com/library/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: 80
path: /index1.html
initialDelaySeconds: 1
periodSeconds: 3
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
然后创建
kubectl create -f liveness-httpget-pod2.yaml
同样的别忘了干掉上面的step2的tcp的探测,免的太乱
kubectl delete pod probe-tcp
观察这个新建的合并readinessProbe与livenessProbe的探测pod发现虽然运行了,但是未就绪
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 0/1 Running 0 3m27s
那么同样的道理使其就绪
[root@k8s-master01 ~]# kubectl exec liveness-httpget-pod -it -- /bin/sh
/ # echo "123" > /usr/share/nginx/html/index1.html
/ # exit
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 0 5m29s
就绪检测ok,那么livenessProbe又怎样呢?
root@k8s-master01 ~]# kubectl exec liveness-httpget-pod -it -- rm -rf /usr/share/nginx/html/index.html
干掉这个被livenessProbe检测的文件后再次看看
[root@k8s-master01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 0/1 Running 1 7m56s
可见,也终于再次重启了
关键字词:Kubernetes,资源,清单,探针