1.Probe(探针)之readinessProbe就绪探针,可用性检查
readinessProbe此探针如果检查失败,pod会处于未就绪状态
1.exec方式检查
#通过rc资源创建了三个pod,然后使用services资源,对外提供三个pod的容器的访问入口。
apiVersion: v1
kind: ReplicationController
metadata:
name: web-rc-readlineprobe
spec:
replicas: 3
selector:
name: lxc
zuoyong: web
template:
metadata:
labels:
name: lxc
zuoyong: web
spec:
zuoyong: web
zuoyong: web
containers:
- name: nginx
image: harbor.lxcedu.com/base-img/nginx:1.14.2
command:
- /bin/sh
- -c
- "touch /tmp/healthy; sleep 5; rm -f /tmp/healthy; sleep 60"
#容器的就绪性检查,如果执行command命令成功则,标记为未就绪状态
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
failureThreshold: 3
initialDelaySeconds: 15
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
name: web-readinessprobe
namespace: default
spec:
selector:
name: lxc
zuoyong: web
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
readinessProbe探针,如果检查不符合条件,pod的状态会变为未就绪,如图READY为0/1
通过查询ep和svc也可以看出三个pod处于未就绪状态,所以也不会加入web-readinessprobe中,所以ep资源就为空,kubectl describe ep web-readinessprobe可以看到三个节点都为未就绪状态。
2.httpGet方式检查
把exec检查方法的无关command删除,再把里面exec换成下面代码就变成httpGet检查
httpGet:
# 指定访问的端口号
port: 80
# 检测指定的访问路径
path: /index.html
启动之后发现pod的容器16秒到17秒状态由未就绪变为就绪,说明:未检查时容器处于未就绪状态。
删除容器的index.html文件,
kubectl exec web-rc-readlineprobe-httpget-hncm4 -- rm /usr/share/nginx/html/index.html
发现对应的pods的容器处于未就绪状态。
3.tcpsocket检查
把exec检查方法的无关command删除,再把里面exec换成下面代码就变成httpGet检查
tcpSocket:
# 指定访问的端口号
port: 80
2.startupProbe启动检查探针
1.16+之后版本才支持
如果提供了启动探针,则所有其他的探针都会被禁用,直到此探针成功为止
如果启动探测失败,kubelet杀死容器,而容器依照重启策略重启。
可以看下面这个例子,结合了三种健康检查,详见资源清单下面说明:
#通过rc资源创建了三个pod,然后使用services资源,对外提供三个pod的容器的访问入口。
apiVersion: v1
kind: ReplicationController
metadata:
name: web-rc-readlineprobe-httpget
spec:
replicas: 3
selector:
name: lxc
zuoyong: web
template:
metadata:
labels:
name: lxc
zuoyong: web
spec:
containers:
- name: nginx
image: harbor.lxcedu.com/base-img/nginx:1.14.2
#容器的就绪性检查,如果执行command命令成功则,标记为未就绪状态
readinessProbe:
httpGet:
# 指定访问的端口号
port: 80
# 检测指定的访问路径
path: /readinessProbe.html
failureThreshold: 3
initialDelaySeconds: 15
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 1
livenessProbe:
httpGet:
port: 80
path: /index.html
failureThreshold: 3
initialDelaySeconds: 15
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 1
startupProbe:
exec:
command:
- cat
- /etc/hh
failureThreshold: 3
initialDelaySeconds: 60
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
name: web-readinessprobe-http-get
namespace: default
spec:
selector:
name: lxc
zuoyong: web
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
首先,在60s之前,此时启动探针未探测,所有其他的探针都会被禁用。
由于启动探针exec的command命令查看的是一个未存在的文件,所以,如果不加干涉,启动探针检查完,kubelet杀死容器,而容器依照重启策略重启
在检查失败前,为其中两个容器创建/etc/hh
可以看到启动探针检查结束,第三个容器开始重启
添加可用性检查探针检查的文件,此时容器处于就绪状态
删除存活性探针检查的文件,此时容器处于重启状态
3.rc资源的升级和回滚
升级:
更改资源清单文件,containers下的image使用新的镜像
更新资源清单文件:kubectl apply -f get rc-xx.xxx.yaml
逐个删除原来的pod: kubectl delete -f pods xxx
由于pod是被rc维护的,删除的pod会重新拉起,代替新的pod
回滚与升级类似
4.ReplicaSet控制器
ReplicaSet控制器简称rs资源,也是用于控制仪pod副本数量。
类似与rc资源,也可以控制pod的存活数量,但是其标签匹配更灵活
先看一个rs资源
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx
spec:
# 指定创建Pod的副本数量,默认值为1.
replicas: 4
# 定义标签选择器,rs资源基于标签选择器关联对应的Pod哟~
selector:
# 基于标签匹配
#labels:
# hobby: sleep
# 基于表达式匹配
matchExpressions:
- key: apps
# values:
# - haha
# - xixi
# - hehe
# - web
# 当operator的值为In或者NotIn时,values的值不能为空。
# - In:
# key的值必须在values定义的数组内。
# - NotIn:
# key的值必须不在values定义的数组内。
# operator: In
# operator: NotIn
# 当operator的值为Exists或者DoesNotExist时,values的值必须为空.
# - Exists:
# 只要存在key即可。
# - DoesNotExist:
# 只要不存在指定的key即可。
# operator: Exists
operator: DoesNotExist
# 定义Pod资源创建的模板
template:
metadata:
labels:
# apps: web
hobby: sleep
spec:
containers:
- name: web
image: harbor.lxcedu.com/base-img/nginx:1.14.2
matchExpressions这种基于表达式匹配的方法,operator的值为Exists或DoesNotExist,value的值必须为空,此时匹配的分别为key不存在和存在的情况,而operator为In和NotIn这种匹配方法匹配的是key有没有包含value,value必须有值,in匹配方式要求key的值必须在values定义的数组内,NotIn匹配方式要求key的值必须不在values定义的数组内