
在Kubernetes中正确关闭容器
(. . — ) Kubernetes-. , drain . , : pod’.
pod’
kubectl drain
pod’ . , :
drain
pod’ control plane. kubelet
, pod’.kubelet
pod’ preStop
preStop
, kubelet
TERM
pod’.kubelet
( pod’, ; 30 ) , ( SIGKILL
). preStop
.
, preStop
, “ ” . , , , TERM
, , , , . , , TERM
(, - ), preStop
API, .
Nginx TERM
, . preStop
. resource lifecycle
. lifecycle
:
lifecycle:
preStop:
exec:
command: [
# Gracefully shutdown nginx
"/usr/sbin/nginx", "-s", "quit"
]
, , SIGTERM
Nginx /usr/sbin/nginx -s quit
. , , Nginx pod, TERM
noop ( — .).
Nginx. , Deployment
:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15
ports:
- containerPort: 80
lifecycle:
preStop:
exec:
command: [
# Gracefully shutdown nginx
"/usr/sbin/nginx", "-s", "quit"
]
pod’
pod’ Nginx , . , , , Nginx , .
, deployment. , , . . pod’.

, Node 1. kubectl drain node-1
, kubelet
preStop
, Nginx:

nginx , . , nginx , , .
, . pod, . , , nginx :

, nginx , pod’ drain
:


, , pod’ , . , , .
? , , ? , pod’ , preStop
, Service
.
Kubernetes AWS , Gruntwork.io.
: