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

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 shutdown or right after they are started. Kubernetes actually provides life cycle hooks for just this kind of use case.

The following example controller definition 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 (you can refer to more details about this in point 1 in the References section at the end of the chapter):

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"]

Listing 2-11: apache-hooks-controller.yaml

You'll note for the postStart hook, we define an httpGet action, but for the preStop hook, I 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  (refer to point 1 in References section)

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.

主站蜘蛛池模板: 长宁区| 麻阳| 常宁市| 伊金霍洛旗| 上虞市| 利辛县| 九寨沟县| 禹城市| 静安区| 潜山县| 连云港市| 济源市| 信宜市| 新安县| 烟台市| 宜宾县| 达州市| 海南省| 长春市| 蒲城县| 弋阳县| 柞水县| 许昌市| 鲜城| 武汉市| 若尔盖县| 手机| 曲沃县| 宝丰县| 桃江县| 阿鲁科尔沁旗| 民乐县| 张北县| 长治市| 绵阳市| 鹿泉市| 西乡县| 长武县| 庆元县| 霍州市| 双城市|