- apiVersion: v1
- kind: Pod
- metadata:
- name: healthcheck
- labels:
- test: healthcheck
- spec:
- restartPolicy: OnFailure # 默认为Always
- containers:
- - name: healthcheck
- image: busybox
- args:
- - /bin/sh
- - -c
- - sleep 10; exit 1 # 模拟容器启动 10 秒后发生故障
- apiVersion: v1
- kind: Pod
- metadata:
- name: liveness
- labels:
- test: liveness
- spec:
- restartPolicy: OnFailure # 默认为Always
- containers:
- - name: liveness
- image: busybox
- args:
- - /bin/sh
- - -c
- - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
- livenessProbe:
- exec:
- command:
- - cat
- - /tmp/healthy
- initialDelaySeconds: 10 # 指定容器启动10s后开始执行Liveness探测
- periodSeconds: 5 # 指定每5s执行一次Liveness探测,默认是10s,最小是1s
- timeoutSeconds: 1 # 探测超时时间,默认是1s,最小是1s
启动进程首先创建文件/tmp/healthy,30s后删除,如果/tmp/healthy文件存在,则认为容器处于正常状态,反之则发生故障。
- apiVersion: v1
- kind: Pod
- metadata:
- name: readiness
- labels:
- test: readiness
- spec:
- restartPolicy: OnFailure # 默认为Always
- containers:
- - name: readiness
- image: busybox
- args:
- - /bin/sh
- - -c
- - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
- readinessProbe:
- exec:
- command:
- - cat
- - /tmp/healthy
- initialDelaySeconds: 10 # 指定容器启动10s后开始执行Readiness探测
- periodSeconds: 5 # 指定每5s执行一次Liveness探测
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: web-deployment
- spec:
- selector: # 通过标签选择被控制的pod
- matchLabels:
- app: web
- replicas: 2
- template:
- metadata:
- labels:
- app: web
- spec:
- containers:
- - name: web
- image: web:1.0
- ports:
- - containerPort: 8080 # 转发到后端pod的端口号
- readinessProbe:
- httpGet:
- scheme: HTTP
- path: /python
- port: 8080
- initialDelaySeconds: 10
- periodSeconds: 5
-
- ---
- apiVersion: v1
- kind: Service
- metadata:
- name: web-svc
- spec:
- type: NodePort # 添加NodePort类型的Service
- selector:
- app: web
- ports: # 将 Service 的 8080 端口映射到 Pod 的 8080 端口,使用 TCP 协议
- - protocol: TCP
- port: 8080 # service监听端口
- targetPort: 8080 # 转发到后端pod的端口号
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: app
- spec:
- selector: # 通过标签选择被控制的pod
- matchLabels:
- app: app
- replicas: 10
- template:
- metadata:
- labels:
- app: app
- spec:
- containers:
- - name: app
- image: busybox
- args:
- - /bin/sh
- - -c
- - sleep 10; touch /tmp/healthy; sleep 30000
- readinessProbe:
- exec:
- command:
- - cat
- - /tmp/healthy
- initialDelaySeconds: 10
- periodSeconds: 5