Kubernetes Pod控制器应用
容器监控探针类型有三种:
ExecAction,TCPSockeAction,HTTPGetAction
kubectl explain pods.spec.containers.livenessProbe
kubectl explain pods.spec.containers.livenessProbe.exec
命令执行返回成功是成功的,如果返回状态码是不成功的
使用exec存活性探测:
[root@master test]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 1d
liveness-exec-pod 1/1 Running 5 6m
myapp-848b5b879b-4xpl7 1/1 Running 0 1d
myapp-848b5b879b-7gtn4 1/1 Running 0 1d
myapp-848b5b879b-xtcdd 1/1 Running 0 1d
nginx-deploy-5b595999-fsxlp 1/1 Running 0 1d
^C[root@master test]# ls
liveness-exec.yaml
[root@master test]# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","tmp/healthy"]
initialDelaySeconds: 1
periodSeconds: 3 #探测时间间隔3秒
kubectl describe pods liveness-exec-pod 查看pod的运行日志
Restart Count: 3 重启3次了
Liveness: exec [test -e tmp/healthy] delay=1s timeout=1s period=3s #success=1 #failure=3
监控pod已经重启五次了
[root@master test]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
client 1/1 Running 0 1d
liveness-exec-pod 1/1 Running 5 6m
myapp-848b5b879b-4xpl7 1/1 Running 0 1d
myapp-848b5b879b-7gtn4 1/1 Running 0 1d
myapp-848b5b879b-xtcdd 1/1 Running 0 1d
nginx-deploy-5b595999-fsxlp 1/1 Running 0 1d
使用tcpSocket探测
kubectl explain pods.spec.containers.livenessProbe.tcpSocket
使用httpGet探测
kubectl explain pods.spec.containers.livenessProbe.httpGet
使用tcpSocket探测
kubectl explain pods.spec.containers.livenessProbe.tcpSocket
使用httpGet探测
kubectl explain pods.spec.containers.livenessProbe.httpGet
进入这个容器:
kubectl exec -it liveness-httpget -- /bin/sh
这时候我们在容器的内部删除index.html网页
/ # rm -f /usr/share/nginx/html/index.html
过了几秒后,我们再次查看pod的信息
kubectl describe pods liveness-httpget
日志内容:
Restart Count: 1
Liveness: http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
再次回到容器中:L
kubectl exec -it liveness-httpget -- /bin/sh
文件出现了 /usr/share/nginx/html/index.html
容器启动了并不一定是可以正常使用的,需要里面的服务正常启动之后才可以使用,如:redis和tomcat
这时候我们需要做一个探测。
这时候就用到了Pod应用状态就绪监测:
[root@master test]# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-container
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
启动:kubectl create -f readiness-httpget.yaml
这时候kubectl get pods 是正常启动的
这时候进入容器内部
kubectl exec -it readiness-httpget -- /bin/sh
rm -f /usr/share/nginx/html/index.html
这时候kubectl get pods 是不启动的
# echo "hi">usr/share/nginx/html/index.html
kubectl get pods
这个时候是启动成功的
启动后多久和终止前多久?
kubectl explain pods.spec.containers.lifecycle
利用busybox来测试:
busybox是有httpd服务的,但是没有网页,我们做一个网页,然后再启动httpd服务,看看两个能不能关联。
cat poststart-pod.yaml #暂时有问题
[root@master manifests]# cat poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-httpd
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ['bin/sh','-c','echo Home_Page >> /tmp/index.html']
command: ["/bin/httpd"]
args: ["-f","-h /tmp"]