k8s的健康检查是通过探针来实现的,探针有两种,livenessprobe和readinessprobe,前者是检查服务是否存活,检查结果为失败时就会重启容器,后者是检查服务是否可访问,检查结果为不可访问时,将从service的endpoints中移除。
探针的检测方法有三种:
1、exec:执行一段命令
2、HTTPGet:通过一个http请求得到返回的状态码
3、tcpSocket:测试某个端口是否可以连通
nginx_pod_exec.yaml:
apiVersion: v1 kind: Pod metadata: name: test-exec labels: app: web spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 args: - /bin/sh - -c - touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy;sleep 600 livenessProbe: exec: command: - cat - /tmp/healthy initialDelaySeconds: 5 periodSeconds: 5
nginx_pod_httpget.yaml:
apiVersion: v1 kind: Pod metadata: name: test-httpget labels: app: web spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 livenessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 5 periodSeconds: 5
nginx_readiness.yaml:
apiVersion: v1 kind: ReplicationController metadata: name: readiness spec: replicas: 2 selector: app: readiness template: metadata: labels: app: readiness spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 readinessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 5 periodSeconds: 5
在测试readiness的时候还需要创建一个service,可以直接通过expose来创建:
kubectl expost rc readiness --port=80
该readiness探针是检查NGINX首页index.html是否存在,如果将某个pod里面NGINX的首页删除,则该pod会从endpoints中移除,index页面回复后又会自动加入到endpoints中。