官术网_书友最值得收藏!

Life cycle hooks or graceful shutdown

As you run into failures in real-life scenarios, you may find that you want to take additional action before containers are shut down or right after they are started. Kubernetes actually provides life cycle hooks for just this kind of use case.

The following example controller definition, apache-hooks-controller.yaml, defines both a postStart action and a preStop action to take place before Kubernetes moves the container into the next stage of its life cycle:

apiVersion: v1 
kind: ReplicationController
metadata:
name: apache-hook
labels:
name: apache-hook
spec:
replicas: 3
selector:
name: apache-hook
template:
metadata:
labels:
name: apache-hook
spec:
containers:
- name: apache-hook
image: bitnami/apache:latest
ports:
- containerPort: 80
lifecycle:
postStart:
httpGet:
path: http://my.registration-server.com/register/
port: 80
preStop:
exec:
command: ["/usr/local/bin/apachectl","-k","graceful-
stop"]

You'll note that, for the postStart hook, we define an httpGet action, but for the preStop hook, we define an exec action. Just as with our health checks, the httpGet action attempts to make an HTTP call to the specific endpoint and port combination, while the exec action runs a local command in the container.

The httpGet and exec actions are both supported for the postStart and preStop hooks. In the case of preStop, a parameter named reason will be sent to the handler as a parameter. See the following table for valid values:

Valid preStop reasons

It's important to note that hook calls are delivered at least once. Therefore, any logic in the action should gracefully handle multiple calls. Another important note is that postStart runs before a pod enters its ready state. If the hook itself fails, the pod will be considered unhealthy.

主站蜘蛛池模板: 江达县| 威远县| 万源市| 乌拉特中旗| 宜黄县| 舟山市| 沙坪坝区| 曲麻莱县| 探索| 砀山县| 霍邱县| 鄢陵县| 肇源县| 涟水县| 西宁市| 科技| 婺源县| 靖宇县| 自贡市| 塔河县| 株洲市| 聂荣县| 贞丰县| 始兴县| 南城县| 黑山县| 汉寿县| 平潭县| 汝城县| 新建县| 闸北区| 隆尧县| 许昌县| 大埔县| 元阳县| 若尔盖县| 山东| 常宁市| 芒康县| 阜南县| 阿坝|