在 Kubernetes 中,你可以为 Pod 里的容器定义一个健康检查“探针”(Probe)。
这样kubelet 就会根据这个 Probe 的返回值决定这个容器的状态,而不是直接以容器进行是否运行(来自 Docker 返回的信息)作为依据。这种机制,是生产环境中保证应用健康存活的重要手段。
下面,我们结合一个例子来了解一下这个原理
我们挂载nginx.conf到容器中去,以此检查它的健康状态
创建configmap
kubectl create configmap nginx-name --from-file=nginx-key=./nginx.conf
mynginx.yml
apiVersion: v1
kind: Pod
metadata:
name: mynginx
spec:
containers:
- name: test-configmap-nginx
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- name: nginxconfigmap
mountPath: "/etc/nginx/conf.d" # 挂载到容器中目录,这个目录会自动创建
#subPath: nginx2.conf
livenessProbe:
httpGet:
path: /stub_status
port: 80
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3
volumes:
- name: nginxconfigmap
configMap:
name: nginx-name # 创建 configmap 对象的名称
items:
- key: nginx-key # 创建 configmap 对象时指定的 key
path: nginx2.conf # 容器 /etc/config 目录中的文件名
成功访问
kubectl logs -f mynginx #成功访问200
curl 容器IP:/stub_status #得到nginx参数
网友评论