健康检查
使用存活探针
1 创建文件
mkdir -p ~/huawei_k8s/labfile/probefile
cd ~/huawei_k8s/labfile/probefile
2 创建使用execaction模式的存活探针pod文件liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
image: busybox
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
perioSeconds: 5
3 创建pod
kubectl apply -f liveness-exec.yaml
4 使用kubectl get pod -w 监控pod状态,可以看到pod反复重启
kubectl get pod -w
5 查看pod详细信息
kubectl describe pod liveness-exec
确认反复重启,通过event项,确认探针工作流程
6 创建使用http存活探针的pod的yaml文件
#vim liveness-http.yaml
apiVersion: v1
kind : Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: mirrorgooglecontainers/liveness
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
perioSeconds: 3
7 创建该pod
8 观察pod的变化
kubectl get pod -w
9 查看events
kubectl describe pod liveness-http
10 创建使用tcp存活探针的pod的yaml,模板采用httpd镜像
#vim liveness-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-tcp
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 10
periodSeconds: 10
11 创建pod,等待完成
12 进入容器内部,修改提供服务的端口,从默认的80修改为8080,并重启容器内部的httpd服务以应用端口变更。
kubectl exec -it liveness-tcp /bin/bash
sed -i "52c listen 8080" /usr/local/apache2/conf/httpd.conf
sed -i "241c ServerName localhost:8080" /usr/local/apache2/conf/httpd.conf
httpd -k restart
exit
13 等待一段时间后,可以看到pod的restarts次数变成了,由于未通过存活探针检测,pod进行了重启,因此业务又恢复正常,并且端口也恢复到了默认的80端口
kubectl get pod
14 使用describe命令查看pod之前未通过liveness的记录
kubectl describe pod
使用readliness探针
1 创建http的deployment的yaml文件,其中配置readliness探针
#vim httpd-deploymet.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deployment
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
ports:
- containerPort: 80
readinessProbe:
exec:
command:
- cat
- /usr/local/apache2/htdocs/index.html
initialDelaySeconds: 5
periodSeconds: 5
2 创建deloyment
3 创建http服务的yaml文件
#vim httpd-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
app: httpd
ports:
- protocol: TCP
port: 8080
targetPort: 80
4创建 service
kubectl apply -f httpd-svc.yaml
5 查看服务endpoint
kubectl describe service httpd-svc
6 进入一个容器,删除/usr/local/apache2/htdocs/index.html文件
kubectl exec -it httpd-deployment-564dc969bb-cd8q8 /bin/sh
rm /usr/local/apache2/htdocs/index.html
7 使用descibe命令查看endpoint,可以看到pod地址已经从endpoint中移除
kubectl describe service httpd-svc
8 查看pod的详细信息,可以看到pod未通过探针检测
9 查看pod信息,可以看到pod处于notready状态
kubectl get pod