背景:假如容器启动命令使用了sh,在删除pod时没有接收SIGTERM信号,只能默认等待30s优雅退出时间。在一些demo测试时颇为浪费时间。
解决:使用trap捕捉到SIGTERM信号时直接退出。
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
k8s-app: test
name: test
namespace: default
spec:
serviceName: test
replicas: 1
selector:
matchLabels:
k8s-app: test
template:
metadata:
labels:
k8s-app: test
spec:
nodeSelector:
your_label: test
hostNetwork: true
containers:
- name: test
image: busybox:latest
command:
- /bin/sh
args:
- -c
- trap 'echo trap SIGTERM $(date +"%F %T");exit' SIGTERM; while true; do echo 'Hello'; sleep 10; done