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

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.

主站蜘蛛池模板: 镇平县| 永昌县| 凭祥市| 利辛县| 五华县| 攀枝花市| 丰县| 永吉县| 江都市| 穆棱市| 井陉县| 安塞县| 玉林市| 兰考县| 丹棱县| 株洲市| 龙岩市| 手游| 聂拉木县| 安远县| 长治县| 贵定县| 修武县| 龙胜| 东安县| 杂多县| 广饶县| 安塞县| 天镇县| 彰武县| 赫章县| 邳州市| 马尔康县| 阿克陶县| 普宁市| 习水县| 清镇市| 大名县| 安陆市| 黔南| 囊谦县|