- Getting Started with Kubernetes
- Jonathan Baier Jesse White
- 276字
- 2021-06-10 19:47:09
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:

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.
- 電氣自動(dòng)化專業(yè)英語(yǔ)(第3版)
- 腦動(dòng)力:C語(yǔ)言函數(shù)速查效率手冊(cè)
- Hands-On Machine Learning on Google Cloud Platform
- R Machine Learning By Example
- 傳感器技術(shù)應(yīng)用
- 分布式多媒體計(jì)算機(jī)系統(tǒng)
- CompTIA Linux+ Certification Guide
- JavaScript典型應(yīng)用與最佳實(shí)踐
- 精通數(shù)據(jù)科學(xué)算法
- PostgreSQL 10 Administration Cookbook
- 西門子變頻器技術(shù)入門及實(shí)踐
- 手機(jī)游戲程序開發(fā)
- Linux系統(tǒng)下C程序開發(fā)詳解
- Web編程基礎(chǔ)
- 空間機(jī)器人